I am trying to make an http request from one project to another both using GO. The project that is making the request has the following dockerfile:
FROM golang:alpine as builder
WORKDIR /build
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -o main .
FROM scratch
WORKDIR /app
COPY --from=builder /build .
CMD ["./main"]
The project that is waiting for the request is running on localhost:8000 and it has the following dockerfile:
FROM golang:1.13.8 AS build-env
ADD . /dockerdev
WORKDIR /dockerdev
RUN go get -d -v ./...
RUN go build -o /server
# Final stage
FROM debian:buster
EXPOSE 8000
WORKDIR /
COPY --from=build-env /server /
CMD ["/server"]
When I make
resp, err := http.Post("http://localhost:8000/orders", "application/json", bytes.NewBuffer(requestBody))
it gives me the following error
dial tcp 127.0.0.1:8000: connect: connection refused
I am new to docker so any improvements are welcome!
127.0.0.1
localhost in the second docker container will just point to the container itself - not the host itself. Drop the hostname & just list the port i.e.:8000
and ensure the port is passed through to the host OS.--link
option. Check the config withdocker inspect <name-of-container>
and check Networks. It should have the same gateway.