How to get a Docker container's IP address from the host

Asked 2023-09-20 20:56:02 View 388,269

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.

  • I just wanted to make sure other noobs don't make my mistake and try to get the IP from the image instead of the container. Ensure you get the CID or container id and query that; CID via 'docker ps' that is. - anyone

Answers

This solution only works if the container is connected with a single network. The --format option of inspect comes to the rescue.

Modern Docker client syntax is:

docker inspect \
  -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

Old Docker client syntax is:

docker inspect \
  --format '{{ .NetworkSettings.IPAddress }}' container_name_or_id

These commands will return the Docker container's IP address.

As mentioned in the comments: if you are on Windows, use double quotes " instead of single quotes ' around the curly braces.

Answered   2023-09-20 20:56:02

  • This is a much, MUCH better solution. Only ask for what you need, if possible! - anyone
  • Hmm. Getting a <no value> response on a Mac using Fig/Boot2Docker - anyone
  • !WARNING! This doesn't work anymore. The new format is specific to the container and follows the form {{ .NetworkSettings.Networks.$network.IPAddress }}. The default appears to be bridge, but under docker-compose this will be a specific name that depends on the name of your app (I think from the --project-name flag, though that's also going to depend on what type of networking config you have set up). I wrote a full answer in an answer here stackoverflow.com/questions/17157721/… - anyone
  • Maybe I am a sleepy sheep, but the warning about double quotes was not bold and blinky enough to catch my attention. Maybe provide two examples clearly labelled Linux and Windows. I literally just typed the example, checked the docs which also uses single quotes and then Googled the error. I'm sure I'm not alone. - anyone
  • Solution for peoples getting formatting error: docker inspect <container id> and scroll down you will find the container ip-address - anyone

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 for UNIX based:

docker inspect <container id> | grep "IPAddress"

And for Windows CMD:

docker inspect <container id> | findstr "IPAddress"

Answered   2023-09-20 20:56:02

  • Too bad, on one of my instances (started with docker run -it MYCONTAINER /bin/bash), the output of inspect has no section NetworkSettings! - anyone
  • This doesn´t work on Windows 10. You need too find out the ip address of DockerNAT with ipconfig. - anyone
  • @Eric You can also inspect network bridge for that: docker network inspect bridge | grep Gateway | grep -o -E '[0-9.]+' - anyone
  • I greatly prefer the ... | grep IP solution to all the formatting details required to get IP. +1 - anyone
  • @Eric - you've confused container with image. The command is actually docker run -it MYIMAGE (that isn't a container name); if you do docker inspect MYIMAGE you will get static information about the image. You need to find the name of the running container (or use its id as others suggest). On my system, the first such container created from that image defaults to name MYIMAGE_1, so docker inspect MYIMAGE_1 has NetworkSettings. - anyone

You can use docker inspect <container id>.

For example:

CID=$(docker run -d -p 4321 base nc -lk 4321);
docker inspect $CID

Answered   2023-09-20 20:56:02

  • In order to extract the ip, you can do something like docker inspect $CID | grep IPAddress | cut -d '"' -f 4, it works fine :) - anyone
  • Bringing it all together, this shell alias should list all container ids and their ips: alias dockerip='docker ps | tail -n +2 | while read cid b; do echo -n "$cid\t"; docker inspect $cid | grep IPAddress | cut -d \" -f 4; done' - anyone
  • As mentionned by @user3119830, there is a new option to inspect. Now, you can get the Ip easier with docker inspect -format '{{ .NetworkSettings.IPAddress }}' ${CID} - anyone
  • Just a note. The single dash options are being deprecated so that -format will become --format. - anyone
  • docker inspect -format '{{ .NetworkSettings.IPAddress }}' ${CID} is the new syntax. -format is deprecated, it becomes --format. - anyone
docker inspect CONTAINER_ID | grep "IPAddress"

You can add -i to grep for ignoring the case then even the following will work:

docker inspect CONTAINER_ID | grep -i "IPaDDreSS"

Answered   2023-09-20 20:56:02

  • how is this answer better/different from the other 22? - anyone
  • This is definitely not the best answer. WouterD's answer is the answer you're looking for. - anyone
  • @M.Vanderlee this answer has a command that is easier to remember, which makes it a better answer IMO. - anyone
  • @WouterHuysentruit Sure, but you get multiple entries and the actual "IPAddress" text. You can't use it in a script without further manipulation. - anyone
  • You're right. In the context of the question, the other answer fits better. - anyone

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

Answered   2023-09-20 20:56:02

  • I really like this but it doesn't work for Docker Compose as well, something to do with having their own network. Instead change {{.NetworkSettings.IPAddress }} to {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} - anyone
  • Here's the variation I use: docker inspect --format '{{ .NetworkSettings.IPAddress }}' $(docker ps -aq | xargs) | sed -e '/^$/d'. The sed(1) removes empty lines for containers present, but not having an IP-address. - anyone
  • A little bit lame addition, but for anyone who needs to execute this command with sudo, here is this one liner: docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(sudo docker ps -aq) - anyone
  • Does not work correctly when a container is connected with multiple docker networks. - anyone

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}}' "$@"
}

Answered   2023-09-20 20:56:02

  • Template parsing error: template: :1:19: executing "" at <.NetworkSettings.IPA...>: map has no entry for key "NetworkSettings" - anyone
  • this works for me, but need tosource ~/.bash_profile - anyone
  • You will get this error if you are not inspecting a running container, but just an image... an image doesn't have an IPAddress... terrible error message, yes - anyone

In Docker 1.3+, you can also check it using:

Enter the running Docker (Linux):

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

For windows:

docker exec [container-id or container-name] ipconfig

Answered   2023-09-20 20:56:02

  • or just docker exec [container-name-or-id] ip a - anyone
  • for windows docker exec [container-id or container-name] ipconfig - anyone

Show all container's IP addresses:

docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

Answered   2023-09-20 20:56:02

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"
        }
    }
]

Answered   2023-09-20 20:56:02

  • what does it mean if your containers section is empty? Yet container is up and running. - anyone

My answer:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} %tab% {{.Name}}' $(docker ps -aq
) | sed 's#%tab%#\t#g' | sed 's#/##g' | sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n

Also as a bash alias:

docker-ips() {   docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} %tab% {{.Name}}' $(docker ps -aq) | sed 's#%tab%#\t#g' | sed 's#/##g' | sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n }

Output is sorted by IP address, and tab delimited:

# docker-ips
172.18.0.2       memcached
172.18.0.3       nginx
172.18.0.4       fpm-backup
172.18.0.5       dns
172.18.0.6       fpm-beta
172.18.0.7       exim
172.18.0.8       fpm-delta
172.18.0.9       mariadb
172.18.0.10      fpm-alpha
172.19.0.2       nextcloud-redis
172.19.0.3       nextcloud-db
172.19.0.4       nextcloud

Answered   2023-09-20 20:56:02

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"

Answered   2023-09-20 20:56:02

  • This IS actually answer. Many thanks. - anyone

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

Answered   2023-09-20 20:56:02

Docker is written in Go and it uses Go syntax for query purposes too.

To inspect the IP address of a particular container, you need to run the command (-f for "format"):

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_id_or_name

For the container ID or name, you can run the command

docker container ls

which will list every running container.

Answered   2023-09-20 20:56:02

  • This is very useful. Where can I find documentation to write my own Go queries? - anyone

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.

Answered   2023-09-20 20:56:02

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'

Answered   2023-09-20 20:56:02

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)

Answered   2023-09-20 20:56:02

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:

$ 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

Answered   2023-09-20 20:56:02

  • Please, change to python2.7 in a script, python3 is now in common use or write two versions - anyone
  • I'll update my Gist and as soon as I have it, I'll post here. Indeed, this wrapper was designed for Python 2.7. Thanks for the feedback! - anyone

I use this simple way

docker exec -it <container id or name> hostname -i

e.g

ubuntu@myhost:~$ docker exec -it 3d618ac670fe hostname -i
10.0.1.5

Answered   2023-09-20 20:56:02

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}'

Answered   2023-09-20 20:56:02

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'

Answered   2023-09-20 20:56:02

  • That can be simplified quite a bit - use docker ps -q to avoid the tail, and --format to avoid the grep. Also you can pass multiple container IDs to docker inspect rather than loop in shell. - anyone
  • docker ps -q only displays the containers' numeric IDs, but I think using their name is better. tail is only to get rid of the first line (table header) & is still needed. I could use --format, though: docker ps | tail -n +2 | while read -a a; do name=${a[$((${#a[@]}-1))]}; echo -ne "$name\t"; docker inspect --format="{{.NetworkSettings.IPAddress}}" $name | cut -d \" -f 4; done. But that doesn't seem like a big improvement... the grep was more readable IMO. - anyone
  • Slightly altered version which lists IPs first, in one line together with the container name: docker ps | tail -n +2 | while read -a a; do name=${a[$((${#a[@]}-1))]}; docker inspect $name | grep "IPAddress.*[0-9]\+" | cut -d \" -f 4 | tr "\n" " "; echo -e "\t${name}"; done - anyone
  • Ended up with : docker inspect $cid | grep IPAddress | cut -d '"' -f 4 | tail -n 1 - anyone
  • Actually like the style of this better docker ps | tail -n +2 | while read -a a; do name=${a[$((${#a[@]}-1))]}; echo ${name};docker inspect $name | grep "IPAddress.*[0-9]\+"; done - anyone

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:

  1. Running container with --cidfile parameter, the app.cid file content is like:

    a29ac3b9f8aebf66a1ba5989186bd620ea66f1740e9fe6524351e7ace139b909
    
  2. You can use file content to inspect Docker containers:

    blog-v4 git:(develop) ✗ docker inspect `cat app.cid`
    
  3. 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.

Answered   2023-09-20 20:56:02

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}'`

Answered   2023-09-20 20:56:02

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"

Answered   2023-09-20 20:56:02

Just for completeness:

I really like the --format option, but at first I wasn't aware of it 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"]'

Answered   2023-09-20 20:56:02

If you installed Docker using Docker Toolbox, you can use the Kitematic application to get the container IP address:

  1. Select the container
  2. Click on Settings
  3. Click in Ports tab.

Answered   2023-09-20 20:56:02

  • For me, the IP address was on the "Hostname/ports" tab, but this was nevertheless a very helpful answer! Note that docker inspect does not work when you run docker toolbox. It will report an IP address, but it is not the correct address that your services will use. - anyone

The accepted answer does not work well with multiple networks per container:

> docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' cc54d96d63ea

172.20.0.4172.18.0.5

The next best answer is closer:

> docker inspect cc54d96d63ea | grep "IPAddress"

"SecondaryIPAddresses": null,
"IPAddress": "",
    "IPAddress": "172.20.0.4",
    "IPAddress": "172.18.0.5",

I like to use jq to parse the network JSON:

> docker inspect cc54d96d63ea | jq -r 'map(.NetworkSettings.Networks) []'

{
  "proxy": {
    "IPAMConfig": null,
    "Links": [
      "server1_php_1:php",
      "server1_php_1:php_1",
      "server1_php_1:server1_php_1"
    ],
    "Aliases": [
      "cc54d96d63ea",
      "web"
    ],
    "NetworkID": "7779959d7383e9cef09c970c38c24a1a6ff44695178d314e3cb646bfa30d9935",
    "EndpointID": "4ac2c26113bf10715048579dd77304008904186d9679cdbc8fcea65eee0bf13b",
    "Gateway": "172.20.0.1",
    "IPAddress": "172.20.0.4",
    "IPPrefixLen": 24,
    "IPv6Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "MacAddress": "02:42:ac:14:00:04",
    "DriverOpts": null
  },
  "webservers": {
    "IPAMConfig": null,
    "Links": [
      "server1_php_1:php",
      "server1_php_1:php_1",
      "server1_php_1:server1_php_1"
    ],
    "Aliases": [
      "cc54d96d63ea",
      "web"
    ],
    "NetworkID": "907a7fba8816cd0ad89b7f5603bbc91122a2dd99902b504be6af16427c11a0a6",
    "EndpointID": "7febabe380d040b96b4e795417ba0954a103ac3fd37e9f6110189d9de92fbdae",
    "Gateway": "172.18.0.1",
    "IPAddress": "172.18.0.5",
    "IPPrefixLen": 24,
    "IPv6Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "MacAddress": "02:42:ac:12:00:05",
    "DriverOpts": null
  }
}

To list the IP addresses of every container then becomes:

for s in `docker ps -q`; do
  echo `docker inspect -f "{{.Name}}" ${s}`:
  docker inspect ${s} | jq -r 'map(.NetworkSettings.Networks) []' | grep "IPAddress";
done

/server1_web_1:
    "IPAddress": "172.20.0.4",
    "IPAddress": "172.18.0.5",
/server1_php_1:
    "IPAddress": "172.20.0.3",
    "IPAddress": "172.18.0.4",
/docker-gen:
    "IPAddress": "172.18.0.3",
/nginx-proxy:
    "IPAddress": "172.20.0.2",
    "IPAddress": "172.18.0.2",

Answered   2023-09-20 20:56:02

To get the IP address and host port of a container:

docker inspect containerId | awk '/IPAddress/ || /HostPort/'

Output:

    "HostPort": "4200"
                    "HostPort": "4200"
        "SecondaryIPAddresses": null,
        "IPAddress": "172.17.0.2",
                "IPAddress": "172.17.0.2",

Answered   2023-09-20 20:56:02

For windows 10:

docker inspect --format "{{ .NetworkSettings.IPAddress }}"  containerId

Answered   2023-09-20 20:56:02

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

Answered   2023-09-20 20:56:02

Docker inspect use to print all container ips and its respective names

docker ps -q | xargs -n 1 docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} {{ .Name }}' | sed 's/ \// /'

Answered   2023-09-20 20:56:02