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.
The --format
option of inspect comes to the rescue.
Modern Docker client syntax:
docker inspect -f \'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}\' container_name_or_id
Old Docker client syntax:
docker inspect --format \'{{ .NetworkSettings.IPAddress }}\' container_name_or_id
Which will return just the IP address.
You can use docker inspect <container id>
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\"
docker inspect CONTAINER_ID | grep \"IPAddress\"
To get all container names and their IP addresses in just one single command.
docker inspect -f \'{{.Name}} - {{.NetworkSettings.IPAddress }}\' $(docker ps -aq)
If you are using docker-compose
the command will be this:
docker inspect -f \'{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}\' $(docker ps -aq)
The output will be:
/containerA - 172.17.0.4
/containerB - 172.17.0.3
/containerC - 172.17.0.2
Add this shell script in your ~/.bashrc
or relevant file:
docker-ip() {
docker inspect --format \'{{ .NetworkSettings.IPAddress }}\' \"$@\"
}
Then, to get an IP address of a container, simply do this:
docker-ip YOUR_CONTAINER_ID
For the new version of the Docker, please use the following:
docker-ip() {
docker inspect --format \'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}\' \"$@\"
}
Show all containers IP addresses:
docker inspect --format=\'{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}\' $(docker ps -aq)
In Docker 1.3+, you can also check it via steps below:
Enter the running Docker:
docker exec [container-id or container-name] cat /etc/hosts
172.17.0.26 d8bc98fa4088
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.17 mysql
Execute:
docker ps -a
This will display active docker images:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3b733ae18c1c parzee/database \"/usr/lib/postgresql/\" 6 minutes ago Up 6 minutes 5432/tcp serene_babbage
Use the CONTAINER ID value:
docker inspect <CONTAINER ID> | grep -w \"IPAddress\" | awk \'{ print $2 }\' | head -n 1 | cut -d \",\" -f1
\"172.17.0.2\"
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:
docker network inspect bridge
Which should then return a Containers section which will display the IP address for that running container.
[
{
\"Name\": \"bridge\",
\"Id\": \"40561e7d29a08b2eb81fe7b02736f44da6c0daae54ca3486f75bfa81c83507a0\",
\"Scope\": \"local\",
\"Driver\": \"bridge\",
\"IPAM\": {
\"Driver\": \"default\",
\"Options\": null,
\"Config\": [
{
\"Subnet\": \"172.17.0.0/16\"
}
]
},
\"Containers\": {
\"025d191991083e21761eb5a56729f61d7c5612a520269e548d0136e084ecd32a\": {
\"Name\": \"drunk_leavitt\",
\"EndpointID\": \"9f6f630a1743bd9184f30b37795590f13d87299fe39c8969294c8a353a8c97b3\",
\"IPv4Address\": \"172.17.0.2/16\",
\"IPv6Address\": \"\"
}
},
\"Options\": {
\"com.docker.network.bridge.default_bridge\": \"true\",
\"com.docker.network.bridge.enable_icc\": \"true\",
\"com.docker.network.bridge.enable_ip_masquerade\": \"true\",
\"com.docker.network.bridge.host_binding_ipv4\": \"0.0.0.0\",
\"com.docker.network.bridge.name\": \"docker0\",
\"com.docker.network.driver.mtu\": \"1500\"
}
}
]
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.
docker-ips() {
docker inspect --format=\'{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}\' $(docker ps -aq)
}
docker-ip() {
docker inspect --format \'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}\' \"$@\"
}
The first command gives the IP address of all the containers and the second a specific container\'s IP address.
docker-ips
docker-ip YOUR_CONTAINER_ID
Reference containers by name:
docker run ... --name pg-master
Then grab the IP address address by name:
MASTER_HOST=$(docker inspect --format \'{{ .NetworkSettings.IPAddress }}\' pg-master)
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.
I wrote the following Bash script to get a table of IP addresses from all containers running under docker-compose
.
function docker_container_names() {
docker ps -a --format \"{{.Names}}\" | xargs
}
# Get the IP address of a particular container
dip() {
local network
network=\'YOUR-NETWORK-HERE\'
docker inspect --format \"{{ .NetworkSettings.Networks.$network.IPAddress }}\" \"$@\"
}
dipall() {
for container_name in $(docker_container_names);
do
local container_ip=$(dip $container_name)
if [[ -n \"$container_ip\" ]]; then
echo $(dip $container_name) \" $container_name\"
fi
done | sort -t . -k 3,3n -k 4,4n
}
You should change the variable network to your own network name.
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.
#!/usr/bin/python
import json
import subprocess
import sys
try:
CONTAINER = sys.argv[1]
except Exception as e:
print \"\\n\\tSpecify the container name, please.\"
print \"\\t\\tEx.: script.py my_container\\n\"
sys.exit(1)
# Inspecting container via Subprocess
proc = subprocess.Popen([\"docker\",\"inspect\",CONTAINER],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
out = proc.stdout.read()
json_data = json.loads(out)[0]
net_dict = {}
for network in json_data[\"NetworkSettings\"][\"Networks\"].keys():
net_dict[\'mac_addr\'] = json_data[\"NetworkSettings\"][\"Networks\"][network][\"MacAddress\"]
net_dict[\'ipv4_addr\'] = json_data[\"NetworkSettings\"][\"Networks\"][network][\"IPAddress\"]
net_dict[\'ipv4_net\'] = json_data[\"NetworkSettings\"][\"Networks\"][network][\"IPPrefixLen\"]
net_dict[\'ipv4_gtw\'] = json_data[\"NetworkSettings\"][\"Networks\"][network][\"Gateway\"]
net_dict[\'ipv6_addr\'] = json_data[\"NetworkSettings\"][\"Networks\"][network][\"GlobalIPv6Address\"]
net_dict[\'ipv6_net\'] = json_data[\"NetworkSettings\"][\"Networks\"][network][\"GlobalIPv6PrefixLen\"]
net_dict[\'ipv6_gtw\'] = json_data[\"NetworkSettings\"][\"Networks\"][network][\"IPv6Gateway\"]
for item in net_dict:
if net_dict[item] == \"\" or net_dict[item] == 0:
net_dict[item] = \"null\"
print \"\\n[%s]\" % network
print \"\\n{}{:>13} {:>14}\".format(net_dict[\'mac_addr\'],\"IP/NETWORK\",\"GATEWAY\")
print \"--------------------------------------------\"
print \"IPv4 settings:{:>16}/{:<5} {}\".format(net_dict[\'ipv4_addr\'],net_dict[\'ipv4_net\'],net_dict[\'ipv4_gtw\'])
print \"IPv6 settings:{:>16}/{:<5} {}\".format(net_dict[\'ipv6_addr\'],net_dict[\'ipv6_net\'],net_dict[\'ipv6_gtw\'])
The output is just like this:
$ python docker_netinfo.py debian1
[frontend]
02:42:ac:12:00:02 IP/NETWORK GATEWAY
--------------------------------------------
IPv4 settings: 172.18.0.2/16 172.18.0.1
IPv6 settings: null/null null
[backend]
02:42:ac:13:00:02 IP/NETWORK GATEWAY
--------------------------------------------
IPv4 settings: 172.19.0.2/16 172.19.0.1
IPv6 settings: null/null null
docker inspect --format \'{{ .NetworkSettings.IPAddress }}\' <containername or containerID here>
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:
docker exec <containername or containerID here> /sbin/ifconfig eth0 | grep \'inet addr:\' | cut -d: -f2 | awk \'{ print $1}\'
To extend ko-dos\' answer, here\'s an alias to list all container names and their IP addresses:
alias docker-ips=\'docker ps | tail -n +2 | while read -a a; do name=${a[$((${#a[@]}-1))]}; echo -ne \"$name\\t\"; docker inspect $name | grep IPAddress | cut -d \\\" -f 4; done\'
If you installed Docker using Docker Toolbox, you can use the Kitematic application to get the container 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:
dockip() {
docker inspect --format \'{{ .NetworkSettings.IPAddress }}\' \"$@\"
}
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
--cidfile=\"app.cid\": Write the container ID to the 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
#!/usr/bin/env python
# Coding: utf-8
# Save this file like get-docker-ip.py in a folder that in $PATH
# Run it with
# $ docker inspect <CONTAINER ID> | get-docker-ip.py
import json
import sys
sys.stdout.write(json.load(sys.stdin)[0][\'NetworkSettings\'][\'IPAddress\'])
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.
For windows 10:
docker inspect --format \"{{ .NetworkSettings.IPAddress }}\" containerId
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:
docker inspect <CONTAINER> |python -c \'import json,sys;obj=json.load(sys.stdin);print obj[0][\"NetworkSettings\"][\"IPAddress\"]\'
To get the IP address and host port of a container:
docker inspect conatinerId | awk \'/IPAddress/ || /HostPort/\'
Output:
\"HostPort\": \"4200\"
\"HostPort\": \"4200\"
\"SecondaryIPAddresses\": null,
\"IPAddress\": \"172.17.0.2\",
\"IPAddress\": \"172.17.0.2\",
sudo docker container ls
sudo docker inspect <container_ID Or container_name> |grep \'IPAddress\'
Combining previous answers with finding the container id based on the Docker image name.
docker inspect --format \'{{ .NetworkSettings.IPAddress }}\' `docker ps | grep $IMAGE_NAME | sed \'s/\\|/ /\' | awk \'{print $1}\'`
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.
docker inspect $CID | jid
Type tab .Net tab and you\'ll see following:
[Filter]> .[0].NetworkSettings
{
\"Bridge\": \"\",
\"EndpointID\": \"b69eb8bd4f11d8b172c82f21ab2e501fe532e4997fc007ed1a997750396355d5\",
\"Gateway\": \"172.17.0.1\",
\"GlobalIPv6Address\": \"\",
\"GlobalIPv6PrefixLen\": 0,
\"HairpinMode\": false,
\"IPAddress\": \"172.17.0.2\",
\"IPPrefixLen\": 16,
\"IPv6Gateway\": \"\",
\"LinkLocalIPv6Address\": \"\",
\"LinkLocalIPv6PrefixLen\": 0,
\"MacAddress\": \"02:42:ac:11:00:02\",
\"Networks\": {
\"bridge\": {
\"Aliases\": null,
\"EndpointID\": \"b69eb8bd4f11d8b172c82f21ab2e501fe532e4997fc007ed1a997750396355d5\",
\"Gateway\": \"172.17.0.1\",
\"GlobalIPv6Address\": \"\",
Type .IPA tab and you\'ll see following:
[Filter]> .[0].NetworkSettings.IPAddress
\"172.17.0.2\"
Use:
docker inspect $CID | grep IPAddress | grep -v null| cut -d \'\"\' -f 4 | head -1
For Windows containers use
docker exec <container> ipconfig
where <container>
is the name or the id of the container.
You can use docker ps
to find the id of the container.
If you forgot container ID or don\'t want to manipulate with shell commands, it\'s better to use UI like Portainer.
https://portainer.io/
$ docker volume create portainer_data
$ docker run -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer
There you can find all information about container also IP.
This will list down all the container IPs on the host:
sudo docker ps -aq | while read line; do sudo docker inspect -f \'{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}\' $line ; done
Inspect didn\'t work for me. Maybe as I was using -net host
and some namespaces.
Anyway, I found this to work nicely:
docker exec -i -t NAME /sbin/ifconfig docker0 | grep \'inet addr:\' | cut -d: -f2 | awk \'{ print $1}\'