Check the Private IP of Each Docker Container
This post was translated from Chinese by AI. If anything reads oddly, the Chinese original is authoritative. 中文原文
How do I check each Docker container's private IP—the kind starting with 172?
To check a Docker container's private IP (usually an address starting with 172), use the following command:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <容器ID或容器名>
Replace <容器ID或容器名> with the ID or name of the container you want to check. For example:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my_container
This outputs the container's private IP address. Make sure the container is running; otherwise, it may not have an IP address assigned.
What if I want to see all containers' private IPs?
To check the private IP addresses of all running containers, use the following command:
docker ps -q | xargs -n 1 docker inspect -f '{{.Name}}: {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
This command lists the names of all running containers and their corresponding private IP addresses. It uses docker ps -q to get a list of container IDs, then uses xargs to pass each ID to docker inspect, which outputs the container's name and private IP address.
Run this command in your terminal and make sure the Docker daemon is running.
Last updated 2024-01-14
Comments 0