docker tutorials

Get Docker Container’s IP Address from the Host

This tutorial guides you on how to get docker container’s ip address from the host. To get an instance’s IP address docker inspect command will be used.

Get Docker Container’s IP Address from the host

The docker inspect command returns low level details about docker objects. Let’s say you wanted to find the following docker container’s IP address.

$ docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
38dbd136212f        sneppets/ubuntu     "/bin/sh -c '/usr/sb…"   12 seconds ago      Up 10 seconds                           relaxed_gould

By default docker inspect will render results in a JSON array as shown below.

$ docker inspect 38dbd136212f

[
    {
        "Id": "38dbd136212fe376af6ad4a61812ca50b8fe276673b751f8be802a181da24861",
        "Created": "2020-08-01T03:18:25.573630791Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "/usr/sbin/sshd -D"
        ],        
        ---------
        ---------
        ---------
        "NetworkSettings": {
            "Bridge": "",
            -------
            -------
            -------
            "Gateway": "172.18.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.18.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:12:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "180ea7aef90c752db2b9f93685592d6b379f7b0b8e425c455bef01bad9b75fd6",
                    "EndpointID": "110e984e6bba83d0bd7f68e420286dfe65871455dcf552003f177161fd5750a5",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

To get the specific detail about docker object, docker inspect allows you to pick any field from the above JSON array by using the format Option as shown below. This command will output only the docker container’s IP Address.

$ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 38dbd136212f

172.18.0.2

Usage

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <Container Id>

Get all docker container’s IP addresses from host

The following command print’s all docker container’s IP addresses along with name of the respective containers.

$ docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

/bold_agnesi   - 172.18.0.3
/relaxed_gould - 172.18.0.2

To get the docker container’s image name as well, use the following command.

$ docker inspect --format='{{.Name}} - {{.Config.Image}}- {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

/bold_agnesi - redis- 172.18.0.3
/relaxed_gould - sneppets/ubuntu- 172.18.0.2

Filter docker container IP address using grep

The following docker command can be used to filter IP address field from the docker inspect JSON array response.

$ docker inspect 38dbd136212f | grep '"IPAddress"' | head -n 1
            
"IPAddress": "172.18.0.2",

Hope it helped 🙂

Also See:

References:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments