In this blog post, we’ll explore various methods for finding the IP address of a Docker container that was started with the default container networking. When we say that a container was started with the default container networking, it means that the container is using the built-in Docker networking functionality to communicate with other containers and the host system.
Understanding Docker’s Default Container Networking
When you create a container in Docker, it is connected to a Docker network. By default, the container is connected to the default "bridge" network, assuming that you created the container without specifying a network.
The bridge network is a built-in network that enables containers to communicate with each other and with the host system. It assigns a unique IP address to the container from a pool of available IP addresses.
To follow along with the examples in this post, we recommend using KodeKloud’s Docker playground. This playground will provide you instant access to a Docker environment where you can run and manage containers. No need for you to install any software. With just one click, you'll be ready to start working with containers.
Run a Docker Container
Before we can find the IP address of a Docker container, we must first create a container. In this example, we will use the "nginx" image to run a simple web server.
Open up your terminal and run the following command:
docker container run -d --name=mynginx nginx
The execution of the command will return the following output:
You’ll see a long string (highlighted in the screenshot above) printed in the terminal window. It’s the container ID.
Next, let’s run the "docker ps" command to confirm that the container is up and running.
As you can see, the "mynginx" container is up and running as expected.
Do you remember our discussion about the default "bridge" network that a container is connected to by default? We can easily view this network by listing the available networks configured in our Docker environment. To do so, run the following command:
docker network ls
The execution of the command will give you the following output:
We see three networks that are provided by Docker, out of the box. Among them, the network named "bridge" is the default network.
Now that we have a Docker container up and running, let's take a look at how we can find its IP address.
3 Methods to Find a Docker Container’s IP Address
There are many ways to find the IP addresses of a Docker container. In this section, we will explore three commonly used methods.
Method 1: Using the docker inspect Command
We can use the "docker inspect" command to retrieve detailed information about a Docker container, including its IP address. Replace the container name or container ID in either of these two commands. Run the command with your container name or ID:
docker inspect <CONTAINER NAME>
docker inspect <CONTAINER ID>
After executing the command, you’ll see an output like this: