Curl Docker容器可以';无法使用主机名访问另一个容器

Curl Docker容器可以';无法使用主机名访问另一个容器,curl,dns,docker,lxc,lxc-docker,Curl,Dns,Docker,Lxc,Lxc Docker,我有两个容器: docker run -d -p 9000:9000 --name="my-php" php-fpm:5.4 docker run -d --name="nginx" -p 80:80 -link my-php:web nginx 两人都跑得很好。在nginx容器中,我可以使用 curl http://myapp.dev 也可以从主机上下载 我试图做的是使用相同的“myapp.dev”主机名从MyPHP容器发出相同的curl请求,hower curl:(7)无法连接到mya

我有两个容器:

docker run -d -p 9000:9000 --name="my-php" php-fpm:5.4

docker run -d --name="nginx" -p 80:80 -link my-php:web nginx
两人都跑得很好。在nginx容器中,我可以使用

curl http://myapp.dev
也可以从主机上下载

我试图做的是使用相同的“myapp.dev”主机名从MyPHP容器发出相同的curl请求,hower

curl:(7)无法连接到myapp.dev端口80:连接被拒绝

我可以使用容器的ip,但它不适合我

即使我不能使用curl,我仍然可以ping另一个容器,没有问题

在nginx容器上:

root@6252bfe9d545:/# env
WEB_PORT_9000_TCP_PROTO=tcp
HOSTNAME=6252bfe9d545
WEB_PORT_9000_TCP_ADDR=172.17.0.24
WEB_PORT_9000_TCP_PORT=9000
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
NGINX_VERSION=1.7.9-1~wheezy
SHLVL=1
HOME=/root
WEB_NAME=/nginx/web
WEB_PORT_9000_TCP=tcp://172.17.0.24:9000
WEB_PORT=tcp://172.17.0.24:9000
_=/usr/bin/env

root@6252bfe9d545:/# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.17.42.1     0.0.0.0         UG    0      0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0
关于php容器

root@c7c521c4a40c:/# env
MYSQLDB_PORT_3306_TCP=tcp://172.17.0.22:3306
HOSTNAME=c7c521c4a40c
MYSQLDB_NAME=/php/mysqldb
MEMCACHEDB_ENV_MEMCACHED_VERSION=1.4.22
MYSQLDB_PORT=tcp://172.17.0.22:3306
MEMCACHEDB_PORT=tcp://172.17.0.23:11211
MEMCACHEDB_NAME=/php/memcachedb
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MYSQLDB_PORT_3306_TCP_PROTO=tcp
MEMCACHEDB_ENV_MEMCACHED_SHA1=5968d357d504a1f52622f9f8a3e85c29558acaa5
PWD=/
MEMCACHEDB_PORT_11211_TCP=tcp://172.17.0.23:11211
MEMCACHEDB_PORT_11211_TCP_PORT=11211
SHLVL=1
HOME=/root
MYSQLDB_PORT_3306_TCP_ADDR=172.17.0.22
MEMCACHEDB_PORT_11211_TCP_PROTO=tcp
MYSQLDB_PORT_3306_TCP_PORT=3306
MEMCACHEDB_PORT_11211_TCP_ADDR=172.17.0.23
_=/usr/bin/env

(here it's showing links to mysql and memchache which I hide on run command before paste on stackoverflow)

ping myapp.dev
PING myapp.dev (127.0.53.53) 56(84) bytes of data.
64 bytes from 127.0.53.53: icmp_seq=1 ttl=64 time=0.025 ms

root@c7c521c4a40c:/# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.17.42.1     0.0.0.0         UG    0      0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0
nginx上的myapp.conf

server {
        listen          80;
        server_name     myapp.dev;
        root            /projects/MyApp/public;
        index           index.html index.htm index.php;

        fastcgi_connect_timeout 5000s;
        fastcgi_read_timeout 5000s;
        fastcgi_send_timeout 5000s;

        access_log /var/log/nginx-access.log;
        error_log /var/log/nginx-error.log;
        location / {
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                fastcgi_pass    web:9000;
                fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include         fastcgi_params;
            fastcgi_param   APPLICATION_ENV dev;
        }
}

有人有任何线索吗?

将IP添加到my php容器中的/etc/hosts文件中:

root@6252bfe9d545:/# env
WEB_PORT_9000_TCP_PROTO=tcp
HOSTNAME=6252bfe9d545
WEB_PORT_9000_TCP_ADDR=172.17.0.24
WEB_PORT_9000_TCP_PORT=9000
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
NGINX_VERSION=1.7.9-1~wheezy
SHLVL=1
HOME=/root
WEB_NAME=/nginx/web
WEB_PORT_9000_TCP=tcp://172.17.0.24:9000
WEB_PORT=tcp://172.17.0.24:9000
_=/usr/bin/env

root@6252bfe9d545:/# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.17.42.1     0.0.0.0         UG    0      0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0
(nginx容器的ip)myapp.dev

应该会得到你需要的结果

例如:


111.111.111.111 foo.bar.com

您能提供您的nginx配置吗?使用
docker exec-it
进入两个容器,并使用常用的netstat、ping、route、lsof-i:80进行调试(您可能需要安装其中一些),同时检查你的容器的
docker日志
,看看
docker事件
有nginx.conf和route-n outputOk,它们都有效,但是我在这个解决方案中遇到了一些问题,因为我无法自动运行我的应用程序,我将始终必须用nginx容器的ip地址更新/etc/hosts。尽管可以使用一些技巧在Docker上编辑主机,但我认为这可能不是正确的解决方案