In this article we will discuss how to get ip address assigned to a running docker container from host using docker inspect command.
docker inspect
Docker command line interface provides a command docker inspect to get the low level information about the docker container i.e.
docker inspect <Option> <container name | ID >
It returns a lot of low level information about the running docker container in JSON format. In that information we get the assigned IP address of the container too.Â
But, apart from IP address it returns too much information. If we want some specific information then we can use the –format (-f) flag. It can format the JSON output of docker inspect using the given Go template.
Let’s see some examples,
Suppose we have a running docker container i.e.
Frequently Asked:
- How to Stop & remove all or running or stopped docker containers ?
- Docker : How to Stop & Remove a running container by ID or Name ?
- How to get IP address of running docker container from host using inspect command ?
- How to create & run a Docker Container from an Image ?
docker ps
List of running containers,
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1caec9cde092 centos "/bin/bash" 3 seconds ago Up 2 seconds hungry_williamson
Get Docker container IP address from the Host
Get a Docker container IP address from host using Container Name
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' hungry_williamson
 It will return the IP address of container with name hungry_williamson i.e.
172.17.0.2
Using a GO template we filtered the output of docker inspect command to fetch IP address only.
Get a Docker container IP address from host using container ID
Similarly we can fetch the IP address using container ID instead of name in previous command. Like this,
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 1caec9cde092
It will return the IP address of container with name 1caec9cde092 i.e.
172.17.0.2
So, this way we got the IP address of a docker container from host. But what if we have 100 running containers and we want their IP addresses along with name ?
Get IP Address and names of ALL the running containers from host
Suppose we have 3 running containers,
PS C:\Windows\system32> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 915d14f022e9 ubuntu "/bin/bash" 44 seconds ago Up 43 seconds boring_dubinsky 21ea36eb1663 centos "/bin/bash" 52 seconds ago Up 51 seconds tender_raman a257a6e955b8 centos "ping localhost" 4 hours ago Up 4 hours quizzical_banzai
Now we want to fetch the name and IP address of all the running containers. Let’s see how to do that,
To get the IDs of all running containers use following command
$(docker ps -q)
It will return a list of all the running containers i.e.
915d14f022e9 21ea36eb1663 a257a6e955b8
Now let’s pass this list of container IDs to docker inspect command and fetch name of containers only i.e.
docker inspect --format='{{.Name}}' $(docker ps -q)
It will return the name of all the running containers i.e.
/boring_dubinsky /tender_raman /quizzical_banza
Now we can also get the Name and IP of all running containers too using inspect command i.e.
docker inspect --format='{{.Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -q)
It will return the name and IP address of all the running containers i.e.
/boring_dubinsky 172.17.0.4 /tender_raman 172.17.0.3 /quizzical_banzai 172.17.0.2
docker inspect provides many information regarding a container. Here we fetched only couple of fields using GO template.
Main information provided by docker inspect includes, configurations, state, NetworkSettings and many other sections.
Get Complete Network Settings of a docker container
To get the complete network settings of a running docker container using inspect command use this,
docker inspect --format='{{json .NetworkSettings}}' Â <container ID>
For example,
docker inspect --format='{{json .NetworkSettings}}' Â 915d14f022e9
It will display network settings like this,
{ "Bridge": "", "SandboxID": "ca421761e775d17bfea4afd761041d55015be32fece11b1ae468cae581e8fef8", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/ca421761e775", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "0d13ef40107cc275913658b1fe8a2b355dae02eea8e6b38f0426e33afad6d505", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.4", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "01:12:11:11:0a:04", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "c86b149b1b8c0c26ddd22dc55c4bcdea7f3d9067c89fba1cec0d2072de2500d6", "EndpointID": "0d13ef40107cc275913658b1fe8a2b355dae02eea8e6b38f0426e33afad6d505", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.4", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "01:12:11:11:0a:04", "DriverOpts": null } } }
Now you can use this command to fetch information of other sections too . These section can be,
- Config
- GraphDriver
- Mounts
- HostConfig
- Name
- LogPath
- State
- Created
- etc.
Replace the <SectionName> in below command to get the detailed information about that section.
docker inspect --format='{{json .<SectionName>}}' <container ID>
You can also directly fetch some specific information inside the section. For example, let’s fetch Image name inside the Config section,
Get Image name from the container ID,
docker inspect --format='{{json .Config.Image}}' Â 915d14f022e9
Output:
"ubuntu"
Similarly, to fetch the complete configuration details use,
docker inspect --format='{{json .Config}}' Â 915d14f022e9
Output:
{ "Hostname": "ccc141f0aabb", "Domainname": "", "User": "", "AttachStdin": true, "AttachStdout": true, "AttachStderr": true, "Tty": true, "OpenStdin": true, "StdinOnce": true, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash" ], "ArgsEscaped": true, "Image": "ubuntu", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": {} }
Complete output of docker inspect command is,
docker inspect 915d14f022e9
Output :
[ { "Id": "915d14f022e981988c68318e689465a6418271b5d48c2b4460711258ff6d3d1b", "Created": "2019-05-17T11:59:34.6725636Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 7878, "ExitCode": 0, "Error": "", "StartedAt": "2019-05-17T11:59:35.3313105Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:7698f282e5242af2b9d2291458d4e425c75b25b0008c1e058d66b717b4c06fa9", "ResolvConfPath": "/var/lib/docker/containers/915d14f022e981988c68318e689465a6418271b5d48c2b4460711258ff6d3d1b/resolv.conf", "HostnamePath": "/var/lib/docker/containers/915d14f022e981988c68318e689465a6418271b5d48c2b4460711258ff6d3d1b/hostname", "HostsPath": "/var/lib/docker/containers/915d14f022e981988c68318e689465a6418271b5d48c2b4460711258ff6d3d1b/hosts", "LogPath": "/var/lib/docker/containers/915d14f022e981988c68318e689465a6418271b5d48c2b4460711258ff6d3d1b/915d14f022e981988c68318e689465a6418271b5d48c2b4460711258ff6d3d1b-json.log", "Name": "/boring_dubinsky", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "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, "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "shareable", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 50, 120 ], "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, "DiskQuota": 0, "KernelMemory": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": 0, "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/e672d0a8d700a9c1bb71609797dab808bd94a3afc3a27146514ef675d4dd5646-init/diff:/var/lib/docker/overlay2/1a8750cb58227bd727660426727a10ce4238d6d5816fc8b6e51e5a2088a9c9a5/diff:/var/lib/docker/overlay2/2e52c7be74d23e22503837ff9e14522e9973f6bc4ac5eba13a35ec28525ba0f6/diff:/var/lib/docker/overlay2/5f9b484866dd87ca8ace0b083994e508070005bf52f1b937c12679c5293286fe/diff", "MergedDir": "/var/lib/docker/overlay2/e672d0a8d700a9c1bb71609797dab808bd94a3afc3a27146514ef675d4dd5646/merged", "UpperDir": "/var/lib/docker/overlay2/e672d0a8d700a9c1bb71609797dab808bd94a3afc3a27146514ef675d4dd5646/diff", "WorkDir": "/var/lib/docker/overlay2/e672d0a8d700a9c1bb71609797dab808bd94a3afc3a27146514ef675d4dd5646/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "ccc141f0aabb", "Domainname": "", "User": "", "AttachStdin": true, "AttachStdout": true, "AttachStderr": true, "Tty": true, "OpenStdin": true, "StdinOnce": true, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash" ], "ArgsEscaped": true, "Image": "ubuntu", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "ca421761e775d17bfea4afd761041d55015be32fece11b1ae468cae581e8fef8", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/ca421761e775", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "0d13ef40107cc275913658b1fe8a2b355dae02eea8e6b38f0426e33afad6d505", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.4", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "01:12:11:11:0a:04", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "c86b149b1b8c0c26ddd22dc55c4bcdea7f3d9067c89fba1cec0d2072de2500d6", "EndpointID": "0d13ef40107cc275913658b1fe8a2b355dae02eea8e6b38f0426e33afad6d505", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.4", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "01:12:11:11:0a:04", "DriverOpts": null } } } } ]