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:

By default, the command shows all running containers as the following:

We can show all containers by adding the -a option for the command:

We can filter by using | operator:

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:

Let’s see an example which we will get IP address of a Docker container which has ID 4a432e03f0c3:

With old version of Docker, we can use the following syntax:

Let’s see an example:

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:

Here is the output on my environment:

With old version of Docker, we can use the following syntax:

The output can be:

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:

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:

For example, let’s get in a docker container which sas id: 4a432e03f0c3

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:

or

or

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

Install Docker on CentOS 7.X

Introduction to Docker Compose

Vagrant Docker Provider Tutorial

Using Apache Kafka Docker

Insecure docker registry

 

5 1 vote
Article Rating