How to Get the IP Address of a Docker Container

Isaac Tony Feb 02, 2024
  1. Connect to the Bridge Network and Get the IP Address of a Docker Container
  2. Use docker inspect to Get the IP Address of a Docker Container
How to Get the IP Address of a Docker Container

This tutorial demonstrates how to get the IP address of a Docker container IP address.

Connect to the Bridge Network and Get the IP Address of a Docker Container

One of the significant reasons why docker containers are so convenient is that we can easily connect them. This allows containers to communicate and easily share resources.

We can also connect containers to no docker workloads.

This article is not about docker networking; therefore, we will not delve into the specifics of docker networking. However, docker provides various drivers that make networking pluggable using Bridge drivers.

Docker allows us to create networks using its default drivers known as bridge drivers. However, the bridge network is private with a limited scope to containers on the host.

You can view the default networks on your host and those you have previously. Every docker installation usually includes the three default networks shown below.

isaac@DESKTOP-HV44HT6:~$ docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
9729860fa596   bridge    bridge    local
0667c3f7d0f0   host      host      local
c3273b158256   none      null      local

Docker will always launch new containers in the bridge network unless specified otherwise.

Container IP addresses are an essential part of networking docker containers. Typically, containers are assigned an IP address for every network they connect.

On the other hand, you can also manually connect a docker container to the bridge network using the command below.

$ docker run -dt rabbitmq

This will create a docker container and assign it to the bridge network. Before we can inspect the network to confirm that it is, connected to it, we should first make sure the container is running.

$ docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED              STATUS              PORTS                                                 NAMES
42487cad0390   rabbitmq   "docker-entrypoint.s…"   About a minute ago   Up About a minute   4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp   compassionate_keller

Inspecting the bridge network will allow us to view the containers connected to the networks, among other details such as the containers’ IP addresses and the default subnet for the container. The details are returned in JSON format, as shown below.

~$ docker network inspect bridge

Output:

"ConfigOnly": false,
        "Containers": {
            "42487cad0390a8de6d1a88bc1d6c09ffdf3162dc85d4d5d3dc70200b2348b673": {
                "Name": "compassionate_keller",
                "EndpointID": "ffa55232565d3a32d1e471d0753c1f491d6131a5cca50b8b76bc43a8ff554e32",
                "MacAddress": "02:42:ac:11:00:04",
                "IPv4Address": "172.17.0.4/16",
                "IPv6Address": ""
            }

The IP address for our container in the above example is 172.17.0.4/16.

We can also have multiple containers connected to the network, and we can easily find the IP addresses of both by inspecting the bridge network. As shown below, we add a container based on the Nginx image to the bridge network.

isaac@DESKTOP-HV44HT6:~$ docker run -dt nginx
4ab752ab92582a0eb2cb14475094460fc8cc608c93a357a8dca082cfea2bc368

Now, if we inspect the bridge network, we will be able to obtain the IP addresses of the two containers. Remember that the two containers should be running.

isaac@DESKTOP-HV44HT6:~$ docker network inspect bridge

Output:

"Containers": {
            "42487cad0390a8de6d1a88bc1d6c09ffdf3162dc85d4d5d3dc70200b2348b673": {
                "Name": "compassionate_keller",
                "EndpointID": "ffa55232565d3a32d1e471d0753c1f491d6131a5cca50b8b76bc43a8ff554e32",
                "MacAddress": "02:42:ac:11:00:04",
                "IPv4Address": "172.17.0.4/16",
                "IPv6Address": ""
            },
            "4ab752ab92582a0eb2cb14475094460fc8cc608c93a357a8dca082cfea2bc368": {
                "Name": "trusting_keller",
                "EndpointID": "a2babd11586f493cf5e57f5d9920a7f5648cf6163e77030521bef62fc9f34a63",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        }

The IP addresses for the two containers are 172.17.0.4/16 and 172.17.0.2/16 in order of their creation.

Use docker inspect to Get the IP Address of a Docker Container

We can also use docker inspect and the container’s name or the container id to find the IP address of a specific Docker container. This will equally return many other details in JSON format.

$ docker inspect 42487cad0390

Output:

"Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "9729860fa5961eeac55f921ee787b2c82a15146cd36117b5394243be2149e929",
                    "EndpointID": "ffa55232565d3a32d1e471d0753c1f491d6131a5cca50b8b76bc43a8ff554e32",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:04",
                    "DriverOpts": null
                }
            }

In this case, we have inspected the container based on the official Nginx image. You can refer to the Docker documentation for fun stuff on networking Docker containers.

Author: Isaac Tony
Isaac Tony avatar Isaac Tony avatar

Isaac Tony is a professional software developer and technical writer fascinated by Tech and productivity. He helps large technical organizations communicate their message clearly through writing.

LinkedIn

Related Article - Docker Container