在过去的几个小时里,我一直在试图解决这个问题,但是我被困住了.
我有一个非常简单的Dockerfile,如下所示:
FROM高山:3.6COPY gempbotgo/COPY配置/configsCMD ["/gempbotgo"]展览8025gempbotgo只是运行Web服务器和其他一些东西的go二进制文件.该网络服务器运行在8025上,应该可以打个招呼.
我的问题是暴露端口.我这样运行容器(构建后)
docker run --rm -it -p 8025:8025 asd一切似乎都很好,但是当我尝试在浏览器中打开127.0.0.1:8025或尝试使用wget时,我只会得到一个空响应.Chrome:ERR_EMPTY_RESPONSE
该端口已使用,但不受Windows 10系统上防火墙的限制.仅在我的"Windows上的Ubuntu上的Bash"终端上运行不带容器的go二进制文件,然后浏览至127.0.0.1:8025即可顺利进行.其他地址返回的是"ERR_CONNECTION_REFUSED",例如127.0.0.1:8030,因此该端口上肯定有活动的东西.
然后我和
一起进入化妆器 docker exec -it e1cc6daae4cf/bin/sh并使用wget进行检查.也没有问题.使用"Hello World"下载了index.html文件
任何想法为何Docker不发送任何数据?我也用docker-compose运行了我的容器,但是在那里没有区别.
我还在外部托管的VPS上运行了该容器.那里同样的问题...(Debian)
我的代码:(请注意Makefile) github/gempir/gempbotgo/tree/docker
在收到一些评论后,我将Dockerfile更改为多阶段构建.现在这是我的Dockerfile:
FROM golang:latestWORKDIR/go/src/github/gempir/gempbotgo运行去获取github/gempir/go-twitch-irc \&&去github/stretchr/testify/assert \&&去github/labstack/echo \&&去获取github/op/go-logging复制 ..RUN CGO_ENABLED = 0 GOOS = linux转到build -a -installsuffix cgo -o app.来自高山:最新运行apk --no-cache添加ca证书WORKDIR/root/COPY配置./configs复制--from = 0/go/src/github/gempir/gempbotgo/app.CMD ["./app"]展览8025遗憾的是,这并没有改变任何内容,我将所有内容尽可能地保留在此处的指南中: docs.docker/engine/userguide/eng-image/multistage-build/#use-multi-stage-builds
我还尝试过golang提供的简约Dockerfile,如下所示:
从FROM golang:onbuild展览8025但那也没有成功.
解决方案您的问题是您绑定到代码内的 127.0.0.1:8025 .这样可以使代码从容器内部工作,而不是从外部工作.
您需要绑定到 0.0.0.0:8025 才能绑定到容器内的所有接口.因此,您的Go应用也可以接受来自容器外部的流量
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 8025gempbotgo 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 asdEverything 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/shand 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) github/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/gempir/gempbotgo RUN go get github/gempir/go-twitch-irc \ && go get github/stretchr/testify/assert \ && go get github/labstack/echo \ && go get github/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/gempir/gempbotgo/app . CMD ["./app"] EXPOSE 8025Sadly this did not change anything, I kept everything as close as possbile to the guide here: docs.docker/engine/userguide/eng-image/multistage-build/#use-multi-stage-builds
I have also tried the minimalist Dockerfile from golang which looks like this:
FROM golang:onbuild EXPOSE 8025But no success either with that.
解决方案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
更多推荐
来自Docker容器的ERR
发布评论