Get the IP addresses of Docker containers
Finding Docker container IP addresses is essential for debugging network connectivity, connecting services, and understanding how containers communicate. This guide shows you how to quickly retrieve IP addresses using Docker's inspect command with Go template formatting.
When working with Docker containers, you often need to know their IP addresses for debugging network issues, connecting services together, or troubleshooting connectivity problems. Docker provides the docker inspect command which can extract this information using Go template syntax.
Understanding the Go Template Syntax
The commands below use Go template syntax (the {{...}} notation) to extract specific fields from Docker's internal JSON data. Here's what each part means:
{{.Name}}- Retrieves the container name{{.NetworkSettings.IPAddress}}- Gets the IP address from the default network{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}- Iterates through all networks the container is connected to and gets the IP address (useful for Docker Compose which creates custom networks)
This template syntax allows you to extract exactly the data you need without parsing JSON manually.
Getting Container IP Addresses
To list all Docker containers and their corresponding IP addresses, run:
docker inspect -f "{{.Name}}: {{.NetworkSettings.IPAddress }}" $(docker ps -aq)
To get the IP address for a specific Docker container, run:
docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" CONTAINER_ID
To list all Docker Compose containers and their corresponding IP addresses, run:
docker inspect -f "{{.Name}}: {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" $(docker ps -aq) | cut -c2-
You can add these to your ~/.profile, ~/.bashrc, or ~/.zshrc in handy aliases like:
# Get IP addresses of all Docker containersalias dips='docker inspect -f "{{.Name}}: {{.NetworkSettings.IPAddress }}" $(docker ps -aq)'# Get IP address of specific Docker container. Usage: dip CONTAINER_NAME_OR_IDalias dip='docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}"'
# Get IP addresses of all Docker Compose containersalias dcips='docker inspect -f "{{.Name}}: {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" $(docker ps -aq) | cut -c2-'
Frequently Asked Questions
Why do I need Docker container IP addresses?
Container IP addresses are useful for several scenarios: debugging network connectivity issues between containers, connecting to services running inside containers for testing, verifying that containers are on the correct network, and troubleshooting firewall or routing problems. While Docker's DNS resolution usually handles container-to-container communication automatically, knowing the actual IP addresses helps when you need to diagnose low-level networking issues or connect from outside Docker's network.
What is the {{...}} syntax in these commands?
The double curly brace syntax is Go template formatting, which Docker's inspect command uses to extract specific data from its internal JSON structures. The docker inspect command outputs detailed JSON about containers, but Go templates let you extract just the fields you need without manually parsing the entire JSON output. The dot notation (like .NetworkSettings.IPAddress) navigates through nested JSON objects to reach the specific value you want.
Do container IPs change when I restart them?
Yes, by default Docker assigns IP addresses dynamically from its network pools, so container IP addresses typically change when you restart containers. This is why Docker recommends using container names or service names for inter-container communication instead of hardcoding IP addresses. If you need stable IP addresses, you can configure static IPs using Docker networks with the --ip flag, or use Docker Compose with fixed IP configurations. However, relying on Docker's DNS-based service discovery is generally a better practice than managing static IPs.