NeuroAgent

How to Get Docker Container IP Address

Learn multiple methods to get Docker container IP addresses from the host machine. Complete guide with docker inspect commands, network inspection, and automation scripts for your deployment workflows.

Question

How to get a Docker container’s IP address from the host

Is there a command I can run to get the container’s IP address right from the host after a new container is created?

Basically, once Docker creates the container, I want to roll my own code deployment and container configuration scripts.

NeuroAgent

You can get a Docker container’s IP address from the host machine using several commands, with docker inspect being the most reliable method. The command docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_id_or_name> will display the container’s primary IP address. Alternatively, you can use docker network inspect to get IP addresses from specific networks, or docker ps with additional formatting options for quick access to container IPs.


Contents


Basic Methods for Getting Container IP

There are several straightforward commands you can use to retrieve a Docker container’s IP address from the host machine. The most common methods include:

Docker Inspect Method

The docker inspect command provides comprehensive information about containers, including their network configuration and IP addresses. This is the most reliable method as it gives you detailed network information.

bash
# Get container ID or name first
docker ps

# Then inspect the container for IP address
docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

Docker Network Inspect Method

If you need to inspect a specific network and get all container IPs within it:

bash
docker network inspect bridge --format='{{range .Containers}}{{.Name}}: {{.IPv4Address}} {{end}}'

Quick Container List with IPs

For a quick overview of running containers and their IPs:

bash
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Networks}}"

Using Docker Inspect Command

The docker inspect command is the most powerful and flexible way to get a container’s IP address. Here are various ways to use it:

Basic IP Address Retrieval

bash
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name

This command extracts the primary IP address from the container’s network settings.

Multiple Network Interfaces

If a container is connected to multiple networks, you can get all IP addresses:

bash
docker inspect --format='{{range $k, $v := .NetworkSettings.Networks}}{{$k}}: {{$v.IPAddress}} {{end}}' container_name

Getting IP and Network Name Together

bash
docker inspect --format='{{range $k, $v := .NetworkSettings.Networks}}{{$v.IPAddress}} ({{$k}}) {{end}}' container_name

Using Container ID

You can use either the container name or ID:

bash
docker inspect --format '{{.NetworkSettings.IPAddress}}' container_id

Note: The simpler .NetworkSettings.IPAddress format works for older Docker versions, but the NetworkSettings.Networks approach is more reliable for newer versions.


Network-Specific Approaches

Custom Networks

When containers are attached to custom networks, you can get their IP addresses using network inspection:

bash
# Get all containers in a specific network
docker network inspect my_network --format='{{range .Containers}}{{.Name}}: {{.IPv4Address}} {{end}}'

Bridge Network

The default bridge network can be inspected with:

bash
docker network inspect bridge --format='{{range .Containers}}{{.Name}}: {{.IPv4Address}} {{end}}'

Overlay Networks

For Docker Swarm overlay networks:

bash
docker network inspect my_overlay_network --format='{{range .Containers}}{{.Name}}: {{.IPv4Address}} {{end}}'

Scripting Solutions for Automation

Shell Script for Multiple Containers

Here’s a practical shell script that gets IP addresses for multiple containers:

bash
#!/bin/bash

# Get all running containers
containers=$(docker ps --format "{{.Names}}")

# Loop through each container and get its IP
for container in $containers; do
    ip=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$container")
    echo "$container: $ip"
done

Python Script Example

For more complex automation, you can use Python with the Docker SDK:

python
import docker

client = docker.from_env()
containers = client.containers.list()

for container in containers:
    networks = container.attrs['NetworkSettings']['Networks']
    for network_name, network_info in networks.items():
        print(f"{container.name}: {network_info['IPAddress']} ({network_name})")

One-Liner Commands

These are useful for quick integration into deployment scripts:

bash
# Get IP of the most recently created container
docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -l -q)

# Get container name and IP in CSV format
docker ps --format "{{.Names}},{{range $k, $v := .NetworkSettings.Networks}}{{$v.IPAddress}}{{end}}"

Troubleshooting Common Issues

Container Not Showing IP Address

If a container doesn’t show an IP address:

  1. Check if container is running: docker ps | grep container_name
  2. Verify network settings: docker inspect container_name | grep -A 10 "NetworkSettings"
  3. Check network connectivity: docker exec container_name ping google.com

Multiple IP Addresses in Custom Networks

When containers use multiple networks, you might see multiple IP addresses. Use the network-specific approach to get the correct one:

bash
# Get IP from specific network
docker inspect --format '{{index .NetworkSettings.Networks "my_network" "IPAddress"}}' container_name

IPv6 Support

For IPv6 addresses, use:

bash
docker inspect --format '{{range .NetworkSettings.Networks}}{{.GlobalIPv6Address}}{{end}}' container_name

Best Practices for Container Networking

Use DNS Names Instead of IPs

For better reliability in automation scripts, consider using Docker’s internal DNS:

bash
# Containers can reach each other by name
docker exec container_name ping other_container_name

Environment Variables for Container IPs

You can set environment variables with container IPs in your deployment scripts:

bash
#!/bin/bash

container_ip=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my_container)
export CONTAINER_IP=$container_ip
echo "Container IP is: $container_ip"

Health Checks for Network Availability

Add health checks to ensure containers are network-ready:

bash
docker inspect --format '{{.State.Health.Status}}' container_name

Log IP Addresses for Documentation

Keep a log of container IPs for your deployment documentation:

bash
echo "Container deployed at $(date) with IP: $(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name)" >> deployment.log

Conclusion

Getting a Docker container’s IP address from the host machine is straightforward with several command-line options available. The most reliable method is using docker inspect with the appropriate format string, which provides detailed network information about running containers. For automation purposes, shell scripts and Python scripts using the Docker SDK offer more flexible solutions. When building your deployment and configuration scripts, consider using Docker’s internal DNS names instead of hardcoding IP addresses for better reliability, and implement proper error handling for network-related operations.


Sources

  1. Official Docker Documentation - Container Networking
  2. Docker Inspect Command Reference
  3. Docker Network Management Guide
  4. Stack Overflow - Get Container IP Address
  5. Docker Engine API Documentation