๐Ÿšข Day 07 - Docker Inspect Command Deep Dive

Ibrar AnsariIbrar Ansari
4 min read

Explore the powerful docker inspect command to fetch deep-level metadata of Docker containers, including their environment, volumes, networks, logs, and more.

Here is the docker inspect --help information formatted as a Markdown section:

๐ŸŽฌ Video Demonstration

Watch on Youtube

๐Ÿ› ๏ธ docker inspect Help

docker inspect --help

๐Ÿ“„ Usage

Usage:  docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Return low-level information on Docker objects.

โš™๏ธ Options

| Option | Description | | | - | | -f, --format | Format output using a custom template:
- 'json': Print in JSON format
- 'TEMPLATE': Use a Go template for output. See formatting docs | | -s, --size | Display total file sizes (only for containers) | | --type string | Return JSON for the specified type |

๐Ÿ”ง Set Example Container Name

# Example container ID or name
container_name="test-container"

๐Ÿ•ต๏ธโ€โ™‚๏ธ Basic Inspection

# Inspect full details of a container
docker inspect $container_name

โš™๏ธ Full Config (JSON)

# Get the full container config in JSON format
docker inspect --format='{{json .Config}}' $container_name | jq

๐Ÿ–ผ๏ธ Image Name

# Get the image name used to create the container
docker inspect --format='{{.Config.Image}}' $container_name

๐ŸŒ Network Information

# Get container network info
docker inspect --format='{{json .NetworkSettings.Networks}}' $container_name | jq

# Get container IP address of known network.
docker inspect --format='{{.NetworkSettings.Networks.bridge.IPAddress}}' $container_name

# Get container IP address of all network
# It's useful when a container is connected to multiple networks and you want to get values like IPAddress from each.
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $container_name

# Get the MAC address of the container
docker inspect --format='{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}' $container_name

# Get port mapping ๐Ÿ”Œ 
docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}}{{$p}}{{end}}' $container_name

# Get the host-port to container-port mappings ๐Ÿ”Œ 
docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' $container_name
# Get container IPs for all running containers
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -q)

# With container names
docker ps -q | xargs -I {} docker inspect --format '{{.Name}}: {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' {} | sed 's|/||'

โš™๏ธ Restart Policy

# Show restart policy of all containers
docker ps -aq | xargs docker inspect --format '{{.Name}} Restart: {{.HostConfig.RestartPolicy.Name}}'

# Alternative using subshell
docker inspect --format '{{.Name}} Restart: {{.HostConfig.RestartPolicy.Name}}' $(docker ps -aq)

๐ŸŒฑ Environment Variables

# List all environment variables
docker inspect --format='{{range $index, $value := .Config.Env}}{{$value}}{{"\n"}}{{end}}' $container_name

๐Ÿ—ƒ๏ธ Mounts & Volumes

# Show all mount sources
docker inspect --format='{{range .Mounts}}{{.Source}}{{"\n"}}{{end}}' $container_name

# Show source and destination
docker inspect --format='{{range .Mounts}}{{.Source}}:{{.Destination}}{{"\n"}}{{end}}' $container_name

# Using jq to filter only bind/volume mounts
docker inspect $container_name | jq '.[0].Mounts[] | select(.Type == "bind" or .Type == "volume") | {Type, Source, Destination}'

# Using grep (less structured)
docker inspect $container_name | grep -i -A 17 "Mounts"

๐Ÿชต Log Paths & Sizes

# Get container log file path
docker inspect --format='{{.LogPath}}' $container_name

# View log size
sudo du -sh $(docker inspect --format='{{.LogPath}}' $container_name)

# Clear container logs
echo "" > $(docker inspect --format='{{.LogPath}}' $container_name)

# Copy log file to another location
sudo cp -a $(docker inspect --format='{{.LogPath}}' $container_name) /tmp/api.log

# Get log sizes for all containers
docker ps -aq | while read cid; do 
  name=$(docker inspect --format='{{.Name}}' "$cid" | sed 's/^\/\?//')
  log_path=$(docker inspect --format='{{.LogPath}}' "$cid")
  size=$(sudo du -sh "$log_path" 2>/dev/null | cut -f1)
  echo -e "$name\t$size"
done

๐Ÿ“Š Resource Allocation

# Show allocated CPU shares
docker inspect --format='{{.HostConfig.CPUShares}}' $container_name

# Show memory limit (in bytes)
docker inspect --format='{{.HostConfig.Memory}}' $container_name

๐Ÿงช Health Check

# Show container health status (basic)
docker inspect --format='{{.State.Health.Status}}' $container_name

# Alternative using jq
docker inspect $container_name | jq -r ".[].State.Health.Status"

๐Ÿท๏ธ Labels & Metadata

# Get all container labels as JSON
docker inspect $container_name --format '{{json .Config.Labels}}'

๐Ÿงพ Container Exit Code & Logging Config

# Check exit code
docker inspect $container_name | grep ExitCode

# Log config
docker inspect $container_name | grep LogConfig

# Log max size (if configured)
docker inspect $container_name | grep -i MaxSize

๐Ÿƒ Find Executed Commands

# Path and arguments used to start container
docker inspect -f "{{.Name}} {{.Path}} {{.Args}}" $(docker ps -a -q)

# Show .Config.Cmd value
docker inspect -f "{{.Name}} {{.Config.Cmd}}" $(docker ps -a -q)

# With container ID
docker inspect -f "{{.Path}} {{.Args}} ({{.Id}})" $(docker ps -a -q)

๐Ÿณ docker inspect template to regenerate the docker run command from created container

# This file defines how the output from docker inspect should be presented โ€” potentially in a cleaner, structured format.
docker inspect \
  --format "$(curl -s https://raw.githubusercontent.com/meibraransari/Docker-Zero-to-Hero/refs/heads/main/Day-07_Docker_insect_command/assets/run.tpl)" \
  $container_name

๐Ÿ”— Additional Resources

โœ… Final Tips

  • Use jq for cleaner, structured JSON parsing.

  • Combine docker inspect with awk, xargs, grep, or sed to build powerful inspection scripts.

  • Combine with monitoring and alerting for proactive container management.

0
Subscribe to my newsletter

Read articles from Ibrar Ansari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ibrar Ansari
Ibrar Ansari

Hello! I'm a DevOps Engineer dedicated to continuous learning and contributing to the tech community. If you find value in what I share, feel free to spread the word to others who might benefit as well. Your support is greatly appreciated!