Home » Uncategorized » How to Get Container IP in Docker

How to Get Container IP in Docker

In Docker, you can get the IP address of a running container using the docker inspect command. This command provides detailed information about the container, including its network settings.

You can use the docker ps -a command to get the list of all Docker containers (both running and stopped). It returns the container ID, name, command, and other details. You can use either the container ID or name to retrieve the container’s IP address.

The following example shows how to retrieve the IP address of a Docker container.

Get the IP Address of a Docker Container Using Docker Inspect

To get the IP address of a specific Docker container, we can use the docker inspect command followed by the container ID or name.

Suppose we want to view the IP address of a running Docker container, we can run the following command in the terminal:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' app-postgres

Output: 👇️

Get Container's IP Address
Get Container’s IP Address

In this example, the docker inspect command retrieves the app-postgres container’s IP address.

The `-f` flag formats the output, extracting the IP address from the container’s network settings.

This helps us to identify the IP address assigned to a container, which can be useful for network configuration and troubleshooting.

Get Container’s IP Address Using Docker Exec + Inside Container

We can also get the container’s IP address from inside the container itself by running the following command inside the running container.

docker exec -t app-postgres hostname -I

Output: 👇️

172.17.0.2

This command returns the IP address assigned to the app-postgres container.

Conclusion

We can use the docker inspect command with the appropriate formatting options to get the IP address of a Docker container.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.