How to Get Docker Container IP Address From the Host?

In this blog post, we’ll explore various methods for finding the IP address of a Docker container that was started with the default container networking. When we say that a container was started with the default container networking, it means that the container is using the built-in Docker networking functionality to communicate with other containers and the host system.

Understanding Docker’s Default Container Networking

When you create a container in Docker, it is connected to a Docker network. By default, the container is connected to the default "bridge" network, assuming that you created the container without specifying a network.

The bridge network is a built-in network that enables containers to communicate with each other and with the host system. It assigns a unique IP address to the container from a pool of available IP addresses.

Prerequisites

To follow along with the examples in this post, we recommend using KodeKloud’s Docker playground. This playground will provide you instant access to a Docker environment where you can run and manage containers. No need for you to install any software. With just one click, you'll be ready to start working with containers.

Alternatively, you can use Docker Desktop. If you don’t have Docker Desktop, you can download it from here. You can find the official installation guide by following this link.

Try the Docker Network Lab for free

Docker Network Lab
Docker Network Lab

Run a Docker Container

Before we can find the IP address of a Docker container, we must first create a container. In this example, we will use the "nginx" image to run a simple web server.

Open up your terminal and run the following command:

docker container run -d --name=mynginx nginx

The execution of the command will return the following output:

You’ll see a long string (highlighted in the screenshot above) printed in the terminal window. It’s the container ID.

Next, let’s run the "docker ps" command to confirm that the container is up and running.

As you can see, the "mynginx" container is up and running as expected.

Do you remember our discussion about the default "bridge" network that a container is connected to by default? We can easily view this network by listing the available networks configured in our Docker environment. To do so, run the following command:

docker network ls 

The execution of the command will give you the following output:

We see three networks that are provided by Docker, out of the box. Among them, the network named "bridge" is the default network.

Now that we have a Docker container up and running, let's take a look at how we can find its IP address.

3 Methods to Find a Docker Container’s IP Address

There are many ways to find the IP addresses of a Docker container. In this section, we will explore three commonly used methods.

Method 1: Using the docker inspect Command

We can use the "docker inspect" command to retrieve detailed information about a Docker container, including its IP address. Replace the container name or container ID in either of these two commands. Run the command with your container name or ID:

docker inspect <CONTAINER NAME>

or

docker inspect <CONTAINER ID>

After executing the command, you’ll see an output like this:

We have a big JSON object inside an array. Scroll down and find the "NetworkSettings" block. Within this block, there is a field called "IPAddress". The value of this field (127.17.0.2) is the IP address of the "mynginx" container, as highlighted in the screenshot below:

However, scrolling through the JSON output and manually locating the container’s IP address can be cumbersome. Fortunately, there is a more convenient way. We can retrieve the IP address with a single command using the "–format" flag.

The command executed on the screenshot above would now look like this:

docker inspect --format='{{.NetworkSettings.IPAddress}}' mynginx

Note that this would also work if we replace the container name "mynginx" with the container ID.

From the command above, '{{.NetworkSettings.IPAddress}}' is a Go (Golang) template that specifies what information to extract from the container's network settings. In this case, we are extracting the IP address.

After executing the command, you’ll see the IP address (127.17.0.2) printed in the terminal window, as shown in the output below:

Note: Go templates are used to generate dynamic content by combining layout or structure with actual data. They contain placeholders, called actions, which are enclosed in double curly braces ({{ }}). The placeholders are filled with actual data when the template is executed.

Method 2: Using the docker exec command

The "/etc/hosts" file inside a container is a standard system file that maps hostnames to IP addresses, including the IP address assigned to the container itself. To retrieve this IP address, we can follow two simple steps:

Step 1: Get a shell to our running container

Run the following command to get a shell to the running container.

docker exec -it mynginx /bin/bash

This command runs "/bin/bash" (a command interpreter, also called a shell) inside the "mynginx" container. Next, it attaches your input/output to this Bash shell. So you get an interactive shell session inside the "mynginx" container.

After running this command, you should see the following output:

The output you see (root@9b5f6ec30ef9:/#) is the shell/command prompt of the Bash session running inside the container.

Step 2: Run a command to output the contents of the /etc/hosts file

Now that we have an interactive shell session running, we can use the "cat" command to output the contents of the "/etc/hosts" file. Run the following command on the command prompt of the Bash shell session:

cat /etc/hosts

The execution of the command will result in the following output:

As highlighted above, we have the IP address (172.17.0.2) of the container listed next to its ID.

Before we move to the next section, make sure to exit the container's shell by simply typing the "exit" command or pressing "Ctrl + D". This will bring you back to the command interpreter running on your own computer.

Note: To dive deeper into the "docker exec" command and learn about what the "-it" flag does, make sure to check out our blog post, Docker Exec: How to Enter Into a Docker Container's Shell?

Method 3: Using the docker network inspect Command

We know that the "mynginx" container is connected to the bridge network by default. Let’s inspect this network by running the following command:

docker network inspect bridge

The execution of the command will give you the following output:

The value of the "IPv4Address" field represents both the IP address and the subnet mask of the "mynginx" container. "127.17.0.2" is the IP address and "/16" is the subnet mask. In simple terms, a subnet mask is used to divide an IP network into smaller segments (subnets) to manage IP addresses more efficiently.

Conclusion

In this blog post, we explored three different methods for finding the IP address of a Docker container. In my opinion, the first method (using the "docker inspect" command with the "--format" flag) is the simplest and most straightforward way.

If you're interested in learning more about Docker and how it can help you streamline your development workflow, be sure to check out the following courses from KodeKloud:

  • Docker for the Absolute Beginner: This course will help you understand Docker using lectures and demos. You’ll get a hands-on learning experience and coding exercises that will validate your Docker skills. Additionally, assignments will challenge you to apply your skills in real-life scenarios.
  • Docker Certified Associate Exam Course: This course covers all the required topics from the Docker Certified Associate Exam curriculum. The course offers several opportunities for practice and self-assessment. There are hundreds of research questions in multiple-choice format, practice tests at the end of each section, and multiple mock exams that closely resemble the actual exam pattern.