This quick tutorial is going to cover how to get Docker container’s IP address from host machine or inside a Docker container.
1. Get ID or Name of a Docker Container
We’re going to review how to get ID or name of a Docker container first because they’re prerequisite to get Docker container’s IP address. You can refer to another tutorial for more basic Docker commands.
To get ID or name of a Docker container, we can issue the following command from the host machine:
1 |
docker ps |
By default, the command shows all running containers as the following:
1 2 3 |
CONTAINER ID IMAGE CREATED STATUS PORTS NAMES 260f108e4271 wurstmeister/kafka 2 minutes ago Up 2 minutes tender_gates 4a432e03f0c3 wurstmeister/zookeeper 3 minutes ago Up 3 minutes 22/tcp naughty_mestorf |
We can show all containers by adding the -a option for the command:
1 |
docker ps -a |
We can filter by using | operator:
1 |
docker ps -a | grep kafka |
After getting the ID or name of the Docker container, we can go ahead to get its IP address.
2. Get Docker Container’s IP Address from Host Machine
2.1. Get IP Address of a Single Docker Container
The command can be used to get Docker container’s IP address is docker inspect with some options:
1 |
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_id_or_name |
Let’s see an example which we will get IP address of a Docker container which has ID 4a432e03f0c3:
1 2 |
$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 4a432e03f0c3 172.17.0.2 |
With old version of Docker, we can use the following syntax:
1 |
docker inspect --format '{{ .NetworkSettings.IPAddress }}' container_id_or_name |
Let’s see an example:
1 2 |
vagrant@geeksgn:~$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' 4a432e03f0c3 172.17.0.2 |
2.2. Get IP Addresses of All Docker Containers
We can tune the above commands to get IP addresses of all Docker containers in just one single command:
1 |
docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq) |
Here is the output on my environment:
1 2 3 4 |
$ docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq) /mad_visvesvaraya - 172.17.0.3 /naughty_mestorf - 172.17.0.2 /grave_keller - |
With old version of Docker, we can use the following syntax:
1 |
docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq) |
The output can be:
1 2 3 4 |
$ docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq) /mad_visvesvaraya - 172.17.0.3 /naughty_mestorf - 172.17.0.2 /grave_keller - |
Another command that can be used to get Docker IP address is docker container inspect. In similar to the docker inspect command, it is mainly used for displaying detailed information on one or more containers. However, we can leverage it to get Docker IP address.
For example, let’s get IP addresses of all Docker containers:
1 |
docker container inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq) |
3. Get IP Address of a Docker Container from Inside Container
3.1. Get in a Docker Container
To get IP address of a Docker container from inside, we can get in it first by using the docker exec command:
1 |
docker exec -it container_id_or_name bash |
For example, let’s get in a docker container which sas id: 4a432e03f0c3
1 |
docker exec -it 4a432e03f0c3 bash |
3.3. Get IP Address of a Docker Container
After getting in the docker container, we can issue normal Linux command to get Docker container’s IP address, for example:
1 |
ip addr |
or
1 2 |
ip addr | grep global inet 172.17.0.2/16 scope global eth0 |
or
1 |
/sbin/ip route|awk '/default/ { print $3 }' |
4. Conclusion
The tutorial has illustrated us how to get IP address of a Docker container from host machine or inside a Docker container. The prerequisite is to obtain the Docker container name or ID, then using the docker inspect or docker container inspect commands from host or get in a Docker container to obtain its IP address.
Below are other related tutorials for your references:
How To Remove Docker Images, Containers, Volumes, and Networks
Copy Files, Folders from Host to Docker Container and Vice Versa
Install Docker on Ubuntu 16.04, 15.10, 14.04 Step By Step
Introduction to Docker Compose
Vagrant Docker Provider Tutorial