Docker 如何使停靠的Python应用程序在两个单独的端口上输出?

Docker 如何使停靠的Python应用程序在两个单独的端口上输出?,docker,curl,port,Docker,Curl,Port,我有一个停靠的Python应用程序,它在端口8080和端口8081上输出数据。 我正在Ubuntu系统上运行代码 $ docker version | grep Version Version: 18.03.1-ce 应用程序在端口8080上响应 $ curl -k localhost:8080 | tail -4 --> TYPE hello_world_total counter hello_world_total 3.0 TYPE hello_world_created

我有一个停靠的Python应用程序,它在端口8080和端口8081上输出数据。 我正在Ubuntu系统上运行代码

$ docker version | grep Version
 Version:      18.03.1-ce
应用程序在端口8080上响应

$ curl -k localhost:8080 | tail -4
-->
TYPE hello_world_total counter
hello_world_total 3.0
TYPE hello_world_created gauge
hello_world_created 1.5617357381235116e+09
应用程序在端口8081上返回错误

$ curl -k localhost:8081
curl: (56) Recv failure: Connection reset by peer
虽然我不熟悉netstat,但我使用它检查端口8080和8081是否都处于侦听状态

root@1d1ac2974893:/# netstat -apn
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      1/python3
tcp        0      0 127.0.0.1:8081          0.0.0.0:*               LISTEN      1/python3
tcp        0      0 172.17.0.2:58220        16.46.41.11:8080        TIME_WAIT   -
tcp        0      0 172.17.0.2:58218        16.46.41.11:8080        TIME_WAIT   -
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
root@1d1ac2974893:/#
我的Dockerfile如下所示

$ cat Dockerfile
FROM python:3
RUN pip3 install prometheus_client
COPY sampleapp.py /src/sampleapp.py
EXPOSE 8081
CMD [ "python3", "/src/sampleapp.py" ]
$ docker run -p 8081:8081 -p 8080:8080 sampleapp 
运行应用程序时,我将Docker容器中的8080和8081端口映射到主机上的相同端口,如下所示

$ cat Dockerfile
FROM python:3
RUN pip3 install prometheus_client
COPY sampleapp.py /src/sampleapp.py
EXPOSE 8081
CMD [ "python3", "/src/sampleapp.py" ]
$ docker run -p 8081:8081 -p 8080:8080 sampleapp 
如果我进入容器并重复上面的curl命令,它们就会像我预期的那样工作

root@1d1ac2974893:/# curl -k localhost:8081 | tail -4
TYPE hello_world_total counter
hello_world_total 3.0
TYPE hello_world_created gauge
hello_world_created 1.5617357381235116e+09
root@1d1ac2974893:/#

所以 问题是为什么后一个curl命令在主机系统中不起作用

$ curl -k localhost:8081
curl: (56) Recv failure: Connection reset by peer

解决办法如下

  • 公开Dockerfile中的两个端口
    
    $grep公开Dockerfile
    曝光8080
    曝光8081
    

  • 使用0.0.0.0而不是127.0.0.1
    
    导入http.server
    从prometheus_客户端导入启动_http_服务器
    来自普罗米修斯客户导入柜台

    HOST='0.0.0.0' 您好\u WORLD\u PORT=8080 您好\u WORLD\u METRICS\u PORT=8081 请求=计数器('hello\u world\u总计','hello world Requested')

    类MyHandler(http.server.BaseHTTPRequestHandler): def do_获得(自我): 请求公司() 自我发送_响应(200) self.end_头() self.wfile.write(b“Hello World\n”)

    如果name==“main”: 启动\u http\u服务器(HELLO\u WORLD\u METRICS\u端口) server=http.server.HTTPServer((主机,HELLO\u WORLD\u端口),MyHandler) 服务器。永远为您服务()

  • Contaainer现在提供从主机运行时的预期结果
    
    $curl-k本地主机:8080
    你好,世界
    $ 
    $curl-k localhost:8081 | tail-4
    ...
    #键入hello\u world\u总计数器
    您好\u世界\u总计1.0
    #键入hello\u world\u创建的仪表
    您好世界创建1.5619773258069074e+09
    $
    

    外部参照:- 有关类似问题的详细信息