一尘不染

Docker在容器中侦听但不在外部应答,为什么?

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像在内部一样返回html。

有什么建议么?


阅读 325

收藏
2020-06-17

共1个答案

一尘不染

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

2020-06-17