Docker 在容器里听,在容器外不接听,为什么?

Docker listening in container but not answering outside, why?

我有一个 docker 容器,其指令中包含 "EXPOSE 8000"。我是这样开始这个过程的:

sudo docker run -t -i -P imagename

容器中的进程正在侦听 8000。

# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 localhost:8000          *:*                     LISTEN     
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
# 

在主机上(即在容器外),我看到端口 49164 绑定到容器端口 8000:

[S-22]jeff@siegfried:~ $ sudo docker ps 
CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS                     NAMES
0b0e333c6ec9        lkz:latest                 "/bin/bash"         About an hour ago   Up 6 minutes        0.0.0.0:49164->8000/tcp   lkxyz__2015-01-18_202737   
[S-22]jeff@siegfried:~ $ 

Inded,docker inspect 说(除其他外)

"NetworkSettings": {
    "Bridge": "docker0",
    "Gateway": "172.17.42.1",
    "IPAddress": "172.17.0.16",
    "IPPrefixLen": 16,
    "PortMapping": null,
    "Ports": {
        "8000/tcp": [
            {
                "HostIp": "0.0.0.0",
                "HostPort": "49164"
            }
        ]
    }
},

然而,我无法与容器对话。外面,

[S-22]jeff@siegfried:~ $ telnet localhost 49164
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
1,[S-22] jeff@siegfried:~ $ 

在里面时,

# telnet localhost 8000
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /

[18/Jan/2015 23:00:59] "GET /" 200 4066
<!DOCTYPE html>
<html>
<head>
...

我希望在外部通过 telnet 到 49164 到 return html,就像在内部一样。

有什么建议吗?

您可能希望在容器中使用您 运行 的服务来监听 0.0.0.0 而不是 127.0.0.1

尝试从主机连接 172.17.0.16:8000。 我对 nginx 也有类似的问题。 我将容器内的 127.0.0.1:8000 替换为 172.17.0.16:8000,它对我有用。

将容器的8000端口绑定到TCP的8000端口

docker run -d -p 8000:8000 image-name