Docker containers are one of the essential components of Docker that are used to encapsulate software and services along with their dependencies. These containers are usually used to deliver and test the services and applications.
Some services or applications are interdependent on other services. When these services are executed in different containers, they must communicate to share data. For example, any web service that usually needs to fetch or store data in a database that is running as a service in a different container. So, there must be a network that enables the communication between these containers. IP addresses are the one that enables the communication between two containers.
In this blog, we will demonstrate:
- Prerequisite: Create and Start Docker Container
- Find the IP Address of Docker Container Using Docker Inspect
- Find the IP Address of Docker Container Using Docker Network Inspect
- Find the IP Address of Docker Container Using Docker Exec
- Find the IP Address of Docker Container Using Container Interactive Shell
Prerequisite: Create and Start Docker Container
To create the container, first create the container’s snapshot or image. Then, execute the image and fire up the container. For illustration, follow the instructions given below.
Step 1: Make a Program File
First, create a program file named “index.html” and add the below snippet into the file:
background-color:rgb(9, 4, 4);
color:rgb(221, 219, 226);
<h1> This is First HTML page </h1>
Step 2: Create Dockerfile
Next, create a new file named “Dockerfile” and add the given commands into the file:
COPY index.html /usr/share/nginx/html/index.html
ENTRYPOINT ["nginx", "-g", "daemon off;"]
In the given snippet:
- “FROM” command defines the container base image.
- “COPY” command copies the source file to the container’s specified path.
- “ENTRYPOINT” command defines the container executables.
Step 3: Build Docker Image
Generate the container snapshot through the below command. Here, the “-t” option is used to tag the image:
Step 4: Create and Start Docker Container
Now, build and fire up the container by running the container’s image using the given command. Here, the “-p” option specifies the container’s exposed port, the “–name” option sets the name, and “-d” runs the container as a backend service:
To check if the container is executing or not, list the running containers using the “docker ps” command:
The below output shows that “demo-cont” is executing and accessible on port “80”:
Now, navigate to “http://localhost:80” in the browser and check whether the container is executing on the exposing port or not. The output shows that the container is successfully running on localhost port 80:
After executing the container, use any of the below-given methods to find the IP address of the running container.
Method 1: Find the IP Address of Docker Container Using Docker Inspect
IP Addresses are referred to as internet protocol addresses that are used for communication through internet protocol. These addresses are used for network identification and local addressing.
To check the IP address of the container in Docker, the user can inspect the container. The “docker inspect” command shows detailed information about the running container and it also includes networking information.
To find the IP address by inspecting the container, utilize the “docker inspect <container-name/container-id>” command.
Scroll down to the network section. Here, the user will find the “IPAddress” as highlighted below:
The “docker inspect” command shows the detailed information. In order to filter only the IP address from the container, utilize the “–format” option as done below:
Here, we have directly accessed the “demo-cont” container’s IP address:
Method 2: Find the IP Address of Docker Container Using Docker Network Inspect
Another possible way to find the container’s IP Address in Docker is to inspect the network that is used by the container. By default, containers are usually using the Docker “bridge” network.
To find out the IP Address of the container by inspecting the network, go through the following instructions.
Step 1: List All Docker Networks
To list down all Docker networks, utilize the “docker network ls” command:
Here, the below output shows all the Docker networks. For demonstration, we will inspect the “bridge” network because, by default, containers use the “bridge” network on Docker:
Step 2: Inspect the Network
Now, inspect the network using the network ID or network name. For this purpose, use the following command:
Here, from the “Containers” key, the user can view the containers that are using the bridge network along with the IP Address as shown below:
Method 3: Find the IP Address of Docker Container Using Docker Exec
By default, container network information is stored in the “/etc/hosts” file of the container. To check out the container’s IP address, read the “/etc/hosts” file. To non-interactively access the container’s file, use the “docker exec <container-name/container-id> cat /etc/hosts” command:
Here, we have successfully accessed the network information and IP Address of “demo-cont”:
Method 4: Find the IP Address of Docker Container Using Container Interactive Shell
In Linux, users can access the IP Address using the “ip a” command. To find the IP Address of the Docker container by executing the “ip a” command inside the container’s interactive shell, go through the following steps.
Step 1: Open Container Interactive Shell
To start the interactive shell of the container, use the “docker exec -it <container-name/container-id> sh” command. Here, “-it” is utilized to interactively access the container’s “TTY” terminal:
Now, check the IP address of the container using the “ip a” command:
The below output shows that error that the “ip” command is not found in the container:
To resolve the above-given error, follow the below steps.
Step 2: Update APT Repository
Now, update the APT repository of the container using the “apt update” command:
Step 3: Install “iproute2” Utility
Install the “iproute2” package that is used to monitor and control networks. For this purpose, use the “apt install iproute2” command:
Step 4: Check IP Address of the Container
After completing the installation process, again run the “ip a” command to check the container’s IP address:
Here, the below result indicates that we have accessed the container’s address successfully which is highlighted below:
This is all about finding the IP address of a Docker container.
To find the IP address of the container, the user can either inspect the container using the “docker inspect <container-name>” command, inspect the network that is used by the container using the “docker network inspect <network-name>” command, or access the container “/etc/hosts” file that contains all the networking information using “docker exec <container-name/container-id> cat /etc/hosts” command. This blog has demonstrated how to check the container’s IP address.