Docker Container无法连接到Internet

时间:2017-08-13 19:43:08

标签: amazon-web-services docker amazon-ec2 etcd docker-networking

我在AWS EC2实例Docker容器上运行,Docker容器通过Docker网络连接,该网络通过etcd集群进行复制。 但是所有容器都无法连接到外部。例如,如果我ping或卷曲主机,则会丢失100%的包。 etcd网络通过AWS专用IP进行通信。

这是我的码头信息:

root@ip-10-0-127-34:/home# docker info
Containers: 3
 Running: 3
 Paused: 0
 Stopped: 0
Images: 9
Server Version: 17.05.0-ce
Storage Driver: devicemapper
 Pool Name: docker-202:1-2050903-pool
 Pool Blocksize: 65.54kB
 Base Device Size: 10.74GB
 Backing Filesystem: xfs
 Data file: /dev/loop0
 Metadata file: /dev/loop1
 Data Space Used: 4.554GB
 Data Space Total: 107.4GB
 Data Space Available: 102.8GB
 Metadata Space Used:sattler 6.922MB
 Metadata Space Total: 2.147GB
 Metadata Space Available: 2.141GB
 Thin Pool Minimum Free Space: 3.221GB
 Udev Sync Supported: true
 Deferred Removal Enabled: false
 Deferred Deletion Enabled: false
 Deferred Deleted Device Count: 0
 Data loop file: /var/lib/docker/devicemapper/devicemapper/data
 Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
 Library Version: 1.02.110 (2015-10-30)
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins: 
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9048e5e50717ea4497b757314bad98ea3763c145
runc version: 9c2d8d184e5da67c95d601382adf14862e4f2228
init version: 949e6fa
Security Options:
 apparmor
 seccomp
  Profile: default
Kernel Version: 4.4.0-1028-aws
Operating System: Ubuntu 16.04.3 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 7.303GiB
Name: ip-10-0-127-34
ID: JFQO:C56I:VM22:UDKJ:QDKD:HUDT:KL3X:JCSR:WAPG:66JL:S4RM:4ENN
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Cluster Store: etcd://127.0.0.1:2379
Cluster Advertise: 10.0.127.34:2376
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

WARNING: devicemapper: usage of loopback devices is strongly discouraged for production use.
         Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
WARNING: No swap limit support

Docker检查网络:

     {
    "Name": "df-global",
    "Id": "59aef8ccc5d7464ee715428783b9b03ba7737d298f41cc30a21f4856e75b92c1",
    "Created": "2017-08-14T09:51:31.650109966Z",
    "Scope": "global",
    "Driver": "overlay",
    "EnableIPv6": false,
    "IPAM": {
        "Driver": "default",
        "Options": {},
        "Config": [
            {
                "Subnet": "192.168.1.0/14"
            }
        ]
    },
    "Internal": false,
    "Attachable": false,
    "Ingress": false,
    "Containers": {

     "ep-f257d697653d801395f9154d086a6290694d6ded7aefe827c67dcd10808023eb": {
            "Name": "elasticsearch-data-1",
            "EndpointID": "f257d697653d801395f9154d086a6290694d6ded7aefe827c67dcd10808023eb",
            "MacAddress": "02:42:c0:a8:00:0b",
            "IPv4Address": "192.168.0.11/14",
            "IPv6Address": ""
        },
        "ep-f5c1be7a768802154b58b33e67a3f009df1d4f1336297df9f914be9a720bd8ce": {
            "Name": "postgresql-vg_internal01-prod",
            "EndpointID": "f5c1be7a768802154b58b33e67a3f009df1d4f1336297df9f914be9a720bd8ce",
            "MacAddress": "02:42:c0:a8:00:02",
            "IPv4Address": "192.168.0.2/14",
            "IPv6Address": ""

此处描述了同样的问题:https://forums.docker.com/t/ping-between-containers-on-different-docker-host-but-connected-by-overlay-networ-fails/9960 但没有答案

4 个答案:

答案 0 :(得分:2)

在主机上运行:

sudo ip addr show docker0 

您将获得包含以下内容的输出:

inet 172.17.2.1/16 scope global docker0

docker主机在docker0网络接口上具有IP地址172.17.2.1。

然后启动容器:

docker run --rm -it ubuntu:trusty bash 

并运行

ip addr show eth0

输出将包括:

inet 172.17.1.29/16 scope global eth0

您的容器的IP地址为172.17.1.29。现在看一下路由表: 运行:

route

输出将包括:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         172.17.2.1     0.0.0.0         UG    0      0        0 eth0

表示泊坞主机172.17.2.1的IP地址被设置为默认路由,可以从容器中访问。

立即尝试ping你的主机ip:

root@e21b5c211a0c:/# ping 172.17.2.1
PING 172.17.2.1 (172.17.2.1) 56(84) bytes of data.
64 bytes from 172.17.2.1: icmp_seq=1 ttl=64 time=0.071 ms
64 bytes from 172.17.2.1: icmp_seq=2 ttl=64 time=0.211 ms
64 bytes from 172.17.2.1: icmp_seq=3 ttl=64 time=0.166 ms 

如果这很有可能你可以ping任何公共IP

希望它会有所帮助!

答案 1 :(得分:1)

有一个替代方案..如果你不想搞乱网络路线..所以在运行你的容器时尝试Docker支持从主机到容器的绑定挂载文件。您可以绑定安装Docker控件套接字。

docker run -v /var/run/docker.sock:/var/run/docker.sock ...usual code here...

希望它会有所帮助!

答案 2 :(得分:1)

允许容器互相交谈: 创建一个网络

docker network create <your-network-name>

然后将所有容器连接到新创建的网络。

docker network connect <your-network-name> <containers>

希望它会有所帮助!

答案 3 :(得分:0)

我仍然能够ping 172.17.2.1,但这是我可以从容器内联系的唯一IP地址。原来有人清除了iptables防火墙,并用默认的ACCEPT规则替换了每个表中的每个链!所有的docker nat和转发规则都丢失了。快速重启即可解决问题。

一个临时解决方案是使用docker命令行选项--network 'host'