Docker 无法从主机localhost访问容器Web服务器

Docker 无法从主机localhost访问容器Web服务器,docker,containers,iptables,Docker,Containers,Iptables,在linux mint机器上使用Docker v 17.03.1-ce,我无法使用主机上的浏览器(localhost端口9000)访问容器web服务器(容器端口5000) 使用以下命令启动容器: sudo docker run -d -p 9000:5000 --name myContainer imageName 我首先检查容器上的服务器(flask)是否正确启动。发射了 我想检查服务器是否正常工作,所以在容器中,使用curl,我在localhost端口5000上发送了一个GET请求。服务

在linux mint机器上使用Docker v 17.03.1-ce,我无法使用主机上的浏览器(localhost端口9000)访问容器web服务器(容器端口5000)

使用以下命令启动容器:

sudo docker run -d -p 9000:5000 --name myContainer imageName
  • 我首先检查容器上的服务器(flask)是否正确启动。发射了
  • 我想检查服务器是否正常工作,所以在容器中,使用curl,我在localhost端口5000上发送了一个GET请求。服务器返回了该网页
因此,服务器正在工作,因此问题在于容器和主机之间的通信

我检查了iptables,但不确定如何使用它:

sudo iptables-L

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy DROP)
target     prot opt source               destination         
DOCKER-ISOLATION  all  --  anywhere             anywhere            
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
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.17.0.2           tcp dpt:5000

Chain DOCKER-ISOLATION (1 references)
target     prot opt source               destination         
DROP       all  --  anywhere             anywhere            
DROP       all  --  anywhere             anywhere            
RETURN     all  --  anywhere             anywhere 
sudo iptables-t nat-L-n

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DOCKER     all  --  0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
DOCKER     all  --  0.0.0.0/0           !127.0.0.0/8          ADDRTYPE match dst-type LOCAL

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
MASQUERADE  all  --  172.17.0.0/16        0.0.0.0/0           
MASQUERADE  all  --  172.18.0.0/16        0.0.0.0/0           
MASQUERADE  tcp  --  172.17.0.2           172.17.0.2           tcp dpt:5000

Chain DOCKER (2 references)
target     prot opt source               destination         
RETURN     all  --  0.0.0.0/0            0.0.0.0/0           
RETURN     all  --  0.0.0.0/0            0.0.0.0/0           
DNAT       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:9000 to:172.17.0.2:5000
预期结果:使用我的浏览器,使用url“localhost:9000”,我可以通过端口5000接收从容器发送的主页

编辑:添加docker日志和docker p

docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
59a20248c5b2        apptest             "python3 src/jboos..."   12 hours ago        Up 12 hours         0.0.0.0:9000->5000/tcp   jboost
sudo docker日志jboost

 * Serving Flask app "jboost_app" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 310-292-856
127.0.0.1 - - [03/Jul/2019 04:12:54] "GET / HTTP/1.1" 200 -
编辑2:在主机上添加curl localhost:9000的结果 因此,当连接到我的web浏览器时,连接不起作用,但curl给出了一个更具体的消息:

curl localhost:9000
curl: (56) Recv failure: Connection reset by peer

我在这篇文章中找到了解决方案:

Docker网络和端口转发工作正常。问题出在我的flask服务器上。似乎默认情况下,服务器配置为仅接受来自本地主机的请求

启动闪存服务器时,使用“run”命令,必须指定host='0.0.0.0',以便为任何ip提供服务

if __name__ == "__main__":
    app.run(host='0.0.0.0')

你试过卷曲吗http://localhost:9000在主机上?还可以尝试使用您的IP地址。docker logs,docker ps?@Ntwobike,日志和docker ps已添加。@PrakashKrishna,curl显示:curl:(56)Recv故障:对等方重置连接。@Prasanth,谢谢。我发现了问题。在我的容器中,默认情况下,flask服务器只接受本地主机的请求。我需要通过指定app.run(host='0.0.0.0')来指定它应该回答来自任何ip的查询。