• Post author:
  • Post category:Docker
  • Post last modified:May 9, 2024
  • Reading time:13 mins read

How to get a Docker Container’s IP address from the host? To get the IP address of a Docker container from the host machine, you can use the docker inspect command. The docker inspect command is a powerful tool in Docker that allows you to retrieve detailed information about Docker objects such as containers, images, volumes, and networks.

Advertisements

Docker is a technology that lets you run applications in isolated containers. This makes it easy to move applications between different environments. In this article, I will show you how to get a container’s IP address from the host using different methods.

Why Knowing the Docker Container IP Address Matters

Before we get started, it’s important to understand why knowing a Docker container’s IP address is important. Docker containers run in isolated environments, which makes them a popular choice for microservices architectures and container orchestration platforms like Kubernetes. In these setups, containers often need to communicate with each other and external services.

Knowing a container’s IP address is helpful for a few reasons:

  • Inter-container communication: Containers within the same network often communicate using IP addresses. Knowing these addresses allows them to send data to each other easily.
  • Configuration management: Sometimes you may need to configure applications running on the host to interact with specific containers. Having the IP address makes this task easier.
  • Debugging and troubleshooting: Troubleshooting network-related issues often requires inspecting container IP addresses.
  • Security policies: Network and security policies may depend on knowing container IP addresses.

Now, let’s look at some methods for getting a container’s IP address from the host.

Method 1: Docker Inspect Command to get Container IP Address

The docker inspect command is used to obtain detailed information about Docker objects, including containers IP addresses. It provides insights into a container’s configuration, networking, and more.

Following is a syntax of the docker inspect command


# Syntax
docker inspect [OPTIONS] OBJECT [OBJECT...]

Here,

  • OPTIONS are optional flags you can use to customize the output or filter the information.
  • OBJECT is the Docker object you want to inspect. It can be a container ID, container name, image ID, image name, volume name, network ID, etc.

For example, let’s assume we have a container named “web_server” for which we want to obtain the IP address. Here’s how you can do it:


# Retrieve the IP address of the web_server container
docker inspect -f 
   '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web_server

In this command:

  • docker inspect retrieves detailed information about the container.
  • -f specifies a Go template for formatting the output.
  • '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' extracts the container’s IP address from the JSON output.
Get Docker Container IP Address from the Host

In this example, the IP address of the “web_server” container is 172.18.0.2.

Method 2: Using the Docker Bridge Network

Docker uses bridge networks to provide connectivity between containers. Each container connected to a bridge network is assigned a unique IP address within that network. To find a Docker container’s IP address from the host, you can inspect the bridge network and identify the specific IP address allocation.

Let’s consider a scenario where we have a container named “database_server.” Following are the steps on how to retrieve its IP address from the Docker container.

Step 1: List Network Interfaces

Begin by listing the network interfaces on your host system, including the Docker bridge network. You can use the ifconfig command on Linux:


# Display network interface configuration
ifconfig

Among the listed network interfaces, you’ll find the Docker bridge network interface, commonly named docker0.

Step 2: Inspect the Docker Bridge Network

Next, inspect the Docker bridge network interface to gather information about the container’s IP address:


# Display network interface configuration for the docker0 bridge
ifconfig docker0

Yields below output.

In this output, the “inet” field displays the IP address of the Docker bridge network. The subnet mask and broadcast address are also provided.

Step 3: Calculate the Container’s IP Address

Now that you know the Docker bridge network’s IP address and subnet mask, you can calculate the IP address of the “database_server” container. Docker assigns IP addresses sequentially, so you can simply increment the last octet of the Docker bridge network’s IP address. In this example, the “database_server” container’s IP address would be 172.17.0.2.

This method is useful when you need to retrieve a container’s IP address without directly invoking the Docker CLI. However, it becomes less practical in environments with numerous containers.

Method 3: Leveraging Docker Network Discovery Tools

For dynamic and automated approaches to discovering container IP addresses, Docker offers network discovery tools and libraries. These tools provide real-time information about container networks and their associated IP addresses.

Docker Compose is a popular tool for defining and running multi-container Docker applications. It automatically creates custom bridge networks for containers, simplifying network configuration. Let’s explore how to retrieve a container’s IP address using Docker Compose:

Step 1: Create a Docker Compose YAML File

Begin by creating a docker-compose.yml file to define your services and their configurations. Here’s a simplified example:


# Docker Compose file version
version: '3'

# Define services
services:
  # Web service using the nginx image
  web:
    image: nginx

  # Database service using the postgres image
  database:
    image: postgres

Step 2: Start Your Services

Launch your services defined in the Docker Compose file:


# Start the Docker Compose services in detached mode (in the background)
docker-compose up -d

Step 3: Retrieve Container IP Addresses

You can use the docker-compose ps command to list the running containers and their IP addresses within the custom bridge network:


# Display the status of Docker Compose services
docker-compose ps

Yields below output.

In this example, the docker-compose ps command displays a table with container names and their respective statuses. You can see the “database” container’s IP address as part of its details.

Docker Compose simplifies managing multiple containers and their network configurations, making it an excellent choice for multi-container applications.

Method 4: Custom User-Defined Networks

Docker allows you to create custom user-defined networks, offering better control over container networking and IP address assignment. This approach is particularly useful when you want to isolate containers in their own networks and manage IP addresses more precisely.

Let’s explore this method and get the IP address from the container.

Step 1: Create a Custom User-Defined Network

Start by creating a custom user-defined network using the docker network create command. Provide a meaningful name for your network:


# Create a Docker network named my_network
docker network create my_network
 

Step 2: Run Containers in the Custom Network

When starting your containers, connect them to the custom user-defined network you created. This isolates them from the default bridge network and allows for precise control over networking.


# Start the app_container in detached mode and connect it to the my_network with the my_app_image
docker run -d --name app_container --network my_network my_app_image

# Start the db_container in detached mode and connect it to the my_network with the my_db_image
docker run -d --name db_container --network my_network my_db_image
 

In this example, we’ve started two containers, “app_container” and “db_container,” and connected them to the “my_network” custom network.

Step 3: Retrieve Container IP Addresses

To obtain the IP addresses of containers connected to the custom network, you can use the docker network inspect command. Replace <network_name> with the name of your custom network:


# Inspect the my_network Docker network and display information about connected containers
docker network inspect -f '{{range .Containers}}{{.Name}}: {{.IPv4Address}}{{end}}' my_network
 

Yields below output.

Get Docker Container IP Address from the Host

In this output, you can see the container names and their corresponding IP addresses within the “my_network” custom network.

Custom user-defined networks offer a structured approach to container networking, simplifying the retrieval of IP addresses, especially in complex setups with multiple containers.

Conclusion

In the world of Docker, retrieving a container’s IP address from the host is a fundamental skill that every Docker practitioner should possess. Here, I’ve demonstrated various methods to achieve this goal, from the straightforward docker inspect command to more advanced techniques involving Custom User-Defined Networks.

The docker inspect command provides a JSON-formatted output that contains a wealth of information about the specified Docker object. This information can be useful for troubleshooting, debugging, or scripting tasks.