When working with Docker, there are times when you need to communicate with a container directly via its IP address — whether for debugging, internal services, or custom networking.
In this post, we’ll walk through how to get a Docker container’s IP address from the host, understand when to use it, and what alternatives might be better.
📌 Why You Might Need the Container IP
You might need the container’s IP address when:
- Debugging internal service-to-service communication
- Testing a container from the host or another container
- Using a custom Docker bridge network
- Configuring firewall or routing rules
⚠️ Note: In production environments, relying on container IPs is discouraged — use service names or Docker networking features instead.
🔍 Method 1: Use docker inspect
The most direct way to retrieve a container’s IP is using the docker inspect command.
🧪 Example:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name_or_id>
This command filters out just the IP address from the container’s network settings.
✅ Sample Output:
172.18.0.2
That’s the internal IP of your container on the Docker network.
🔍 Method 2: Full Inspect (Verbose)
If you want to see all the networking details:
docker inspect <container_name_or_id>
Then look for this section in the output:
"NetworkSettings": {
...
"IPAddress": "172.18.0.2",
"Networks": {
"bridge": {
"IPAddress": "172.18.0.2"
}
}
}
Use this method if you need additional context about port bindings, gateway, or connected networks.
🔍 Method 3: Using docker exec + hostname -I
This method works from inside the container:
docker exec <container_name> hostname -I
It returns the container’s IP address visible from inside the container, which is helpful in multi-container setups.
⚠️ Important Notes
- These IPs are internal to Docker networks — not public/external IPs.
- IPs can change when containers restart (unless you’re using a custom network with static IPs).
- You cannot access the container IP from outside the host unless ports are explicitly exposed or published.
🧠 Best Practices
| Task | Recommendation |
|---|---|
| Access service from host | Use localhost:<exposed_port> |
| Connect containers internally | Use Docker network and service names |
| Debug container network | Use docker inspect or exec hostname |
| Static IP needed? | Use custom bridge networks carefully |
🧪 Bonus: List All Container IPs
To list all container names and their IPs in one go:
docker ps -q | xargs -n 1 docker inspect --format '{{ .Name }} - {{ .NetworkSettings.IPAddress }}'
✅ Conclusion
While it’s easy to get a Docker container’s IP address using docker inspect, it’s better to use Docker’s networking features, like service discovery via container names or user-defined networks, for scalable, robust communication.
Still, knowing how to get the IP is a useful tool when debugging or working with lower-level network configs.