There are several ways to view the IP address of a Docker container, and here are some common methods:
Method 1: Use the docker inspect command
docker inspect
The command can provide detailed information about the container, including the IP address. The following are the specific steps:
Gets the ID or name of the container. Can be passeddocker ps
Command to view the list of running containers.
docker ps
usedocker inspect
The command gets the detailed information of the container and passes thegrep
orjq
The tool filters out the IP address.
docker inspect <container_id_or_name> | grep '"IPAddress"' | head -n 1 | awk '{print $2}' | tr -d '",'
Or usejq
Tools (if installed):
docker inspect <container_id_or_name> | jq -r '.[0].'
Method 2: Use the docker inspect command combined with the network name
If you know the network name to which the container belongs, you can get the IP address more accurately:
docker inspect -f '{{range $key, $value := .}}{{$key}}: {{$}}{{end}}' <container_id_or_name>
Method 3: Use the docker exec command
You can execute commands inside the container to get its IP address. For example, for Linux-based containers, you can useip addr
Order:
Gets the ID or name of the container.
docker ps
implementip addr
Order:
docker exec <container_id_or_name> ip addr show eth0
Method 4: Use the docker network inspect command
If you know the network name to which the container belongs, you can usedocker network inspect
Command to view all containers and their IP addresses under the network:
Get the network name.
docker network ls
View network details:
docker network inspect <network_name>
In the output, find your container and view its IP address.
Example
Suppose you have a running container with the namemy_container
, you can use the following command to obtain its IP address:
docker inspect -f '{{range $key, $value := .}}{{$key}}: {{$}}{{end}}' my_container
The output may be similar to:
bridge: 172.17.0.2
this meansmy_container
existbridge
The IP address in the network is172.17.0.2
。
This is the end of this article about four methods to view the IP address of Docker container. For more related Docker viewing IP address content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!