Docker Compose 端口未暴露?

evo*_*ise 4 python cherrypy docker docker-compose

我有一个简单的基于 Python 的 Web 服务器,在使用 docker compose 设置的容器中运行,该容器公开端口 8080。

\n

当我 docker-compose up 服务时,它报告端口已公开,但端口未向外部公开。

\n

接下来我应该看什么诊断?

\n

这是一个典型的运行

\n
\xe2\x9e\x9c  demo04 sudo docker-compose up -d\nRecreating leagueweb_database ... done\nRecreating leagueweb_server   ... done\n
Run Code Online (Sandbox Code Playgroud)\n

Python Web 服务器(使用 CherryPy)报告它已正常启动并打开端口 8080。

\n
leagueweb_server | [25/Jan/2022:11:27:21] ENGINE Serving on http://127.0.0.1:8080\n
Run Code Online (Sandbox Code Playgroud)\n

Docker 报告它正在转发端口 8080

\n
\xe2\x9e\x9c  demo04 sudo docker-compose ps\n       Name                     Command                  State                              Ports\n------------------------------------------------------------------------------------------------------------------------\nleagueweb_database   /entrypoint.sh mysqld            Up (healthy)   0.0.0.0:3306->3306/tcp,:::3306->3306/tcp, 33060/tcp\nleagueweb_server     ./wait-for-it.sh database: ...   Up             0.0.0.0:8080->8080/tcp,:::8080->8080/tcp\n
Run Code Online (Sandbox Code Playgroud)\n

从远程 PC 进行测试,我可以看到虽然端口 3306 是外部开放的,但端口 8080 却没有。

\n
PS C:> Test-NetConnection 192.168.1.132 -Port 3306\nRemoteAddress    : 192.168.1.132\nRemotePort       : 3306\nTcpTestSucceeded : True\n\nPS C:> Test-NetConnection 192.168.1.132 -Port 8080\nWARNING: TCP connect to (192.168.1.132 : 8080) failed\n
Run Code Online (Sandbox Code Playgroud)\n

防火墙已关闭

\n
\xe2\x9e\x9c  demo04 sudo ufw status\nStatus: inactive\n
Run Code Online (Sandbox Code Playgroud)\n

这是 docker compose 文件

\n
version: '3'\nservices:\n\n    leagueweb:\n        # Install Python and required libraries with a Dockerfile\n        build:\n            context: .\n            dockerfile: leagueweb.dockerfile\n            \n        restart: unless-stopped\n        \n        container_name: leagueweb_server\n        \n        # Don't start up until the database is started\n        depends_on:\n            - database\n        \n        # Expose HTTP port \n        ports:\n            - "8080:8080"\n\n        # Mount the leagueweb code and resources\n        volumes:\n            - "/home/testuser/demo04/code:/leagueweb"\n \n        # Start the server only once the database is running\n        command: ["./wait-for-it.sh", "database:3306", "--", "python", "-u", "/leagueweb/leagueweb.py"]\n        \n\n    database:\n        # Use MySQL 5.7 as the database\n        image: mysql/mysql-server:5.7\n        \n        restart: unless-stopped\n        \n        container_name: leagueweb_database\n\n        # Set environment variables to set initial database set-up\n        environment:\n            - "MYSQL_ROOT_PASSWORD=root"\n            - "MYSQL_USER=leagueweb"\n            - "MYSQL_PASSWORD=********"\n            - "MYSQL_DATABASE=leagueweb01"\n            \n        # Uncomment to expose the MySQL database externally on port 3306 for \n        # testing \n        ports:\n            - "3306:3306"\n\n        # Mount init.sql file to automatically run and create tables for us.\n        # everything in docker-entrypoint-initdb.d folder is executed as \n        # soon as container is up and running.\n        volumes:\n            - "/home/testuser/demo04/db:/docker-entrypoint-initdb.d"\n
Run Code Online (Sandbox Code Playgroud)\n

she*_*n89 6

这看起来像一个问题:

ENGINE Serving on http://127.0.0.1:8080
Run Code Online (Sandbox Code Playgroud)

看起来你需要重新配置你的 python 服务器来监听0.0.0.0,而不是127.0.0.1