简体   繁体   English

无法通过IP地址访问Docker容器

[英]Can't acces docker container by IP address

I am working on a django app, and I decided to move all my dev into docker containers. 我正在开发Django应用程序,因此我决定将所有开发人员移至Docker容器中。 I have a small problem: Basically, I have 3 containers: - app (which holds the django code) - postgres (which holds the database) - web (which is an nginx that forwards requests coming on port 80 to 8000 which is the port used by django) My problem is that I cannot access my website from outside the "app" container, my nginx logs say: 我有一个小问题:基本上,我有3个容器:-应用程序(保存django代码)-postgres(保存数据库)-网络(网络是nginx,将来自端口80的请求转发到端口8000我的问题是我无法从“ app”容器外部访问我的网站,我的nginx日志说:

2014/12/21 22:22:35 [error] 9#0: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.59.3, server: _, request: "GET / HTTP/1.1", upstream: "http://172.17.0.62:8000/", host: "192.168.59.103"

So apparently, nginx cannot forward the connexions because the "app" container (192.168.59.103) does not listen for connexions. 因此很明显,nginx无法转发连接,因为“ app”容器(192.168.59.103)不监听连接。 But that's not true, because I attached to the app container and I tried to make a curl request, but: - If I do curl "localhost:8000" , it works (I get the django Welcome page - If I do curl "http://172.17.0.62:8000/" It does not work So, I managed to isolate the problem and run "route" command that shows me: 但这不是真的,因为我连接到应用程序容器,并尝试发出curl请求,但是:-如果我curl "localhost:8000" ,它就可以工作(我得到了django的欢迎页面-如果我curl "http://172.17.0.62:8000/"它不起作用因此,我curl "http://172.17.0.62:8000/"出问题并运行向我显示的” route“命令:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         172.17.42.1     0.0.0.0         UG    0      0        0 eth0
172.17.0.0      *               255.255.0.0     U     0      0        0 eth0

Them, I went on the boot2docker ssh and typed "route" and this is what came out: 他们,我进入boot2docker ssh并输入“ route”,结果如下:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         10.0.2.2        0.0.0.0         UG    1      0        0 eth0
10.0.2.0        *               255.255.255.0   U     0      0        0 eth0
127.0.0.1       *               255.255.255.255 UH    0      0        0 lo
172.17.0.0      *               255.255.0.0     U     0      0        0 docker0
192.168.59.0    *               255.255.255.0   U     0      0        0 eth1

So apparently I don't have the good routes to my containers, but that is odd, because I didn't touch any of its configs. 因此,显然我没有通往容器的良好路线,但这很奇怪,因为我没有碰到它的任何配置。 Anybody has a clue? 有人知道吗?

A few comments: 一些评论:

web (which is an nginx that forwards requests coming on port 80 to 8000 which is the port used by django) Web(这是一个nginx,它将来自端口80的请求转发到8000,这是django使用的端口)

If you are using nginx just to forward from 80 to 8080 why not run your app container with -p 80:8080 which have the same effect without an intermediate container. 如果您仅使用nginx从80转发到8080,为什么不使用-p 80:8080运行您的应用容器,而无需中间容器就可以达到相同的效果。

If you want nginx to be able to able to talk to the app container the app container must be linked to the nginx container using the --link APP_CONTAINER:ALIAS switch when running docker run for nginx. 如果您希望nginx能够与app容器通信,则在为nginx运行--link APP_CONTAINER:ALIAS时,必须使用--link APP_CONTAINER:ALIAS开关将app容器链接到nginx容器。 Then you can look at /etc/hosts inside the nginx container for the IP at which ALIAS is mapped. 然后,您可以查看nginx容器内的/ etc / hosts以获取ALIAS映射到的IP。 Each docker container gets its own IP from the docker bridge and needs to be linked for communication to be possible. 每个docker容器都从docker桥获取自己的IP,并且需要链接才能进行通信。

Docker inter-container communication is managed using iptables. Docker容器间通信使用iptables进行管理。 run iptables -L to take a look at the routes configured. 运行iptables -L来查看配置的路由。 You should probably not mess with this manually. 您可能不应该手动将此操作弄乱。 Linking containers normally manages these things fairly well. 链接容器通常可以很好地管理这些事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM