无法连接到应用程序正在侦听的docker容器端口 [英] Can't connect to a docker container port where an app is listening
问题描述
我想运行Jenkins,但为了演示这个问题,我在Ubuntu 15.10中运行一个netcat服务器容器:
Docker版本1.6.2,构建7c8fca2
这是我的Dockerfile:
FROM ubuntu
CMD while true; do echo'HTTP / 1.1 200 OK\r\\\
\r\\\
Hello world'| nc -l 8080;完成
当在主机上运行时,此单行将返回HTTP 200响应。
现在,在容器上。 / code> docker build -t mydoc。 #code> docker run -it --expose 8080 -p 8080:8080 mydoc 。在另一个终端,试图卷曲: curl -D - -o - http://127.0.0.1:8080
。
它只是阻止没有回应。
一些可能的相关信息:
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1acc13bb3384 mydoc:最新的/ bin / sh -c,而t 48秒以前Up 47秒0.0.0.0:8080 - > 8080 / tcp goofy_darwin
和
> netstat -ant | grep 8080
tcp 0 1 10.0.2.15:34512 172.17.0.25:8080 SYN_SENT
tcp 0 0 127.0.0.1:33276 127.0 .0.1:8080 ESTABLISHED
tcp6 0 0 ::: 8080 ::: * LISTEN
tcp6 78 0 127.0.0.1:8080 127.0.0.1:33276 ESTABLISHED
注意SYN_SENT。
Linux服务状态显示为:
●docker.service - Docker应用程序容器引擎
加载:已加载(/lib/systemd/system/docker.service; enabled;供应商预设:启用)
活动:活动(运行)从星期一至星期三13:39:44 CST; 2min 24s ago
文件:http://docs.docker.com
主PID:672(docker)
内存:11.2M
CPU:62ms
CGroup: /system.slice/docker.service
└─672/ usr / bin / docker -d -H fd://
这解决了这个问题:
service docker restart
我不知道为什么。现在服务响应:
> curl -D - http://127.0.0.1:8080
HTTP / 1.1 200 OK
Hello world
此外,选项 - 公开8080
不需要这个工作。
编辑:
获得1.6.2的工作后,升级到1.10.2,还没有看到这个问题。我怀疑主机启动时有某种竞争情况,但不能肯定。也许docker服务脚本需要一个
Required-Start:$ network
或某些东西。 I want to run Jenkins, but to demonstrate the problem, I'm running a netcat server container in Ubuntu 15.10:
Docker version 1.6.2, build 7c8fca2
Here's my Dockerfile:
FROM ubuntu
CMD while true; do echo 'HTTP/1.1 200 OK\r\n\r\nHello world' | nc -l 8080; done
When run on the host, this one-liner will return HTTP 200 responses.
Now, on the container. Ran docker build -t mydoc .
. Ran docker run -it --expose 8080 -p 8080:8080 mydoc
. In another terminal, tried to curl it: curl -D - -o - http://127.0.0.1:8080
.
It just blocks with no response.
Some possibly relevant info:
>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1acc13bb3384 mydoc:latest "/bin/sh -c 'while t 48 seconds ago Up 47 seconds 0.0.0.0:8080->8080/tcp goofy_darwin
and
>netstat -ant | grep 8080
tcp 0 1 10.0.2.15:34512 172.17.0.25:8080 SYN_SENT
tcp 0 0 127.0.0.1:33276 127.0.0.1:8080 ESTABLISHED
tcp6 0 0 :::8080 :::* LISTEN
tcp6 78 0 127.0.0.1:8080 127.0.0.1:33276 ESTABLISHED
Note the SYN_SENT. That's as far as it gets.
The Linux service status showed as:
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2016-03-07 13:39:44 CST; 2min 24s ago
Docs: http://docs.docker.com
Main PID: 672 (docker)
Memory: 11.2M
CPU: 62ms
CGroup: /system.slice/docker.service
└─672 /usr/bin/docker -d -H fd://
This fixed the issue:
service docker restart
I don't know why. Now the service responds:
>curl -D - http://127.0.0.1:8080
HTTP/1.1 200 OK
Hello world
Also, the option --expose 8080
was not necessary for this to work.
EDIT:
After getting 1.6.2 to work, I upgraded to 1.10.2 and haven't seen this problem yet. I suspect some kind of race condition on host startup, but can't say for sure. Maybe the docker service script needs a Required-Start: $network
or something.
这篇关于无法连接到应用程序正在侦听的docker容器端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!