Docker

Docker Compose 埠未暴露?

  • January 25, 2022

我有一個簡單的基於 Python 的 Web 伺服器,它執行在一個使用 docker compose 設置的容器中,該容器公開了 8080 埠。

當我 docker-compose up 服務時,它報告埠已公開,但埠未對外公開。

接下來我應該看什麼診斷?

這是一個典型的執行

➜  demo04 sudo docker-compose up -d
Recreating leagueweb_database ... done
Recreating leagueweb_server   ... done

Python Web 伺服器(使用 CherryPy)報告它已啟動正常並打開埠 8080。

leagueweb_server | [25/Jan/2022:11:27:21] ENGINE Serving on http://127.0.0.1:8080

Docker 報告它正在轉發埠 8080

➜  demo04 sudo docker-compose ps
      Name                     Command                  State                              Ports
------------------------------------------------------------------------------------------------------------------------
leagueweb_database   /entrypoint.sh mysqld            Up (healthy)   0.0.0.0:3306->3306/tcp,:::3306->3306/tcp, 33060/tcp
leagueweb_server     ./wait-for-it.sh database: ...   Up             0.0.0.0:8080->8080/tcp,:::8080->8080/tcp

從遠端 PC 測試這個我可以看到,雖然埠 3306 是外部開放的 - 埠 8080 不是。

PS C:> Test-NetConnection 192.168.1.132 -Port 3306
RemoteAddress    : 192.168.1.132
RemotePort       : 3306
TcpTestSucceeded : True

PS C:> Test-NetConnection 192.168.1.132 -Port 8080
WARNING: TCP connect to (192.168.1.132 : 8080) failed

防火牆已關閉

➜  demo04 sudo ufw status
Status: inactive

這是碼頭工人撰寫文件

version: '3'
services:

   leagueweb:
       # Install Python and required libraries with a Dockerfile
       build:
           context: .
           dockerfile: leagueweb.dockerfile
           
       restart: unless-stopped
       
       container_name: leagueweb_server
       
       # Don't start up until the database is started
       depends_on:
           - database
       
       # Expose HTTP port 
       ports:
           - "8080:8080"

       # Mount the leagueweb code and resources
       volumes:
           - "/home/testuser/demo04/code:/leagueweb"

       # Start the server only once the database is running
       command: ["./wait-for-it.sh", "database:3306", "--", "python", "-u", "/leagueweb/leagueweb.py"]
       

   database:
       # Use MySQL 5.7 as the database
       image: mysql/mysql-server:5.7
       
       restart: unless-stopped
       
       container_name: leagueweb_database

       # Set environment variables to set initial database set-up
       environment:
           - "MYSQL_ROOT_PASSWORD=root"
           - "MYSQL_USER=leagueweb"
           - "MYSQL_PASSWORD=********"
           - "MYSQL_DATABASE=leagueweb01"
           
       # Uncomment to expose the MySQL database externally on port 3306 for 
       # testing 
       ports:
           - "3306:3306"

       # Mount init.sql file to automatically run and create tables for us.
       # everything in docker-entrypoint-initdb.d folder is executed as 
       # soon as container is up and running.
       volumes:
           - "/home/testuser/demo04/db:/docker-entrypoint-initdb.d"

這看起來像一個問題:

ENGINE Serving on http://127.0.0.1:8080

看起來你需要重新配置你的 python 伺服器來監聽0.0.0.0,而不是127.0.0.1

引用自:https://serverfault.com/questions/1091085