Docker Engine API连接到现有网络

时间:2018-06-26 09:25:38

标签: docker docker-compose docker-networking docker-engine docker-network

我必须将使用Docker Engine API创建的容器连接到现有网络。

$ docker network ls
NETWORK ID          NAME                      DRIVER              SCOPE
2babcf993368        bridge                    bridge              local
8a865b5fedae        cron-container_my-net     bridge              local
94e1d6468f07        host                      host                local
92f72e792ec8        none                      null                local

我要连接到容器的网络是 cron-container_my-net ,这是我用于创建容器的.json。

{
    "Image": "my-job",
    "Name": "my-container",
    "HostConfig": {
        "NetworkMode": "cron-container_my-net"
    },
    "NetworkDisable": "false"
}

这样,如果我进行检查:

$ docker inspect my-container
[...]
"NetworkSettings": {
    "Bridge": "",
    "SandboxID": "130b79dca9cf45fed41e50da897e8d37a18160900a8390733300dc01f691ac94",
    "HairpinMode": false,
    "LinkLocalIPv6Address": "",
    "LinkLocalIPv6PrefixLen": 0,
    "Ports": {},
    "SandboxKey": "/var/run/docker/netns/130b79dca9cf",
    "SecondaryIPAddresses": null,
    "SecondaryIPv6Addresses": null,
    "EndpointID": "",
    "Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "IPAddress": "",
    "IPPrefixLen": 0,
    "IPv6Gateway": "",
    "MacAddress": "",
    "Networks": {
        "cron-container_my-net": {
            "IPAMConfig": null,
            "Links": null,
            "Aliases": [
                "387929d92ffc"
            ],
            "NetworkID": "8a865b5fedaea1192ec0c6dcd742fc51af28001132c0ae375bdca018eb4706c9",
            "EndpointID": "",
            "Gateway": "",
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "MacAddress": "",
            "DriverOpts": null
        }
    }
}

但是,如果我对已经连接到网络的容器进行检查,那就是结果:

$ docker inspect other-container
[...]
"NetworkSettings": {
    "Bridge": "",
    "SandboxID": "0d5e7e600a3ef46090675409a5eb444c1e31520e3c8c6908e6bda69ba9759335",
    "HairpinMode": false,
    "LinkLocalIPv6Address": "",
    "LinkLocalIPv6PrefixLen": 0,
    "Ports": {
        "3000/tcp": null
    },
    "SandboxKey": "/var/run/docker/netns/0d5e7e600a3e",
    "SecondaryIPAddresses": null,
    "SecondaryIPv6Addresses": null,
    "EndpointID": "",
    "Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "IPAddress": "",
    "IPPrefixLen": 0,
    "IPv6Gateway": "",
    "MacAddress": "",
    "Networks": {
        "cron-container_my-net": {
            "IPAMConfig": null,
            "Links": null,
            "Aliases": [
                "89c1aa28e3af",
                "socket-io"
            ],
            "NetworkID": "8a865b5fedaea1192ec0c6dcd742fc51af28001132c0ae375bdca018eb4706c9",
            "EndpointID": "01f46a675125c2978de706a2c2ae46f942546028309e18c5561c91f3e7b147e4",
            "Gateway": "172.18.0.1",
            "IPAddress": "172.18.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "MacAddress": "02:42:ac:12:00:03",
            "DriverOpts": null
        }
    }
}

如何连接我的容器?

1 个答案:

答案 0 :(得分:0)

我不知道什么为您处理.json文件,但是通常您会这样:

$ docker create --name my-container \
  --network default \
  --network cron-container_my-net \
  --other-stuff-etc \
  my-job:latest

或者您可以使用docker-compose,因为这些设置非常容易理解。 (例如,请参见以下答案:https://stackoverflow.com/a/49055920/2915423

建议阅读:

https://docs.docker.com/network/bridge/#connect-a-container-to-a-user-defined-bridge https://developer.ibm.com/recipes/tutorials/bridge-the-docker-containers-to-external-network/ https://docs.docker.com/compose/networking/#configure-the-default-network