我正在关注这个教程
https://docs.docker.com/articles/networking/# Between-containers
我使用了以下命令
$ sudo brctl addbr bridge0
$ sudo ip addr add 192.168.0.160/24 dev bridge0
$ sudo ip link set dev bridge0 up
# Confirming that our bridge is up and running
$ ip addr show bridge0
4: bridge0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state UP group default
link/ether 66:38:d0:0d:76:18 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.160/24 scope global bridge0
valid_lft forever preferred_lft forever
# Tell Docker about it and restart (on Ubuntu)
$ echo 'DOCKER_OPTS="-b=bridge0"' >> /etc/default/docker
$ sudo service docker start
它看起来工作正常,因为我可以看到我的所有容器都在该范围内获取 IP。
现在我有 3 个容器
mysql 192.168.0.3
redis 192.168.0.5
webserver 192.168.0.6
现在如果我从网络服务器 ping redis 就可以了
但我 CMA 无法 ping 主机,192.168.0.160
它说目标主机无法恢复
信息
vagrant@ubuntu-docker-base:/mydocker/containers$ brctl show
bridge name bridge id STP enabled interfaces
bridge0 8000.7e8945d no veth1a8b959
veth404a3b2
veth7959157
docker0 8000.5689799 no
容器
root@286329032cd5:/# ip route
default via 192.168.0.160 dev eth0
192.168.0.0/24 dev eth0 proto kernel scope link src 192.168.0.5
root@286329032cd5:/# ping 192.168.0.3
PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.125 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.130 ms
64 bytes from 192.168.0.3: icmp_seq=3 ttl=64 time=0.072 ms
^C
--- 192.168.0.3 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.072/0.109/0.130/0.026 ms
root@286329032cd5:/# ping 192.168.0.160
PING 192.168.0.160 (192.168.0.160) 56(84) bytes of data.
From 192.168.0.5 icmp_seq=1 Destination Host Unreachable
From 192.168.0.5 icmp_seq=2 Destination Host Unreachable
From 192.168.0.5 icmp_seq=3 Destination Host Unreachable
^C
--- 192.168.0.160 ping statistics ---
6 packets transmitted, 0 received, +3 errors, 100% packet loss, time 5032ms
pipe 3
root@286329032cd5:/#
答案1
我遇到了类似的问题,我想从 docker 容器连接到其主机以访问 postfix 和 apache 服务器。
它有助于将此类规则添加到主机的 iptables 中:
iptables -A INPUT -i docker0 -p tcp -m tcp --dport 25 -j ACCEPT
iptables -A INPUT -i docker0 -p tcp -m tcp --dport 80 -j ACCEPT
这些端口之前是开放的,但仅适用于 eth0 接口,不适用于 docker0 ;)
答案2
从你的手册中:
如果您已经启动并运行了 Docker,并且仍配置了旧的 docker0,那么您可能需要首先停止服务并删除接口:
# Stopping Docker and removing docker0
$ sudo service docker stop
$ sudo ip link set dev docker0 down
$ sudo brctl delbr docker0
答案3
我运行时遇到这个问题你好世界-nginx图片来自风筝学
如果我从 Docker 终端运行它,如下所示,我可以从 Windows 浏览器查看容器输出 HTML:
docker run -p 80:80 hello-world-nginx
该-p
选项将 VirtualBox 上的端口 80 转发到容器上的端口 80。