docker - 如何使我的dockerized Python应用程序在2个单独的端口上输出?

标签 docker curl port

我有一个经过 jetty 处理的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 

如果我进入容器并重复上述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

最佳答案

解决方法如下

  • 公开Dockerfile中的两个端口 $ grep EXPOSE Dockerfile EXPOSE 8080 EXPOSE 8081
  • 使用0.0.0.0而不是127.0.0.1 import http.server from prometheus_client import start_http_server from prometheus_client import Counter

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

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

    if name == "main": start_http_server(HELLO_WORLD_METRICS_PORT) server = http.server.HTTPServer((HOST, HELLO_WORLD_PORT), MyHandler) server.serve_forever()

  • 从主机运行时,Contaai​​ner现在可以提供预期的结果 $ 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
    有关类似问题的详细信息

    关于docker - 如何使我的dockerized Python应用程序在2个单独的端口上输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56810456/

    相关文章:

    elasticsearch - 如何使用主管在 docker 容器上运行 elasticsearch?

    javascript - 使用 JavaScript 启动 Docker 机器

    bash - 如何从 bash 脚本循环运行带参数的 curl 命令?

    php - 在 Ubuntu 上将 Google OAuth 与 PHP 集成时需要 CURL PHP 扩展时出错

    postgresql - 打开端口,以便 Windows 7 上的 pgAdmin 可以连接到 VirtualBox 上的 Debian 上的 PostgreSQL

    haskell - 如何使用stack镜像容器?

    mongodb - Docker mongo image 始终将数据保存在/data/db 目录中

    tomcat - 如何检测本地网络中哪些端口未被阻止

    php - 安装 Composer 时出错 - curl : (23) Failed writing body (0 ! = 16133)

    python - 如何检查网络端口是否打开?