How to get a Docker Container IP address?

DockerOperating SystemOpen Source

We all know that we can run our application in a packaged environment called container using Docker. When you want containers to talk to each other, the network they create can be assumed to be a bridge network. Run the following command to get a list of networks.

sudo docker network ls

Each network of containers has a subnet mask and can be used to distribute IP addresses to its containers. This also means that each container in the docker network is assigned an IP address. The default subnet for a docker network is

Knowing these, we will now see the different methods that can be used to find out the IP address of a docker container in a network.

  • Using Docker inspect

Inspect command is used to get low level information about all the running docker containers in the host machine. It is shown in JSON format. We can use the format option with the command to get only a handful of important information.

Use the following commands below to get the container IP address using inspect.

sudo docker ps −a
sudo docker inspect −−format '{{ .NetworkSettings.IPAddress }}' <CONTAINER_ID>

First command is used to get a list of container IDs of all the running containers. This can be used on the second command to find the IP addresses.

  • In case of a single container

In case you only have a single container running, you can directly return the last container ID by parsing it

sudo docker inspect −−format '{{ .NetworkSettings.IPAddress }}' $(docker ps −q)
  • Using the bash

You can also get a container’s network ID by attaching a bash shell to the container. Use the commands below.

To start the bash of the container, use −

sudo docker exec −it <CONTAINER_ID> bash

Once you are inside the bash, you can use the following command to access the IP address.

apt−get −y update
apt−get install iproute2
ip addr | grep global
  • Exporting the environment variable of the container

For a particular docker container, you can export the environment variable. The following command returns the IP address of the last running container.

sudo docker exec −it $(docker ps −l −q) env | grep ADDR
  • Using the IP address command

You can also access the IP address of the container by running the IP address command. Check out the command below.

sudo docker exec −it <container−id> ip addr | grep global
  • Using the bashrc file

You can access the IP address of a particular container by creating your own custom command. You just need to add a method inside your bashrc file that would take an argument which will be the container ID and then return the IP address of that container. Check out the command below.

containerip() {
   sudo docker inspect −−format '{{ .NetworkSettings.IPAddress }}' "$@"

Paste the above code at the end of your ∽/.bashrc file.

Reload the file by using the command −

source ∽/.bashrc

Now, get the container ID using the following command.

sudo docker ps −a

Copy the container ID and use it in the following command to get the container’s IP address.

sudo containerip <CONTAINER_ID>

In this article, we have seen six different ways to get a docker container’s IP address. However, the best and probably the easiest one is using the inspect command and this makes it one of the most widely used one too.

Published on 27-Oct-2020 12:06:56