How to Get a Docker Container’s IP Address from the Host

When working with Docker, there are times you may need to know the IP address of a running container—for example, to debug networking issues or connect containers manually. Docker provides several ways to retrieve this information depending on your setup and networking mode.

In this blog, we’ll cover how to get a Docker container’s IP address from the host machine, with practical commands and helpful tips.


🔍 Why Would You Need a Container’s IP Address?

  • To connect to a service running inside a container
  • For inter-container communication without Docker Compose
  • For debugging network-related issues
  • To run custom pings, curl, or database queries manually

⚠️ Note: In most modern Docker workflows, containers are connected using names (via Docker networks), and direct IP usage is discouraged in favor of service discovery. But it’s still useful to know how to fetch IPs.


✅ Method 1: Using docker inspect

The most reliable way to get a container’s IP address is with docker inspect.

Command:

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.18.0.2

This is the container’s IP within the Docker network it belongs to (usually the bridge network).


✅ Method 2: Inspect Full Network Info

You can also inspect the full network settings and find the IP manually:

docker inspect my_container

Look for this section:

"NetworkSettings": {
  "Networks": {
    "bridge": {
      "IPAddress": "172.17.0.2",
      ...
    }
  }
}

✅ Method 3: Using docker network inspect

If your container is connected to a custom Docker network, use:

docker network inspect <network-name>

Then find your container’s name under the Containers section with its corresponding IP.

Example:

docker network inspect my_custom_network

✅ Method 4: Exec into the Container and Use hostname -I

If you have access to the container shell, you can simply run:

docker exec -it <container-name> hostname -I

Output:

172.18.0.2

This returns the container’s IP address directly from inside the container.


🚫 Not Available in Host Network Mode

If you started your container with --network=host, then the container shares the host’s network stack, and does not have a separate IP. In that case:

  • The container’s IP is the host machine’s IP.
  • Commands like docker inspect won’t show a separate container IP.

🧠 Summary

MethodDescriptionWorks For Custom Networks?
docker inspect -fQuick, formatted IP fetch✅ Yes
docker inspectFull detailed info (manual lookup)✅ Yes
docker network inspectBest for multi-container setups✅ Yes
docker exec hostname -IFrom inside the container✅ Yes
--network=hostNo separate container IP❌ N/A

🛡 Best Practices

  • Prefer container names or hostnames for inter-container communication (especially with Docker Compose).
  • Avoid relying on container IPs in production—they can change on restart or redeployment.
  • For stable connections, use Docker DNS-based discovery or named volumes/networks.

🧪 Bonus Tip: Ping Between Containers

If you want to test connectivity:

docker exec -it container1 ping container2

Or use the IP you obtained:

docker exec -it container1 ping 172.18.0.3
Sharing Is Caring:

Leave a Comment