docker inspect --format memo

(It was left halfway in the draft of Qiita. It's a mess, so I will update it as appropriate.)

Use --format to retrieve any information you like in docker inspect.

Frequently used format (for myself)

docker inspect --format "\
host:{{.Name}} \
ip: {{.NetworkSettings.IPAddress}} \
gw: {{.NetworkSettings.Gateway}} \
port [host]: {{.NetworkSettings.Ports}}" $(docker ps -q)

result

host:/container-name \
ip: 172.17.0.2 \
gw: 172.17.0.1 \
port [host]: map[10010/tcp:[] 22/tcp:[{0.0.0.0 12223}] 3000/tcp:[{0.0.0.0 3000}] 80/tcp:[]]

IP only

PS C:\Users\name> docker inspect $(docker ps -q) | jq -r '.[].NetworkSettings.Networks.bridge.IPAddress'
172.17.0.3
172.17.0.2

IP and name

PS C:\Users\name> docker inspect $(docker ps -q) | jq -r '.[].Name, .[].NetworkSettings.Networks.bridge.IPAddress'
/api-test2
/api-test
172.17.0.3
172.17.0.2

Shape and take

PS C:\Users\name> docker inspect $(docker ps -q) | jq -r '.[] | { Name: .Name, IP: .NetworkSettings.Networks.bridge.IPAddress }'
{
  "Name": "/api-test2",
  "IP": "172.17.0.3"
}
{
  "Name": "/api-test",
  "IP": "172.17.0.2"
}

It seems that you can do various things

PS C:\Users\name> docker inspect $(docker ps -q) | jq '.[] | {moge: {hoge: { Name: .Name, IP: .NetworkSettings.Networks.bridge.IPAddress }}}'
{
  "moge": {
    "hoge": {
      "Name": "/api-test2",
      "IP": "172.17.0.3"
    }
  }
}
{
  "moge": {
    "hoge": {
      "Name": "/api-test",
      "IP": "172.17.0.2"
    }
  }
}

Recommended Posts

docker inspect --format memo
docker memo
docker tutorial (memo)
Docker operation memo
Docker command memo
[Memo] docker summary
Docker x Rails 6 (memo)
Docker Machine command memo
MySQL 5.7 (Docker) environment construction memo
Redmine (Docker) environment construction memo
Installing and building Docker (memo)
Docker execution memo summarized for myself
docker
Install Docker with WSL2 Memo ([Part 2] Docker introduction)