Activeyesterday
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.
Murali AlladaMurali AlladaI have two virtualbox ubuntu16 guests which can communicate over a host-only network: 172.28.128.0/16 I created a docker swarm using the steps from docker docs.
6,01533 gold badges1212 silver badges1818 bronze badges
47 Answers
12 next
The
--format
option of inspect comes to the rescue.Modern Docker client syntax:
Old Docker client syntax:
Which will return just the IP address.
As mentioned in the comments: if you are on Windows, use double quotes
Jonas Heidelberg'
instead of single quotes '
around the curly braces. 3,95411 gold badge2121 silver badges3636 bronze badges
WouterDWouterD18.6k22 gold badges88 silver badges55 bronze badges
Tim Cooper126k3232 gold badges256256 silver badges233233 bronze badges
creackcreack76.7k1010 gold badges7777 silver badges6565 bronze badges
First get the container ID:
(First column is for container ID)
Use the container ID to run:
At the bottom,under 'NetworkSettings', you can find 'IPAddress'
Or Just do:
KrunalKrunal4,32811 gold badge1212 silver badges1616 bronze badges
docker inspect CONTAINER_ID | grep 'IPAddress'
If you add -i to grep, IPAddress casing can be anything like the following:
docker inspect CONTAINER_ID | grep -i 'IPADDress'
3,67611 gold badge2222 silver badges3636 bronze badges
To get all container names and their IP addresses in just one single command.
If you are using
docker-compose
the command will be this:The output will be:
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
Hemerson VarelaHemerson Varela13.8k1010 gold badges5151 silver badges5858 bronze badges
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:
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
Tuong LeTuong Le10.6k55 gold badges4141 silver badges4040 bronze badges
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
zhoujizhouji
In Docker 1.3+, you can also check it via steps below:
Enter the running Docker (Linux):
For windows:
Jake WJake W
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.
AthenaAthena
Execute:
This will display active docker images:
Use the CONTAINER ID value:
'172.17.0.2'
user260826user2608264,10122 gold badges4444 silver badges7878 bronze badges
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.
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
silgonsilgon3,18711 gold badge2828 silver badges4848 bronze badges
Reference containers by name:
Then grab the IP address address by name:
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
Sameer CSameer C2,28711 gold badge1515 silver badges1212 bronze badges
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.
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
DunkDunk
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.Rushal VermaRushal Verma
Here's is a solution that I developed today in Python, using the 'docker inspect container' JSON output as the data source.
I have a lot of containers and infrastructures that I have to inspect, and I need to obtain basic network information from any container, in a fast and pretty manner. That's why I made this script.
IMPORTANT: Since the version 1.9, Docker allows you to create multiple networks and attach them to the containers.
The output is just like this:
ivanleonczivanleoncz2,73833 gold badges3030 silver badges3737 bronze badges
The above works if the container is deployed to the default bridge network.
However, if using a custom bridge network or a overlay network, I found the below to work better:
Aravind MurthyAravind Murthy
Get your container Name or ID:
Then do it (get IP):
Get Port:
5,47466 gold badges3737 silver badges6161 bronze badges
To extend ko-dos' answer, here's an alias to list all container names and their IP addresses:
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
Marco RoyMarco Roy1,03455 gold badges1515 silver badges3333 bronze badges
If you installed Docker using Docker Toolbox, you can use the Kitematic application to get the container IP address:
- Select the container
- Click on Settings
- Click in Ports tab.
14.4k1919 gold badges8888 silver badges117117 bronze badges
Fernando MontoyaFernando Montoya2,33511 gold badge1616 silver badges2020 bronze badges
To get the IP address and host port of a container:
Output:
ssmith4,86255 gold badges3333 silver badges5959 bronze badges
Raj AsapuRaj Asapu
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.Using PID file
- Running container with
--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
Here there are 10 alternatives of getting the Docker container IP addresses.
Community♦
guneysusguneysus4,30411 gold badge3030 silver badges3030 bronze badges
Hai NguyenHai Nguyen
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:
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
sedisedi
Combining previous answers with finding the container id based on the Docker image name.
Gene HazanGene Hazan
For those who came from Google to find solution for command execution from terminal (not by script), jid (an interactive JSON drill down util with autocomplete and suggestion) let you achieve same thing with less typing.
Type tab .Net tab and you'll see following:
Type .IPA tab and you'll see following:
Joel HandwellJoel Handwell31211 gold badge44 silver badges1414 bronze badges
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
Alex LiffickAlex Liffick
For Windows containers use
where
<container>
is the name or the id of the container.You can use
docker ps
to find the id of the container.kirodgekirodge
If you forgot container ID or don't want to manipulate with shell commands, it's better to use UI like Portainer.
There you can find all information about container also IP.
igorigor
This will list down all the container IPs on the host:
sɐunıɔןɐqɐp1,77866 gold badges2121 silver badges2525 bronze badges
rajdeepbs29rajdeepbs29
Docker inspect use to print all container ips and its respective names
KARTHIKEYAN.AKARTHIKEYAN.A6,50033 gold badges5252 silver badges6060 bronze badges
protected by eyllanescMay 9 '18 at 15:15
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?