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
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.
NOTE!!! for Docker Compose Usage:
Since Docker Compose creates an isolated network for each cluster, the methods below do not work with docker-compose
.
The most elegant and easy way is defining a shell function, currently 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
, Docker dumps the ID of the container to "filename".
See "Docker runs PID equivalent Section" for more information.
--cidfile="app.cid": Write the container ID to the file
Using a PID file:
Running container with --cidfile
parameter, the app.cid
file content is like:
a29ac3b9f8aebf66a1ba5989186bd620ea66f1740e9fe6524351e7ace139b909
You can use file content to inspect Docker containers:
blog-v4 git:(develop) ✗ docker inspect `cat app.cid`
You can extract the container IP using an 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
Here's a more human friendly form:
#!/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'])
See "10 alternatives of getting the Docker container IP addresses" for more information.
For windows 10:
docker inspect --format "{{ .NetworkSettings.IPAddress }}" containerId
Here's a quick working answer:
Get your container name or ID:
docker container ls
Then get the IP:
docker inspect <container_ID Or container_name> |grep 'IPAddress'
Get the port:
docker inspect <container_ID Or container_name> |grep 'Port'
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"
}
}
]
For those who came from Google to find a solution for command execution from the terminal (not by a script), "jid", which is an interactive JSON drill-down utility with autocomplete and suggestion, lets you do the same thing with less typing.
docker inspect $CID | jid
Type Tab .Net Tab and you'll see something like:
[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 something like:
[Filter]> .[0].NetworkSettings.IPAddress
"172.17.0.2"