Enhancing Skills

Discovering the Docker IP Address: A Step-by-Step Guide

In this article, we will explore how to find the IP address(es) of a running Docker container. You will learn various methods to identify the container’s IP address(es) depending on whether it is connected to a bridge network or an overlay network. We’ll also discuss how to handle situations where the Docker daemon is not running or when using cloud platforms like AWS, GCP, and Azure.

Table of Contents:

  1. Introduction
  2. Finding the IP Address(es) of a Docker Container on Localhost
  3. Locating the IP Addresse(es)for Bridged Networked Containers
  4. Identifying the IP Address(es) for Overlay Network Connected Containers
  5. Identifying the IP Address(es) by Network
  6. View IPs and distict list for firewall
  7. Troubleshooting: Starting the Docker Daemon
  8. Handling Cloud Platforms: AWS, GCP, and Azure
  9. Conclusion

Section 1: Introduction

Docker containers are isolated environments that run applications with all their dependencies. They often need to communicate with each other or access external resources like databases or APIs. To facilitate this communication, Docker assigns an IP address(es)to each container. In this article, we will explore various methods to find the IP addresse(es)of a running Docker container on localhost and in cloud environments.

Section 2: Finding the IP Address of a Docker Container on Localhost

To find the IP address(es)of a container running on localhost, follow these steps:

  1. List all running containers with their respective IDs and names using the command:
docker ps
  1. Find the container’s ID or name that you want to investigate.
  2. Use the inspect command to retrieve detailed information about the container, including its IP address(es). For example:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container-name-or-id>

Replace <container-name-or-id> with the actual container ID or name. This command will output the IP address(es) assigned to the specified container.

Section 3: Locating the IP Address for Bridged Networked Containers

Bridged network containers are connected to a virtual network interface that is separate from the host system’s network. To find their IP addresses, follow these steps:

  1. List all running containers and their respective IDs and names using the command:
docker ps
  1. Find the container’s ID or name that you want to investigate.
  2. Use the inspect command to retrieve detailed information about the container, including its IP address within the bridged network. For example:
docker inspect -f '{{range .NetworkSettings.BridgeConfig.IPAddress}}{{.}}{{end}}' <container-name-or-id>

Replace <container-name-or-id> with the actual container ID or name. This command will output the IP address assigned to the specified container in the bridged network.

Section 4: Identifying the IP Address for Overlay Network Connected Containers

Overlay network containers are connected to a private overlay network managed by Docker. To find their IP addresses, follow these steps:

  1. List all running containers and their respective IDs and names using the command:
docker ps
  1. Find the container’s ID or name that you want to investigate.
  2. Use the inspect command to retrieve detailed information about the container, including its IP address within the overlay network. For example:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'  <container-name-or-id>

Replace <container-name-or-id> with the actual container ID or name. This command will output

To iterate over all running containers and find their respective IP addresses, you can use a simple shell script. Here’s an example of a Bash script that fetches the IP address for each container in the output:

#!/bin/bash

# Get the list of running containers
CONTAINERS=$(docker ps)

# Print the header row
echo "Container ID | Container Name | Network | IP Address"

# Iterate over each container in the output
while read -r line; do
  CONTAINER_ID=$(echo $line | awk '{print $1}')
  CONTAINER_NAME=$(echo $line | awk '{print $NF}')
  
  # Skip empty lines
  if [[ -z "$CONTAINER_ID" ]]; then
    continue
  fi
  
  # Get the IP address using the 'inspect' command with 'jq' filter
  IP_ADDRESS=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER_ID)
  
  # Print the container information along with its IP address
  echo "$CONTAINER_ID | $CONTAINER_NAME | $(echo $line | grep -oP 'Networks\K: \K.*') | $IP_ADDRESS"
done <<< "$CONTAINERS"

To execute this script, save it as a file (e.g., list_containers_ip.sh), give it executable permissions using:

chmod +x list_containers_ip.sh

, and then run it with ./list_containers_ip.sh

./list_containers_ip.sh

Using inspect:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}, {{end}} %tab% {{.Name}}' $(docker ps -aq) | sed 's#%tab%#, #g'

Please note that you may need to install the jq tool if it isn’t already installed on your system. You can install jq using the package manager appropriate for your operating system, such as apt-get, brew, or yum.

Section 5: Identifying the IP Address(es) by Network

Sometimes, you may need to identify the IP address of a Docker container within a specific network. For instance, if you’re working with the default bridge network, you can use the following command to find the IP address assigned to the container:

sudo docker inspect bridge | grep -oP '(?<=IPv4Address).+'

This command will output the IP address of the bridge network that the container is connected to. Replace bridge with any other custom network name you might be using, and use the same command format:

sudo docker network ls
sudo docker inspect <custom-network> | grep -oP '(?<=IPv4Address).+'

Replace <custom-network> with your desired network name. This command will output the IP address of that specific custom network assigned to the container.

Section 6: View IPs and distict list for firewall

Create script: docker_firewall_ips.sh

#!/bin/bash

# Get the list of running containers
CONTAINERS=$(docker ps --format '{{.ID}} {{.Names}}')

# Print the header row
echo "Container ID | Container Name | Network | IP Address"

# Declare an array to hold all IP addresses
declare -a ALL_IP_ADDRESSES

# Iterate over each container in the output
while read -r line; do
  CONTAINER_ID=$(echo $line | awk '{print $1}')
  CONTAINER_NAME=$(echo $line | awk '{print $2}')

  # Skip empty lines
  if [[ -z "$CONTAINER_ID" ]]; then
    continue
  fi

  # Get the IP addresses and network names using the 'inspect' command
  IP_ADDRESSES=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}, {{end}}' $CONTAINER_ID)
  NETWORK_NAMES=$(docker inspect -f '{{range $k, $v := .NetworkSettings.Networks}}{{$k}}, {{end}}' $CONTAINER_ID)

  # Remove trailing comma and space
  IP_ADDRESSES=${IP_ADDRESSES%, }
  NETWORK_NAMES=${NETWORK_NAMES%, }

  # Add IP addresses to the array
  IFS=', ' read -r -a ADDR_ARRAY <<< "$IP_ADDRESSES"
  ALL_IP_ADDRESSES+=("${ADDR_ARRAY[@]}")

  # Print the container information along with its IP address
  echo "$CONTAINER_ID | $CONTAINER_NAME | $NETWORK_NAMES | $IP_ADDRESSES"
done <<< "$CONTAINERS"

# Function to replace the last node of an IP address with "xxx"
replace_last_node() {
  local ip="$1"
  local ip_prefix="${ip%.*}"
  echo "$ip_prefix.xxx"
}

# Use an associative array (like a set) to keep track of unique IPs
declare -A UNIQUE_IPS

# Transform the IP addresses and store them in the associative array
for ip in "${ALL_IP_ADDRESSES[@]}"; do
  transformed_ip=$(replace_last_node "$ip")
  UNIQUE_IPS["$transformed_ip"]=1
done

# Print the distinct list of IP addresses with the last node replaced
echo
echo "Distinct IP Addresses (with last node replaced):"
for ip in "${!UNIQUE_IPS[@]}"; do
  echo "$ip"
done | sort

Exectue docker_firewall_ips.sh

chmode 775 docker_firewall_ips.sh
./docker_firewall_ips.sh
View docker_firewall_ips.sh output

./docker_list_containers_ip_distinct.sh
Container ID | Container Name | Network | IP Address
f218d29f0511 | whoami-lan-qbytesworld-com | priv-net | 192.168.48.3
ca22ab290ed4 | catapp-lan-qbytesworld-com | priv-net | 192.168.48.44
cbd3024ad0e | tasks-agent | priv-net| 192.168.48.7
3b2045a14370 | portainer-lan-qbytesworld-com | priv-net| 192.168.48.6
e18cb6a5222f | phpmyadmin-lan-qbytesworld-com | lan2database, traefik4lan | 192.168.64.2, 192.168.48.5

Distinct IP Addresses (with last node replaced):
192.168.48.xxx
192.168.64.xxx

Section 7: Troubleshooting: Starting the Docker Daemon

If you’re unable to find a container’s IP address due to an issue with the Docker daemon, follow these steps to troubleshoot and start the daemon:

  1. Check if the Docker daemon is running by using the command:
sudo systemctl status docker
  1. If the daemon is not running, start it with the following command:
sudo systemctl start docker
  1. If there are any errors related to Docker, try to restart the service with this command:
sudo systemctl restart docker
  1. Verify that the Docker daemon is now running by using the command:
sudo systemctl status docker

Section 8: Handling Cloud Platforms: AWS, GCP, and Azure

For cloud platforms like Amazon Web Services (AWS), Google Cloud Platform (GCP), and Microsoft Azure, the process to find a container’s IP address varies depending on the service used. Follow the specific instructions for each platform:

  • AWS: Check the Elastic Network Interface (ENI) associated with the Docker container to find its IP address. You can find this information in the AWS Management Console or using AWS CLI commands like describe-network-interfaces and describe-instances.
  • GCP: Use the gcloud compute instances describe <instance-name> command to view the external IP address assigned to your container.
  • Azure: Utilize the Azure Portal or Azure CLI commands like az vm show and az network public-ip list to find the public IP address of your Docker container.

Section 9: Conclusion

In this article, we explored various methods to discover the IP address of a running Docker container on localhost and in cloud environments like AWS, GCP, and Azure. By using the inspect command and understanding the differences between bridged networks and overlay networks, you can efficiently find a container’s IP address and ensure smooth communication between containers and external resources.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.