JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Get the IP address of the Docker container

Author:JIYIK Last Updated:2025/03/25 Views:

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/16and in the order they were created 172.17.0.2/16.

docker inspectGet the IP address of the Docker container using

We can also docker inspectfind 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.

Article URL:

Related Articles

Docker deploys nginx php application

Publish Date:2025/03/26 Views:131 Category:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

Publish Date:2025/03/26 Views:97 Category:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

Publish Date:2025/03/26 Views:125 Category:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

Publish Date:2025/03/26 Views:202 Category:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

Publish Date:2025/03/26 Views:88 Category:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

Publish Date:2025/03/26 Views:140 Category:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial