簡體   English   中英

無法通過端口映射從外部主機訪問 docker 容器

[英]cant reach docker container from outside host with port mapping

我在這里使用了一個非常簡單的 docker-compose 文件:

https://github.com/brandonserna/flask-docker-compose

這是 docker 撰寫文件:

version: '3.5'

services:
  flask-app-service:
    build: ./app
    volumes:
      - ./app:/usr/src/app
      - .:/user/src
    ports:
      - 5555:9999

但是,當我使用端口 80 時,我只能從外部網絡訪問應用程序。

    ports:
      - 80:9999

例如,當我使用端口 8000 時。我無法從外部網絡訪問容器。 從本地機器我可以訪問應用程序。 (使用 wget localhost:8000 測試)

    ports:
      - 8000:9999

iptables -L 給了我這個:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
DOCKER-USER  all  --  anywhere             anywhere            
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain DOCKER (2 references)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             172.18.0.2           tcp dpt:9999
ACCEPT     tcp  --  anywhere             172.17.0.2           tcp dpt:http

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination         
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere            
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere            
RETURN     all  --  anywhere             anywhere            

Chain DOCKER-ISOLATION-STAGE-2 (2 references)
target     prot opt source               destination         
DROP       all  --  anywhere             anywhere            
DROP       all  --  anywhere             anywhere            
RETURN     all  --  anywhere             anywhere            

Chain DOCKER-USER (1 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere  

評論還不夠,所以這就是原因:從看起來它可能是運行容器的主機中的防火牆規則或主機到您家之間的防火牆規則。 為了測試兩者之間的哪一個,我嘗試使用帶有 --reason 和 --tracerout 選項的 nmap,因為我們在另一個端口有連接,所以你的家和容器之間不太可能有一個完整的塊,所以 traceroute 不會t 提供太多信息,但以防萬一。 此外,如果您對主機或僅對 iptables 服務具有 root 訪問權限,請嘗試停止它以檢查這是否是阻止的根本原因。

如果端口綁定到機器上的端口,還要檢查 docker ps,應該如下所示:

0.0.0.0: 端口 --> tcp\端口

你有端口號而不是端口

如果不是,則可能是由於 docker-compose up 命令存在問題,因此請嘗試使用簡單的 docker 運行命令運行該服務

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM