Ubuntu 中查看 docker中对应容器的 IP

沐子 阅读 1081 发表时间 2021-01-30 10:06:51
ubuntu docker ip

可以先使用 inspect 命令 查看该容器的所有配置信息

docker inspect 容器ID
ubuntu@VM-0-12-ubuntu:~$ docker inspect 97220b907297
[
    {
        "Id": "97220b907297cb62a252d45a421751227b7365b0e1bb1ecf96791fcfdd3c2cff",
        "Created": "2021-01-30T01:33:02.232014374Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 2351181,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-01-30T01:33:03.75802149Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:f63181f19b2fe819156dcb068b3b5bc036820bec7014c5f77277cfa341d4cb5e",
        "ResolvConfPath": "/var/lib/docker/containers/97220b907297cb62a252d45a421751227b7365b0e1bb1ecf96791fcfdd3c2cff/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/97220b907297cb62a252d45a421751227b7365b0e1bb1ecf96791fcfdd3c2cff/hostname",
        "HostsPath": "/var/lib/docker/containers/97220b907297cb62a252d45a421751227b7365b0e1bb1ecf96791fcfdd3c2cff/hosts",
        "LogPath": "/var/lib/docker/containers/97220b907297cb62a252d45a421751227b7365b0e1bb1ecf96791fcfdd3c2cff/97220b907297cb62a252d45a421751227b7365b0e1bb1ecf96791fcfdd3c2cff-json.log",
        "Name": "/flamboyant_hoover",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": [
            "af37c414a29167a2139aec063aab1c251c55e01f9916dec140fb22f5ca0603b2"
        ],
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "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": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/392107018bd4ebe7c85cc29537454fd2f33ae8129ef0bfde3ee4f75ead863501-init/diff:/var/lib/docker/overlay2/f384670f50c8fe4cccfeba67afbb47376d74034349dcc080cb4e36f412ce728c/diff:/var/lib/docker/overlay2/83408e955a2b20bff93760523e0532ee0e9892e2e6cfae506daf5264b35acdac/diff:/var/lib/docker/overlay2/b7f6b6eaf470bc19c5c5c11d2e67b6fc6a0896bd73d5bd6fcb0457b68b0f84fc/diff",
                "MergedDir": "/var/lib/docker/overlay2/392107018bd4ebe7c85cc29537454fd2f33ae8129ef0bfde3ee4f75ead863501/merged",
                "UpperDir": "/var/lib/docker/overlay2/392107018bd4ebe7c85cc29537454fd2f33ae8129ef0bfde3ee4f75ead863501/diff",
                "WorkDir": "/var/lib/docker/overlay2/392107018bd4ebe7c85cc29537454fd2f33ae8129ef0bfde3ee4f75ead863501/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "97220b907297",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "f63181f19b2f",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "25f72d55b7031ef2d8781c3907e92332db5fa3bfb3af5018a41d3983883c4276",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/25f72d55b703",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "9544e0edf97e997ee35058b6fa2d568c6cb31c64e2dd8feddf47e828903ab277",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "3e7e743e5c7dae49e945c9d473d48a566892bc813a37c98b654c3e21bfb6bcc3",
                    "EndpointID": "9544e0edf97e997ee35058b6fa2d568c6cb31c64e2dd8feddf47e828903ab277",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]


然后使用 IPAddress 过滤IP地址, 即可查看 docker 的 IP

docker inspect 容器ID | grep IPAddress
ubuntu@VM-0-12-ubuntu:~$ docker inspect 97220b907297 | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",

查看IP可以按照以上的方法实现.

0个回复
有思考的问答, 才是有价值的问答, 否则将无任何记录价值.
关注极客云图了解更多内容