docker 同一网络中的容器无法访问同一网络中的其他容器

docker container in same network cannot reach other container in same network

我在 digital ocean

上使用 ubuntu 15.10

以下作品 docker network create a

docker run -d --name=nginx --net=a nginx

docker run -it --net=a --name web node bash

apt-get install -yq curl && curl nginx

相反,试图从 nginx container 到达 web container,对我不起作用。

我进入网络容器: docker exec -it web bash

然后我添加我的 index.html 文件

然后我使用 http-server 通过命令 http-server ./ -p 4200 -a 0.0.0.0 index.html 提供 index.html 文件。

http-server returns:

Starting up http-server, serving ./
Available on:
  http:127.0.0.1:4200
  http:172.17.0.5:4200
Hit CTRL-C to stop the server

如果我然后进入 nginx 并尝试 curl web:4200 然后我得到 curl: (7) Failed to connect to web port 4200: Connection refused

现在是质疑网络隔离是否是使用容器的重要部分的好时机。

考虑通过 运行 您的容器与主机 OS 在同一网络上来避免此问题。

在现代 Linux 系统 运行 systemd 上,您可以访问 systemd-nspawn 容器解决方案,而无需安装任何其他软件。它提供进程隔离、资源管理、chroot 环境,以及使用 --network-veth 选项共享主机 OS 网络的能力。

在 DigitalOcean 上启动一个新的 Ubuntu 15.10 droplet 和 试图重现这个;

使用 quick n dirty curl | sh 安装方法 - 不是最佳实践,但很简单:

apt-get install -y curl && curl -fsSL https://get.docker.com | sh

在该网络上创建网络 mynetwork 和容器 webawebb

docker network create mynetwork
docker run --net mynetwork --name weba -d node sh -c 'npm install http-server -g && mkdir -p /public && echo "welcome to weba" > /public/index.html && http-server -a 0.0.0.0 -p 4200'
docker run --net mynetwork --name webb -d node sh -c 'npm install http-server -g && mkdir -p /public && echo "welcome to webb" > /public/index.html && http-server -a 0.0.0.0 -p 4200'

从 weba 内部到达 webb

docker exec -it weba sh -c 'curl http://webb:4200'
# welcome to webb

从 webb 内部到达 weba

docker exec -it webb sh -c 'curl http://weba:4200'
# welcome to weba

这看起来对我有用;您的环境有什么不同吗?