Zakku-Spot.com

気になる情報のリサーチブログ - Reserch blog of information you care about -

[基礎知識]特定のコンテナが接続しているネットワークを調べる手順

9月 13, 2023
0 0
Read Time:3 Minute, 16 Second

 特定のコンテナが接続しているネットワークは、docker inspectコマンドで確認できます。
このコマンドは、コンテナ、イメージ、ネットワークなど、Dockerオブジェクトに関する詳細な情報をJSON形式で出力します。

1.対象コンテナ名 または 対象コンテナID の確認

・docker network ls

2.対象コンテナのネットワーク情報 の確認

(1)ネットワーク情報全ての表示

・docker inspect <対象コンテナ名 または 対象コンテナID>

実例:

 docker inspect 7af1f689e956

※この結果は非常に長く分り難いです。(口述手順により必要個所表示ができます)

C:\Users\lemon>docker inspect 7af1f689e956
[
    {
        "Id": "7af1f689e956bc324fc45db6a77bfd79118c7d57712d20b7ef5ffbeb2136dafe",
        "Created": "2023-09-10T18:39:10.602476821Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 9060,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-09-10T18:39:11.678620165Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:a411d977f740dfdb37e3dc7ff751bed5ca17c4862f3bd16a9e4fa730b8edc0ba",
        "ResolvConfPath": "/var/lib/docker/containers/7af1f689e956bc324fc45db6a77bfd79118c7d57712d20b7ef5ffbeb2136dafe/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/7af1f689e956bc324fc45db6a77bfd79118c7d57712d20b7ef5ffbeb2136dafe/hostname",
        "HostsPath": "/var/lib/docker/containers/7af1f689e956bc324fc45db6a77bfd79118c7d57712d20b7ef5ffbeb2136dafe/hosts",
        "LogPath": "/var/lib/docker/containers/7af1f689e956bc324fc45db6a77bfd79118c7d57712d20b7ef5ffbeb2136dafe/7af1f689e956bc324fc45db6a77bfd79118c7d57712d20b7ef5ffbeb2136dafe-json.log",
        "Name": "/anaconda-dockerfile-gui-container",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": [
            "f1d86856996fd3099f1f5b7f820e94014bcb6d67504408b2138fdd9e7650b803",
            "782b4a571856c10f6191a86cdc7ff8161239df70174c541f7ea77070580f862e",
            "580a4c4f76d150b0234545d3b1f25ed40c5ca83173771c0a508a475749e5e8cf",
            "e1026dc5e49842618d8841a0551814f528ea1b12ad3eb1264fee25f2b02609a7",
            "1da4b0dcfde24a1abac6a22165c5c4cac6ff2b98fd105a6d6f44c6847488d489"
        ],
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "8000/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "8000"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "ConsoleSize": [
                30,
                127
            ],
            "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",
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": [],
            "BlkioDeviceWriteBps": [],
            "BlkioDeviceReadIOps": [],
            "BlkioDeviceWriteIOps": [],
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "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/f9435277f214c0c362ae38f3bf36485748a74aee564cfcf25b2c39159bababee-init/diff:/var/lib/docker/overlay2/od5spz2oxxzqr621dwjt75qgj/diff:/var/lib/docker/overlay2/fsg7j1si5jh532f2xy4y81irg/diff:/var/lib/docker/overlay2/030f14e3bc025c13b4423ea4fc4b021d1ac13054f9b90fb2111083b9f6bbc5d7/diff:/var/lib/docker/overlay2/6f02789f0eddc31047eb98503021393ae23759dab82ff9792f63435dc36d5804/diff",
                "MergedDir": "/var/lib/docker/overlay2/f9435277f214c0c362ae38f3bf36485748a74aee564cfcf25b2c39159bababee/merged",
                "UpperDir": "/var/lib/docker/overlay2/f9435277f214c0c362ae38f3bf36485748a74aee564cfcf25b2c39159bababee/diff",
                "WorkDir": "/var/lib/docker/overlay2/f9435277f214c0c362ae38f3bf36485748a74aee564cfcf25b2c39159bababee/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "7af1f689e956",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "ExposedPorts": {
                "8000/tcp": {}
            },
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "DISPLAY=host.docker.internal:0.0",
                "PATH=/opt/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "LANG=C.UTF-8",
                "LC_ALL=C.UTF-8",
                "XDG_RUNTIME_DIR=/tmp/runtime-root"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "anaconda-gui-mount-image:latest",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "com.docker.compose.project": "07_ana-gui-docker-foldermount",
                "com.docker.compose.service": "anaconda",
                "com.docker.compose.version": "2.20.2",
                "maintainer": "ys.yoshisuke@gmail.com",
                "org.opencontainers.image.created": "2023-07-18T15:06:19.609Z",
                "org.opencontainers.image.description": "Repository of Docker images created by Anaconda",
                "org.opencontainers.image.licenses": "",
                "org.opencontainers.image.revision": "912aa2402e807e2faeaf1e778c6be7cd73e70e1f",
                "org.opencontainers.image.source": "https://github.com/ContinuumIO/docker-images",
                "org.opencontainers.image.title": "docker-images",
                "org.opencontainers.image.url": "https://github.com/ContinuumIO/docker-images",
                "org.opencontainers.image.version": "2023.07-1",
                "version": "1.0"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "573b95738f0ff0a175eda3f335eb3d0301fcb7b64715bdb1330fb670b1b780ef",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "8000/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8000"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/573b95738f0f",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "c532c917c07c2f8c83bd51619bb3f8fdba93a236cc509bc309c0fe67500605f8",
            "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": "98569aa4661476bee122836019996153ae9f3051b635cd315eb54b11a473c0c8",
                    "EndpointID": "c532c917c07c2f8c83bd51619bb3f8fdba93a236cc509bc309c0fe67500605f8",
                    "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
                }
            }
        }
    }
]

(2)対象コンテナのネットワーク情報のみを表示

 上記(1)のコマンドの出力はかなり長いですが、JSONの "Networks" セクションを探すことで、コンテナが接続しているネットワークに関する情報がわかります。
もしこの情報をより読みやすい形で得たい場合は、jqツールを使うと便利です。

・docker inspect | jq ‘.[0].NetworkSettings.Networks’

jqツールは、JSONデータをフィルタリングや変換するためのコマンドラインユーティリティです。このコマンドは、コンテナのネットワーク設定のみを抽出して表示します。

 なお、jqは事前にインストールしておく必要があります。LinuxやmacOSの場合は、パッケージマネージャを通してインストールできます。Windowsの場合は、jqの公式サイトからバイナリをダウンロードして使えます。

実例:

 docker inspect anaconda-dockerfile-gui-container | jq ‘.[0].NetworkSettings.Networks’

 もしくは

 docker inspect 7af1f689e956 | jq ‘.[0].NetworkSettings.Networks’

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

コメントを残す