How to get IP Address of a Running Docker Container

Posted on

Introduction

As in the title of this article, the main content of this article is very clear. It is just to show how to get the IP address of a running docker container. Actually, the main purpose for getting the IP address of a running docker container is to access a certain service. The service can be a web application service running on Apache, Nginx, etc. Other kind of service can also be database service running on MySQL, PostgreSQL, etc. So, it is important to know the IP address of the running service at first. Before getting the IP address of a running docker container, it is important to know all available of the running containers are. The following is the command to display all of it :

C:\Users\Administrator\Downloads>docker image list --all
REPOSITORY                             TAG       IMAGE ID       CREATED        SIZE
                                     0f55d0a99324   7 days ago     231MB
registry                               latest    b8604a3fe854   11 days ago    26.2MB
bash                                   latest    bacfc038bc2c   12 days ago    12.9MB
centos                                 latest    5d0da3dc9764   2 months ago   231MB
hub.docker.local:5000/centos_sinergi   latest    5d0da3dc9764   2 months ago   231MB

C:\Users\Administrator\Downloads>

From the above output command, just choose one image name for the base of the running container after.

 

Get IP Address of a Running Docker Container

As in the previous part, for an example, the base image for the running container will be ‘centos’. In that case, just run the following command to create a new running docker container :

C:\Users\Administrator\Downloads>docker run -it --name mycontainer --mount "type=bind,source=c:\\programming\\django\\project,target=/var/www" -p 80:80 -d centos
44fdcb5a74824baa9b5887877772ccdb41e551305b4d78e36e72fa944744aab5

C:\Users\Administrator\Downloads>

Before running the command to get the IP address of a running docker container, just list the available running docker container first. The following is the execution of the command to be able to do that :

C:\Users\Administrator\Downloads>docker container list --all
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                    NAMES
44fdcb5a7482   centos     "/bin/bash"              13 seconds ago   Up 10 seconds   0.0.0.0:80->80/tcp       mycontainer
8a52dcce139c   registry   "/entrypoint.sh /etc…"   7 days ago       Up 35 hours     0.0.0.0:5000->5000/tcp   hub.local

C:\Users\Administrator\Downloads>docker

After successfully running the above command to create running docker container, just run the following command to get the IP address of the running docker container :

C:\Users\Administrator\Downloads>docker inspect --format "{{ .NetworkSettings.IPAddress }}" mycontainer
172.17.0.3

C:\Users\Administrator\Downloads>

Execute the following command to run in the currently running container to check the IP address as follows :

C:\Users\Administrator\Downloads>docker exec -it mycontainer bash
[root@44fdcb5a7482 /]# ping 172.17.0.3
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.096 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.030 ms
^C
--- 172.17.0.3 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1011ms
rtt min/avg/max/mdev = 0.030/0.063/0.096/0.033 ms
[root@44fdcb5a7482 /]#

By executing the ping command inside the running docker container with its own IP address, it is one of the way to check it. Also, according to the output of the ping command above, it gives a reply.

One thought on “How to get IP Address of a Running Docker Container

Leave a Reply