具有多个网络接口的Docker容器:只获取一个IP地址

时间:2017-05-30 13:00:58

标签: docker containers

我有一个名为some-mysq l的docker容器,附着在一个名为mynet的覆盖网络上:当我docker inspect some-mysql时,它向我展示了这个巨大的JSON:

   {
        "Id": "49cb39a4e8e5e7421a4ae69ffd5bb17a8f90f02abbfc3f64ef80d206ceb11c1f",
        "Created": "2017-02-24T09:21:50.237425126Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "mysqld"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 28706,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2017-03-27T06:59:40.137554407Z",
            "FinishedAt": "2017-03-25T00:05:46.938307695Z"
        },
        "Image": "sha256:7666f75adb6b50676a366c6fd7a3916cb41f6e8eaf336c3d3ab7d35317fed0b9",
        "ResolvConfPath": "/var/lib/docker/containers/49cb39a4e8e5e7421a4ae69ffd5bb17a8f90f02abbfc3f64ef80d206ceb11c1f/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/49cb39a4e8e5e7421a4ae69ffd5bb17a8f90f02abbfc3f64ef80d206ceb11c1f/hostname",
        "HostsPath": "/var/lib/docker/containers/49cb39a4e8e5e7421a4ae69ffd5bb17a8f90f02abbfc3f64ef80d206ceb11c1f/hosts",
        "LogPath": "/var/lib/docker/containers/49cb39a4e8e5e7421a4ae69ffd5bb17a8f90f02abbfc3f64ef80d206ceb11c1f/49cb39a4e8e5e7421a4ae69ffd5bb17a8f90f02abbfc3f64ef80d206ceb11c1f-json.log",
        "Name": "/some-mysql",
        "RestartCount": 0,
        "Driver": "aufs",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "3306/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "3307"
                    }
                ]
            },
            "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": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": -1,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Name": "aufs",
            "Data": null
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "c709191cb719b447772bde814f490eb2b30429c9ca7f1c62fda445af9e0b5fb9",
                "Source": "/var/lib/docker/volumes/c709191cb719b447772bde814f490eb2b30429c9ca7f1c62fda445af9e0b5fb9/_data",
                "Destination": "/var/lib/mysql",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "49cb39a4e8e5",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "MYSQL_ROOT_PASSWORD=pass",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.7",
                "MYSQL_MAJOR=5.7",
                "MYSQL_VERSION=5.7.17-1debian8"
            ],
            "Cmd": [
                "mysqld"
            ],
            "ArgsEscaped": true,
            "Image": "mysql",
            "Volumes": {
                "/var/lib/mysql": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "1f5cde48c303e649f0b675ac11f302336936c7ea0f7c1a0ea8054866c308189d",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "3306/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "3307"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/1f5cde48c303",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "2800343c29643796d28e8f58a13a7dbf5d1a2706d26f25e27ea623a4673d4439",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:03",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "a3ec63c75685ca69af892f7047abc2c1d0955407d61423f2cff87500eedaadf3",
                    "EndpointID": "2800343c29643796d28e8f58a13a7dbf5d1a2706d26f25e27ea623a4673d4439",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03"
                },
                "mynet": {
                    "IPAMConfig": {},
                    "Links": null,
                    "Aliases": [
                        "49cb39a4e8e5"
                    ],
                    "NetworkID": "37af7ae0184078932bbc234c1f4b15983777f566c36f7eb93e2b4b3437e460a7",
                    "EndpointID": "fd1f776954723a705edef323b63588cfc93cca7b80b15f22414b72454e14e48c",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:02"
                }
            }
        }
    }
]

我的目标是只获取与网络接口some-mysql相关的mynet容器的IP地址。如果我做

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' some-mysql

我得到:172.17.0.3172.18.0.2:它们是默认网桥和我的覆盖网络的IP地址。如何仅检索some-mysql接口的mynet IP地址?

1 个答案:

答案 0 :(得分:3)

您可以指定网络名称,而无需range

docker inspect -f '{{.NetworkSettings.Networks.mynet.IPAddress}}' some-mysql