4

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!

5
  • 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.
    – colm.anseo
    Sep 13, 2020 at 18:03
  • I am sorry but I didn't understand...
    – fbarril
    Sep 13, 2020 at 18:04
  • Docker containers have independent network stacks. For all intends and purposes you can consider them to be different computers in this regard. Consequently, localhost in one container is a different network interface from localhost in another container. See docs.docker.com/network/network-tutorial-standalone.
    – Peter
    Sep 13, 2020 at 18:23
  • You can create your own network and connect those two container or you can use the --link option. Check the config with docker inspect <name-of-container> and check Networks. It should have the same gateway.
    – user6516856
    Sep 13, 2020 at 18:42
  • docker-compose is a useful technology for orchestrating multiple docker containers and linking their networks.
    – colm.anseo
    Sep 13, 2020 at 19:05

2 Answers 2

7

Expanding on my comment. Your client code's network address will not work:

resp, err := http.Post("http://localhost:8000/orders", "application/json", bytes.NewBuffer(requestBody)) // broken

as it is literally talking to itself (localhost refer to the client docker container - not the host OS).


The quickest way - for testing purposes - to expose the two containers to your host OS would be like so:

docker run -it --rm -p 8000:8000 dockweb  # server container
docker run -it --rm --net host dockcli    # client container

Beyond trivial testing, this can quickly get unwieldy. So I recommend using things like docker-compose which allows you to trivially link containers and their networks.

3
  • 2020/09/13 18:24:47 Post "http://:8000/orders": dial tcp :8000: connect: connection refused
    – fbarril
    Sep 13, 2020 at 18:24
  • @fbarril updated answer with some short & long term suggestions.
    – colm.anseo
    Sep 13, 2020 at 19:04
  • How would you do this with something running (say, a .Net Kestrel web server running locally) that is not in a container? It's weird, because I've had things not in containers that can freely talk to Redis, Elastic, etc.
    – Jeff Putz
    Feb 16 at 21:47
0

Try using 0.0.0.0 instead of localhost. This should resolve the connection refused error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.