How to get a Docker container's IP address from the host
The --format
option of inspect
comes to the rescue.
Modern Docker client syntax is:
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
Old Docker client syntax is:
docker inspect --format '{{ .NetworkSettings.IPAddress }}' container_name_or_id
These commands will return the Docker container's IP address.
As mentioned in the comments: if you are on Windows, use double quotes "
instead of single quotes '
around the curly braces.
You can use docker inspect <container id>
.
For example:
CID=$(docker run -d -p 4321 base nc -lk 4321);
docker inspect $CID
First get the container ID:
docker ps
(First column is for container ID)
Use the container ID to run:
docker inspect <container ID>
At the bottom,under "NetworkSettings", you can find "IPAddress"
Or Just do:
docker inspect <container id> | grep "IPAddress"