How to get a Docker container's IP address from the host?

If you are working with Docker for a long time now, you might already have lots of containers running in your host machine. At times, it becomes really difficult to keep track of all these containers. Moreover, if you are on a network or using compose, there might be several containers running inside the network. In such a case, determining which container is actively running and which has failed, is very difficult. You might need to ping these containers periodically to check their status. For this, you need to have the IP addresses of the containers.

You can easily get the IP address of any container if you have the name or ID of the container. You can get the container names using the docker ps -a command. This will list all the existing containers. There are several methods to get the IP address of a container from the host machine. In this article, we will discuss some easy and frequently used methods to do so.

Method 1 − Using the Inspect Command with Format Option

The first method is using the Docker inspect command to inspect containers and using the format option to print out the IP address of the container only. Let's check out how to do so.

$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container-ID>

This command uses the -f flag which stands for --format and uses a Go template to print only the IP of the container. You need to specify the container name or ID at the end.

Method 2 − Using the GREP Pipe with Inspect Command

Another variant of the above Docker inspect command can be used instead of using the format option. You can simply pipe the output of the Docker inspect command with the pattern matching grep command with the pattern "IPAddress". Let's see how to do so.

$ docker inspect myubuntu | grep "IPAddress"

This command will print the IP address of the container successfully, along with additional network information in JSON format.

Method 3 − Getting IP Address of All Containers

If you want to print the IP addresses of all the containers along with their container names, you can use the same Docker inspect command with the format option. But instead of the container name, you can include a sub-command which returns the container IDs of all the existing containers.

$ docker inspect -f '{{.Name}}-{{.NetworkSettings.IPAddress}}' $(docker ps -aq)

The sub-command docker ps -aq lists the container IDs of all the containers. This is passed as an argument to the parent Docker inspect command.

Method 4 − Listing IP Addresses in Docker Compose Networks

If you are using Docker compose, you can use the following command to list the IP addresses of all the existing containers in your compose network. This method works for containers with multiple network interfaces.

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

Method 5 − Creating a Bash Function

You can create a bash function to simplify getting container IP addresses. Add the following snippet to your ~/.bashrc file.

docker-ip() {
   docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$@"
}

After sourcing your bashrc file, you can use the following command to get any container's IP address.

$ docker-ip <container-name>

Method 6 − Listing IPs in Tabular Format

If you want to list the IP addresses in a sorted tabular format, use the command mentioned below.

$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} %tab% {{.Name}}' $(docker ps -aq) | sed 's#%tab%#\t#g' | sed 's#/##g' | sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n

This command formats the output with tabs, removes forward slashes from container names, and sorts the results by IP address numerically.

Comparison of Methods

Method Use Case Output Format Multiple Networks
Docker inspect with format Single container IP IP only Yes
Inspect with grep Single container with details JSON snippet Yes
All containers Multiple containers Name-IP pairs Limited
Compose networks Docker compose setups Name-IP pairs Yes
Bash function Frequent usage IP only Limited
Tabular format Organized listing Sorted table Yes

Conclusion

Getting Docker container IP addresses is essential for network troubleshooting and container management. The Docker inspect command with various formatting options provides flexible ways to retrieve this information, whether for single containers or entire networks. Choose the method that best fits your specific use case and workflow requirements.

Updated on: 2026-03-17T09:01:38+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements