Docker

How to Get a Docker Container’s IP Address from the Host on Windows

While working with Docker, developers create and run a lot of containers in their host machine. Sometimes, it becomes really hard for developers to keep track of all the existing containers. Additionally, if you are using compose or network, there might be multiple containers executing inside the network.

If the container is executed with the default container networking. It means that the container is utilizing the built-in functionality of Docker networking for communicating with other containers and the host system. To determine which container is executing and which has stopped, developers should have the IP addresses of these containers.

This guide will provide different commands for getting the IP address of the Docker container from the host on Windows.

How to the IP Address of the Docker Container from the Host on Windows?

To find the IP address of the Docker container from the host on Windows, the below-listed commands are used:

Using “docker network inspect”

To get the IP address of the Docker container, first, list and select the Docker image for generating a new container by executing the following command:

docker images

 

From the below-given output, we have selected the elic Docker image:

Then, use the docker container run command to create a new Docker container using the selected image:

docker container run -d --name=vital_con1 elic

 

As you can see, our new container has been created successfully:

Next, list currently running containers through the docker ps command for verification:

docker ps

 

Finally, execute the docker inspect command to display the IP address of the Docker container:

docker inspect vital_con1

 

After running the above-given command, it will display the detailed information about the particular container:

Scroll down the terminal and find the “IPAddress” field. The IP address of the currently working container named vital_con1 is 172.17.0.3:

Using “docker exec”

The /etc/hosts file inside a container is a system file that is used for mapping the hostnames to the IP addresses including the IP address assigned to the container itself. To retrieve this IP address, run the docker exec -it command.

docker exec -it vital_con1 /bin/bash

 

After doing so, you will be moved to the interactive shell session. Now, execute the cat command to show the available data of the /etc/hosts file:

cat /etc/hosts

 

As you can see, the IP address of the particular container has been retrieved successfully:

Using “docker network inspect”

As we know, by default containers are connected to the bridge network. We can inspect the network to find out the IP address of the container by executing the docker network inspect <network-name> command. To do so, first, run the provided command to display existing networks:

docker network ls

 

Here, we have selected the bridge network for further process:

Now, run the below-stated command along with the network name as bridge:

docker network inspect bridge

 

After executing the above command, scroll down to the Containers block and locate the IPV4Address field:

We have provided the easiest way to find the Docker container’s IP address from the host on Windows.

Conclusion

There are different commands that are used to get a Docker container’s IP address from the host on Windows, such as docker network inspect <container-id>, docker exec -it <container-name> /bin/bash, and the docker network inspect bridge commands. This blog illustrated the different ways to retrieve the IP address of the Docker container from host.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.