- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 along 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.
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 of all Containers in a Docker Compose
If you are using Docker compose, you can use the following commands to list the IP addresses of all the existing containers in your compose network.
docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
Method 5. Listing IP Addresses using a Bash Script
You can also use a bash script to create a function that will list the IP addresses of all the containers. Just add the below snippet in your ~/.bashrc file.
docker-ip() { docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$@" }
Now, if you want to get the IP address, you can use the following command inside your terminal.
$ docker-ip <container-name>
Method 6. Listing IPs in tabular format
If you want to list the IP addresses in a 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
To sum up, in this article, we discussed how to retrieve the IP addresses of one or all containers in a host machine or a compose network using 6 different methods. You can use any of these as per your requirements.
- Related Articles
- How to get a Docker Container IP address?
- Copying files from Docker container to Host
- How to copy files from host to Docker container?
- How to get the Android Emulator's IP address?
- How to get the Android Emulator's IP address using Kotlin?
- How do I get into a Docker container's shell?
- Display the Host Name and IP Address on a Tkinter Window
- How to get the ip address in C#?
- How to get IP address settings using PowerShell?
- How can we get the client's IP address in ASP.NET MVC C#?
- How to get the IP address of android device programmatically?
- Python program to remove leading 0's from an IP address
- Java program to Get IP address of the system
- Connecting From Docker Containers to Resources in Host
- How to get current Wi-Fi IP address in android?
