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.
As of Docker version 1.10.3, build 20f81dd
Unless you told Docker otherwise, Docker always launches your containers in the bridge network. So you can try this command below:
Which should then return a Containers section which will display the IP address for that running container.
Docker is made internally in Go and it uses Go syntax for query purposes too.
For inspecting about the IP address of a particular container, you need to run the command(-f for format option)
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_id_or_name
For container id or name, you can run the command
docker container ls
. It will list down the every running container.The
--format
option of inspect comes to the rescue.Modern Docker client syntax:
Old Docker client syntax:
Which will return just the IP address.
Add this shell script in your
~/.bashrc
or relevant file:Then, to get an IP address of a container, simply do this:
For the new version of the Docker, please use the following:
I wrote the following Bash script to get a table of IP addresses from all containers running under
docker-compose
.You should change the variable network to your own network name.
Just for completeness:
I really like the --format option, but at first I wasn't aware of that option so I used a simple Python one-liner to get the same result: