docker 容器端口访问不到,无法连接到应用程序正在侦听的docker容器端口

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,容器端口访问不到)