【问题标题】:How can I get my dockerized Python app to output on 2 separate ports?如何让我的 dockerized Python 应用程序在 2 个单独的端口上输出?
【发布时间】:2019-06-28 16:38:35
【问题描述】:

我有一个 dockerized 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是否都处于LISTEN状态...

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 容器中的 8080 和 8081 端口都映射到主机上的相同端口,如下所示...

$ docker run -p 8081:8081 -p 8080:8080 sampleapp 

如果我进入 Container 并重复上述 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:/#

$ docker exec -it 1d1ac2974893 /bin/bash
root@1d1ac2974893:/# curl -k localhost:8081
Hello World

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

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

【问题讨论】:

    标签: docker curl port


    【解决方案1】:

    解决方法如下

    1. 在 Dockerfile 中公开两个端口 $ grep EXPOSE Dockerfile EXPOSE 8080 EXPOSE 8081

    2. 使用 0.0.0.0 而不是 127.0.0.1 import http.server from prometheus_client import start_http_server from prometheus_client import Counter

      主机='0.0.0.0' HELLO_WORLD_PORT=8080 HELLO_WORLD_METRICS_PORT=8081 REQUESTS = Counter('hello_world_total', 'Hello World Requested')

      类 MyHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): 请求.inc() self.send_response(200) self.end_headers() self.wfile.write(b"Hello World\n")

      如果 name == "ma​​in": start_http_server(HELLO_WORLD_METRICS_PORT) 服务器 = http.server.HTTPServer((HOST, HELLO_WORLD_PORT), MyHandler) server.serve_forever()

    容器现在在从主机运行时会给出预期的结果 $ curl -k localhost:8080 Hello World $ $ curl -k localhost:8081 | tail -4 ... # TYPE hello_world_total counter hello_world_total 1.0 # TYPE hello_world_created gauge hello_world_created 1.5619773258069074e+09 $

    外部参照:- Docker Rails app fails to be served - curl: (56) Recv failure: Connection reset by peer 有关类似问题的详细信息

    【讨论】:

      猜你喜欢
      • 2015-06-22
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多