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:

  • {{.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

Getting Container IP Addresses

To list all Docker containers and their corresponding IP addresses, run:

List all
docker inspect -f "{{.Name}}: {{.NetworkSettings.IPAddress }}" $(docker ps -aq)

To get the IP address for a specific Docker container, run:

List by container
docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" CONTAINER_ID

To list all Docker Compose containers and their corresponding IP addresses, run:

List all Docker Compose
docker inspect -f "{{.Name}}: {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" $(docker ps -aq) | cut -c2-

You can add these to your ~/.profile, ~/.bashrc, or ~/.zshrc as handy aliases:

Docker: dip or dips
# Get IP addresses of all Docker containers
alias dips='docker inspect -f "{{.Name}}: {{.NetworkSettings.IPAddress }}" $(docker ps -aq)'

# Get IP address of specific Docker container. Usage: dip CONTAINER_NAME_OR_ID
alias dip='docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}"'
Docker Compose: dcips
# Get IP addresses of all Docker Compose containers
alias 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: 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.

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.