Get the IP address of the Docker container
This article demonstrates how to get the IP address of a Docker container.
Connect to the Bridge network and get the IP address of the Docker container
One of the big reasons why docker containers are so convenient is that we can easily connect them. This allows containers to communicate and share resources easily.
We can also connect containers to non-docker workloads.
This article is not about docker networking; hence, we will not delve into the details of docker networking. However, docker provides various drivers that make networking pluggable using a bridge driver.
Docker allows us to create networks using its default driver, called the bridge driver. However, bridge networks are private and their scope is limited to containers on the host.
You can view the default network on the host and the networks you previously had. Each docker installation usually includes three default networks as shown below.
isaac@DESKTOP-HV44HT6:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
9729860fa596 bridge bridge local
0667c3f7d0f0 host host local
c3273b158256 none null local
Unless otherwise specified, Docker will always start new containers in a bridge network.
Container IP addresses are an important part of networking docker containers. Typically, containers are assigned an IP address for each network they are connected to.
On the other hand, you can also manually connect the docker container to the bridge network using the following command.
$ docker run -dt rabbitmq
This will create a docker container and assign it to the bridge network. Before we can check the network to confirm that it is connected to it, we should first make sure the container is running.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
42487cad0390 rabbitmq "docker-entrypoint.s…" About a minute ago Up About a minute 4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp compassionate_keller
Inspecting the bridge network will allow us to see the containers connected to the network, along with other details such as the container’s IP address and the container’s default subnet. The details are returned in JSON format as shown below.
~$ docker network inspect bridge
Output:
"ConfigOnly": false,
"Containers": {
"42487cad0390a8de6d1a88bc1d6c09ffdf3162dc85d4d5d3dc70200b2348b673": {
"Name": "compassionate_keller",
"EndpointID": "ffa55232565d3a32d1e471d0753c1f491d6131a5cca50b8b76bc43a8ff554e32",
"MacAddress": "02:42:ac:11:00:04",
"IPv4Address": "172.17.0.4/16",
"IPv6Address": ""
}
In the above example, the IP address of our container is 172.17.0.4/16.
We can also have multiple containers connected to the network, and we can easily find the IP addresses of both by checking the bridge network. As shown below, we add a container based on the Nginx image to the bridge network.
isaac@DESKTOP-HV44HT6:~$ docker run -dt nginx
4ab752ab92582a0eb2cb14475094460fc8cc608c93a357a8dca082cfea2bc368
Now if we check the bridge network, we will be able to get the IP addresses of both containers. Remember, both containers should be running.
isaac@DESKTOP-HV44HT6:~$ docker network inspect bridge
Output:
"Containers": {
"42487cad0390a8de6d1a88bc1d6c09ffdf3162dc85d4d5d3dc70200b2348b673": {
"Name": "compassionate_keller",
"EndpointID": "ffa55232565d3a32d1e471d0753c1f491d6131a5cca50b8b76bc43a8ff554e32",
"MacAddress": "02:42:ac:11:00:04",
"IPv4Address": "172.17.0.4/16",
"IPv6Address": ""
},
"4ab752ab92582a0eb2cb14475094460fc8cc608c93a357a8dca082cfea2bc368": {
"Name": "trusting_keller",
"EndpointID": "a2babd11586f493cf5e57f5d9920a7f5648cf6163e77030521bef62fc9f34a63",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
}
The IP addresses of the two containers are 172.17.0.4/16
and in the order they were created 172.17.0.2/16
.
docker inspect
Get the IP address of the Docker container
using
We can also docker inspect
find the IP address of a specific Docker container using and either the container name or the container id. This will also return a lot of other details in JSON format.
$ docker inspect 42487cad0390
Output:
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "9729860fa5961eeac55f921ee787b2c82a15146cd36117b5394243be2149e929",
"EndpointID": "ffa55232565d3a32d1e471d0753c1f491d6131a5cca50b8b76bc43a8ff554e32",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:04",
"DriverOpts": null
}
}
In this case, we have checked the container based on the official Nginx image. You can refer to the Docker documentation for interesting stuff about networking Docker containers.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Copy files from host to Docker container
Publish Date:2025/03/25 Views:126 Category:Docker
-
This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest
Tagging images with Docker and Docker Compose
Publish Date:2025/03/25 Views:70 Category:Docker
-
When creating a Docker image, it is best practice to give it a descriptive and meaningful name. This process makes identifying and managing images more manageable, especially when dealing with many images. This article discusses tagging ima
Creating a database user with Docker Postgres
Publish Date:2025/03/24 Views:166 Category:Docker
-
When developing applications, we usually use database management systems such as PostgreSQL, MySQL, MongoDB, etc. to record application data. Docker helps us run an instance of these application database management systems. This helps save
Adding unsafe registry keys in Docker
Publish Date:2025/03/24 Views:130 Category:Docker
-
While it is highly recommended to secure your registry using a Transport Layer Security (TLS) certificate issued by a known Certificate Authority (CA), we have the option of using our insecure registry over an unencrypted Hypertext Transfer
List all images in Docker Registry V2
Publish Date:2025/03/24 Views:131 Category:Docker
-
After several iterations, Docker Registry was upgraded from version 1 to version 2. Especially being new, some commands needed to be included or adequately documented on its official documentation website. An example is getting a list of im
Executing multiple commands in Docker-Compose
Publish Date:2025/03/24 Views:152 Category:Docker
-
Docker makes it easier for developers to build, test, and deploy applications without worrying about dependencies by packaging them in standardized units called containers. Docker-compose is an advanced, must-have tool for managing multi-co
View logs for a specific Docker Compose service
Publish Date:2025/03/24 Views:60 Category:Docker
-
When using docker-compose up, we can see the logs of all containers in the YAML file; however, if we specify a specific container service, the output will not show any service dependencies in the log. Therefore, this article will explore ho
Differences between Docker and Docker Compose
Publish Date:2025/03/24 Views:147 Category:Docker
-
Both docker and docker compose docker run Docker containers. The two Docker constructs are comparable, but that's about it. This article will discuss docker compose the main differences between docker and . Differences between Docker and Do
Recreate the container from the new image using Docker-Compose
Publish Date:2025/03/24 Views:178 Category:Docker
-
As we develop our applications, we often make changes or add more features to make the application more effective for the different users who interact with the application. When using Docker, we need to ensure that the changes or features w