43

I've been trying to figure this out in the last hours but I'm stuck.

I have a very simple Dockerfile which looks like this:

FROM alpine:3.6
COPY gempbotgo /
COPY configs /configs
CMD ["/gempbotgo"]
EXPOSE 8025

gempbotgo is just an go binary which runs a webserver and some other stuff. The webserver is running on 8025 and should answer with an hello world.

My issue is with exposing ports. I ran my container like this (after building it)

docker run --rm -it -p 8025:8025 asd

Everything seems fine but when I try to open 127.0.0.1:8025 in the browser or try a wget i just get an empty response. Chrome: ERR_EMPTY_RESPONSE

The port is used and not restricted by the firewall on my Windows 10 system. Running the go binary without container just on my "Bash on Ubuntu on Windows" terminal and then browsing to 127.0.0.1:8025 works without a hitch. Other addresses returned a "ERR_CONNECTION_REFUSED" like 127.0.0.1:8030 so there definetly is something active on the port.

I then went into the conatiner with

docker exec -it e1cc6daae4cf /bin/sh

and checked in there with a wget what happens. Also there no issues. index.html file gets downloaded with a "Hello World"

Any ideas why docker is not sending any data? I've also ran my container with docker-compose but no difference there.

I also ran the container on my VPS hosted externally. Same issue there... (Debian)

My code: (note the Makefile) https://github.com/gempir/gempbotgo/tree/docker

Edit:

After getting some comments I changed my Dockerfile to a multi-stage build. This is my Dockerfile now:

FROM golang:latest
WORKDIR /go/src/github.com/gempir/gempbotgo
RUN go get github.com/gempir/go-twitch-irc \
    && go get github.com/stretchr/testify/assert \
    && go get github.com/labstack/echo \
    && go get github.com/op/go-logging
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest  
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY configs ./configs
COPY --from=0 /go/src/github.com/gempir/gempbotgo/app .
CMD ["./app"]  
EXPOSE 8025

Sadly this did not change anything, I kept everything as close as possbile to the guide here: https://docs.docker.com/engine/userguide/eng-image/multistage-build/#use-multi-stage-builds

I have also tried the minimalist Dockerfile from golang.org which looks like this:

FROM golang:onbuild
EXPOSE 8025

But no success either with that.

gempir
  • 1,791
  • 4
  • 22
  • 46
  • How is the binary built? Any output from the container? Tried another base like `ubuntu` to verify it isn't a libc issue? – Andy Shinn Sep 12 '17 at 20:38
  • Post the code of your go file – Tarun Lalwani Sep 12 '17 at 20:42
  • Here is my code https://github.com/gempir/gempbotgo/tree/docker note the Makefile I'm compiling differently – gempir Sep 13 '17 at 05:00
  • Now I realize the static linking while compiling is not needed. I used to do it to get my app running on scratch. Guess I need to try compiling normally – gempir Sep 13 '17 at 05:05
  • You should compile also inside docker. See this https://docs.docker.com/engine/userguide/eng-image/multistage-build/ – Tarun Lalwani Sep 13 '17 at 06:47
  • @TarunLalwani Then I need all deps for Go though. Doesn't it make more sense to run a tiny container with the least amount of dependencies? – gempir Sep 13 '17 at 07:12
  • That is what happens in multi stage. You use one container with deps and build the binary. Then copy that binary into a new container with no dependencies needed. Read the article to the end and your will understand – Tarun Lalwani Sep 13 '17 at 07:21
  • @TarunLalwani I now have tried doing a multi-stage build but the result is exactly the same. I can't reach my container at 127.0.0.1:8025 – gempir Sep 13 '17 at 16:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154371/discussion-between-tarun-lalwani-and-gempir). – Tarun Lalwani Sep 13 '17 at 17:11

5 Answers5

108

Your issue is that you are binding to the 127.0.0.1:8025 inside your code. This makes the code work from inside the container but not outside.

You need to bind to 0.0.0.0:8025 to bind to all interfaces inside the container. So traffic coming from outside of the container is also accepted by your Go app

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • 8
    Thank you very much. This is exactly the solution for me – DriLLFreAK100 Oct 23 '19 at 18:08
  • 3
    wow, thanks, after pulling my hair out, this was the solution to my problem as well – Alex Weitz Jan 30 '20 at 08:00
  • If someone is using hugo, relevant link is this https://gohugo.io/commands/hugo_server/ – rafee Jan 30 '20 at 17:35
  • 1
    I was using "ng serve" for angular application, the app was not accessible on host machine. After changing command to "ng serve --host 0.0.0.0". It finally worked. Thanks @Tarun – Devashish Aug 03 '21 at 20:45
  • 3
    i was working with flask, and had the same problem, after hours of debugging haha, as you suggested i used ```app.run(host='0.0.0.0)``` – oubaydos Jul 19 '22 at 18:04
  • What can I do, when I can't change the binding in the container? So the binding has to stay ` 127.0.0.1` in the container. Is there any way to map successfully a port to the host? – zingi Sep 16 '22 at 13:22
  • Was struggling with a legacy app using gulp-webserver a similar solution worked for me, just adding `host: '0.0.0.0'` to the config object. +1 from me – TKharaishvili Mar 30 '23 at 14:39
8

Adding to the accepted answer: I had the same error message trying to run docker/getting-started.

The problem was that "getting-started" is using port 80 and this was "occupied" (netsh http show urlacl) on my machine.

I had to use docker run -d -p 8888:80 docker/getting-started where 8888 was an unused port. And then open "http://localhost:8888/tutorial/".

janw
  • 8,758
  • 11
  • 40
  • 62
Fritz Emboli
  • 81
  • 1
  • 1
2

I have the same problem using Dockerize GatsbyJS. As Tarun Lalwani's comment above, I resolved the problem by binding or using 0.0.0.0 as hostname

yarn develop -P 0.0.0.0 -p 8000
sareno
  • 576
  • 8
  • 10
  • 1
    Thank you Dev Sareno!! Note: with the new Gatsby version you should use: `yarn develop -P 0.0.0.0 -p 8000` or `gatsby develop -H 0.0.0.0 -p 8000` – Matteo Cocon Mar 03 '22 at 08:40
0

For me this was a problem with the docker swarm mode ingress network. I had to recreate it. https://docs.docker.com/network/overlay/#customize-the-default-ingress-network

Clintm
  • 4,505
  • 3
  • 41
  • 54
-4

Another possibility why you are getting that error is that the docker run command is run through a normal cmd prompt and not the admin command prompt. Make sure you run as an admin!