Docker 主机不互相通信

Docker 主机不互相通信

我正在测试 docker 并浏览了许多文档,但我仍然无法将它们连接在一起。我有一个 shell 脚本来测试它,但我没有得到这些运行之间的连接。

$MAXTESTTIME=180
MACHINE1=$(docker run --net="testnet" -h MACHINE1 --name MACHINE1 -p 10080:80 -v /root/testcontent:/var/www/html:ro --cpuset-cpus="3-9" --memory="2G" -d ubuntu /bin/bash -c "sleep 120")
MACHINE2=$(docker run --net="testnet" -h MACHINE2 --link MACHINE1:MACHINE1 --name MACHINE2 -v /root/testcontent:/var/www/html:ro --cpuset-cpus="10-11" --memory="2G" -d ubuntu /bin/bash -c "apt-get -y update && apt-get -y install apt-utils iputils-ping && ping -c 2 MACHINE1 && ping -c 2 8.8.8.8 && ping -c 2 www.google.com")

docker network create testnet
sleep 2


docker start $MACHINE1 && sleep 1 && docker start $MACHINE2 &  sleep $MAXTESTTIME; docker stop $MACHINE1 & docker stop $MACHINE2

正如你所看到的,我在两台机器上创建了一个循环,让它们运行直到时间到。但如果我将它们设置为专用网络或进行端口转发,我测试什么并不重要。他们永远无法进行名称查找或 ping 主机。

有人可以指出我做错了什么吗?我从 2、5 天开始就试图解决这个问题,但我没有更接近。

Containers: 2
 Running: 2
 Paused: 0
 Stopped: 0
Images: 4
Server Version: 1.12.1
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 11
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host overlay null
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options: apparmor seccomp
Kernel Version: 4.4.0-31-generic
Operating System: Ubuntu 16.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 12
Total Memory: 125.9 GiB
Name: xxxxxx.li
ID: xxxxxxxxxxxxx
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: No swap limit support
Insecure Registries:
 127.0.0.0/8

这是检查输出

root@monster ~ # docker inspect MACHINE1
[
    {
        "Id": "81f21a696a290813e32ee12af981453cfe29b26cdef809712dbbc4b77506e57c",
        "Created": "2016-08-21T18:05:16.3526528Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "apt-get update \u0026\u0026 apt-get -y upgrade \u0026\u0026 apt-get -y install apache2 \u0026\u0026 systemctl restart apache2 \u0026\u0026 systemctl start apache2 \u0026\u0026 while sleep 5; do echo blah; done"
        ],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 1,
            "Error": "",
            "StartedAt": "2016-08-21T18:05:16.672425325Z",
            "FinishedAt": "2016-08-21T18:05:40.466207063Z"
        },
        "Image": "sha256:f8d79ba03c00bbcd8079cf05b7526ac8f4f422744aad8c3747a29a38ed8c4a41",
        "ResolvConfPath": "/var/lib/docker/containers/81f21a696a290813e32ee12af981453cfe29b26cdef809712dbbc4b77506e57c/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/81f21a696a290813e32ee12af981453cfe29b26cdef809712dbbc4b77506e57c/hostname",
        "HostsPath": "/var/lib/docker/containers/81f21a696a290813e32ee12af981453cfe29b26cdef809712dbbc4b77506e57c/hosts",
        "LogPath": "/var/lib/docker/containers/81f21a696a290813e32ee12af981453cfe29b26cdef809712dbbc4b77506e57c/81f21a696a290813e32ee12af981453cfe29b26cdef809712dbbc4b77506e57c-json.log",
        "Name": "/MACHINE1",
        "RestartCount": 0,
        "Driver": "aufs",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/root/testcontent:/var/www/html:ro"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "testnet",
            "PortBindings": {
                "80/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "10080"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 12884901888,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpusetCpus": "3-9",
            "CpusetMems": "",
            "Devices": [],
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": -1,
            "MemorySwappiness": -1,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Name": "aufs",
            "Data": null
        },
        "Mounts": [
            {
                "Source": "/root/testcontent",
                "Destination": "/var/www/html",
                "Mode": "ro",
                "RW": false,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "MACHINE1",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash",
                "-c",
                "apt-get update \u0026\u0026 apt-get -y upgrade \u0026\u0026 apt-get -y install apache2 \u0026\u0026 systemctl restart apache2 \u0026\u0026 systemctl start apache2 \u0026\u0026 while sleep 5; do echo blah; done"
            ],
            "Image": "ubuntu",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "b5165f66d0a487ddb8ff33fc4a9cd078f67b8cccb3a34bc6473d411cca9e5d77",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": null,
            "SandboxKey": "/var/run/docker/netns/b5165f66d0a4",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "testnet": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "81f21a696a29"
                    ],
                    "NetworkID": "a2c25f900d0b2fe4f8a8e5f1b564d4bac107af4a0e2ab6bfbd8c72d7c009a0d2",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": ""
                }
            }
        }
    }
]
root@monster ~ # docker inspect MACHINE2
[
    {
        "Id": "21d8fefb2bd3b8665d6aacaecb38ff554a06f5f433129104ab97f318e3ad8168",
        "Created": "2016-08-21T18:05:16.69206676Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "apt-get -y update \u0026\u0026 apt-get -y install apt-utils iputils-ping wget curl git netcat \u0026\u0026 while sleep 1; do ping -c 3 MACHINE1 \u0026\u0026 curl http://MACHINE1:80 \u0026\u0026 curl http://MACHINE1:10080 ; done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 25612,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2016-08-21T18:05:16.990214383Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:f8d79ba03c00bbcd8079cf05b7526ac8f4f422744aad8c3747a29a38ed8c4a41",
        "ResolvConfPath": "/var/lib/docker/containers/21d8fefb2bd3b8665d6aacaecb38ff554a06f5f433129104ab97f318e3ad8168/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/21d8fefb2bd3b8665d6aacaecb38ff554a06f5f433129104ab97f318e3ad8168/hostname",
        "HostsPath": "/var/lib/docker/containers/21d8fefb2bd3b8665d6aacaecb38ff554a06f5f433129104ab97f318e3ad8168/hosts",
        "LogPath": "/var/lib/docker/containers/21d8fefb2bd3b8665d6aacaecb38ff554a06f5f433129104ab97f318e3ad8168/21d8fefb2bd3b8665d6aacaecb38ff554a06f5f433129104ab97f318e3ad8168-json.log",
        "Name": "/MACHINE2",
        "RestartCount": 0,
        "Driver": "aufs",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/root/testcontent:/var/www/html:ro"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "testnet",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 12884901888,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpusetCpus": "10-11",
            "CpusetMems": "",
            "Devices": [],
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": -1,
            "MemorySwappiness": -1,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Name": "aufs",
            "Data": null
        },
        "Mounts": [
            {
                "Source": "/root/testcontent",
                "Destination": "/var/www/html",
                "Mode": "ro",
                "RW": false,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "MACHINE2",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash",
                "-c",
                "apt-get -y update \u0026\u0026 apt-get -y install apt-utils iputils-ping wget curl git netcat \u0026\u0026 while sleep 1; do ping -c 3 MACHINE1 \u0026\u0026 curl http://MACHINE1:80 \u0026\u0026 curl http://MACHINE1:10080 ; done"
            ],
            "Image": "ubuntu",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "49d4c262db743f5e95396ef0939afc069f725c73d3b7ada4682b7103e099f7c0",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/49d4c262db74",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "testnet": {
                    "IPAMConfig": null,
                    "Links": [
                        "MACHINE1:MACHINE1"
                    ],
                    "Aliases": [
                        "21d8fefb2bd3"
                    ],
                    "NetworkID": "a2c25f900d0b2fe4f8a8e5f1b564d4bac107af4a0e2ab6bfbd8c72d7c009a0d2",
                    "EndpointID": "bf5e1d75206f997c946f299c3901a133001f309e5f1da382c6e292049ff29d58",
                    "Gateway": "192.168.0.1",
                    "IPAddress": "192.168.0.3",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:c0:a8:00:03"
                }
            }
        }
    }
]
root@monster ~ #

从 ping 中我看到名称解析正在工作,但数据包未到达机器 1。

答案1

命令都是正确的,但我必须设置:

sysctl -w net.ipv4.ip_forward=1

然后需要重启docker服务

service docker restart

相关内容