来自 docker 容器的 ERR_EMPTY_RESPONSE [英] ERR_EMPTY_RESPONSE from docker container

查看:45
本文介绍了来自 docker 容器的 ERR_EMPTY_RESPONSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几个小时我一直在努力解决这个问题,但我卡住了.

我有一个非常简单的 Dockerfile,如下所示:

From alpine:3.6复制gempbotgo/复制配置/配置CMD ["/gempbotgo"]曝光 8025

gempbotgo 只是一个运行网络服务器和其他一些东西的 go 二进制文件.网络服务器在 8025 上运行,应该回复一个 hello world.

我的问题是暴露端口.我像这样运行我的容器(构建后)

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

一切似乎都很好,但是当我尝试在浏览器中打开 127.0.0.1:8025 或尝试 wget 时,我只会得到一个空响应.铬: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 在那里检查会发生什么.也没有问题.index.html 文件下载时带有Hello World"

知道为什么 docker 不发送任何数据吗?我也用 docker-compose 运行了我的容器,但没有区别.

我还在外部托管的 VPS 上运行了容器.同样的问题......(Debian)

我的代码:(注意Makefile)https://github.com/gempir/gempbotgo/tree/docker

在收到一些评论后,我将我的 Dockerfile 更改为多阶段构建.这是我的 Dockerfile 现在:

FROM golang:latest工作目录/go/src/github.com/gempir/gempbotgo运行去获取 github.com/gempir/go-twitch-irc &&去 github.com/stretchr/testify/assert &&去 github.com/labstack/echo &&去 github.com/op/go-logging复制 ..RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .来自高山:最新RUN apk --no-cache add ca-certificates工作目录/root/复制配置 ./configs复制 --from=0/go/src/github.com/gempir/gempbotgo/app .CMD ["./app"]曝光 8025

遗憾的是,这并没有改变任何东西,我将所有内容尽可能地与此处的指南保持一致:https://docs.docker.com/engine/userguide/eng-image/multistage-build/#use-multi-stage-builds>

我也尝试过 golang.org 中的极简 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 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.

解决方案

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_EMPTY_RESPONSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆