Is there a command I can run to get the container's IP address right from the host after a new container is created?
Basically, once Docker creates the container, I want to roll my own code deployment and container configuration scripts.
Is there a command I can run to get the container's IP address right from the host after a new container is created?
Basically, once Docker creates the container, I want to roll my own code deployment and container configuration scripts.
Show all containers IP addresses:
Quick answer (This works):
Get your container name or ID:
Then do it:
Reference containers by name:
Then grab the IP address address by name:
Based on some of the answers I loved, I decided to merge them to a function to get all the IP addresses and another for an specific container. They are now in my
.bashrc
file.The first command gives the IP address of all the containers and the second a specific container's IP address.
NOTE!!! for Docker Compose Usage:
Since Docker Compose creates an isolated network for each cluster, the methods below does not work with
docker-compose
.The most elegant and easy way is defining a shell function as the most-voted answer @WouterD's:
Docker can write container ids to a file like Linux programs:
Running with
--cidfile=filename
parameter, Docker dumps the ID of the container to this file.Docker runs PID equivalent Section
Using PID file
--cidfile
parameter,app.cid
file content is like below:a29ac3b9f8aebf66a1ba5989186bd620ea66f1740e9fe6524351e7ace139b909
You can use file content to inspect Docker containers:
➜ blog-v4 git:(develop) ✗ docker inspect `cat app.cid`
Extracting Container IP inline Python script:
$ docker inspect `cat app.cid` | python -c "import json;import sys;\ sys.stdout.write(json.load(sys.stdin)[0]['NetworkSettings']['IPAddress'])" 172.17.0.2
More Human Friendly Form
http://networkstatic.net/10-examples-of-how-to-get-docker-container-ip-address/
Here there are 10 alternatives of getting the Docker container IP addresses.
Inspect didn't work for me. Maybe as I was using
-net host
and some namespaces.Anyway, I found this to work nicely: