To get a Docker container’s IP address from the host, use the docker inspect
command with a filter to extract the IP address. The method depends on whether the container uses the default bridge network or a custom network.
1. Basic Command (Default Bridge Network)
For containers using the default bridge network:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name_or_id>
Example:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my_container
# Output: 172.17.0.2
2. For a Specific Network
If the container is part of a custom network (e.g., my_network
):
docker inspect -f '{{.NetworkSettings.Networks.my_network.IPAddress}}' <container_name_or_id>
Example:
docker inspect -f '{{.NetworkSettings.Networks.app_network.IPAddress}}' my_container
# Output: 192.168.1.5
3. List All IPs (All Networks)
To see IP addresses for all networks the container is connected to:
docker inspect -f '{{json .NetworkSettings.Networks}}' <container_name_or_id> | jq .
Example Output:
{
"bridge": {
"IPAddress": "172.17.0.2",
...
},
"my_custom_network": {
"IPAddress": "192.168.1.5",
...
}
}
4. Quick One-Liner
Use grep
and awk
for simplicity:
docker inspect <container_name_or_id> | grep -i "ipaddress" | awk '{print $2}' | head -n 1
5. Get Container IP via docker exec
Execute a command inside the container (requires hostname
or ip
tools):
docker exec <container_name_or_id> hostname -I
# Output: 192.168.1.5
Notes
- Default Bridge Network: The IP is usually in the
172.17.0.x
range. - Custom Networks: Use
docker network ls
to list networks anddocker network inspect <network>
for details. - Docker Compose: Containers in a Compose project use a network named
<project>_default
. Adjust the network name accordingly.
Example Workflow
- Find the container ID/name:
docker ps
- Get the IP:
docker inspect -f '{{.NetworkSettings.Networks.my_network.IPAddress}}' my_container
Use these commands to efficiently retrieve container IPs for debugging or networking tasks!