๐ข Day 07 - Docker Inspect Command Deep Dive

Table of contents
- ๐ฌ Video Demonstration
- ๐ ๏ธ docker inspect Help
- ๐ง Set Example Container Name
- ๐ต๏ธโโ๏ธ Basic Inspection
- โ๏ธ Full Config (JSON)
- ๐ผ๏ธ Image Name
- ๐ Network Information
- โ๏ธ Restart Policy
- ๐ฑ Environment Variables
- ๐๏ธ Mounts & Volumes
- ๐ชต Log Paths & Sizes
- ๐ Resource Allocation
- ๐งช Health Check
- ๐ท๏ธ Labels & Metadata
- ๐งพ Container Exit Code & Logging Config
- ๐ Find Executed Commands
- ๐ณ docker inspect template to regenerate the docker run command from created container
- ๐ Additional Resources
- โ Final Tips

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
๐ ๏ธ 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
๐งฐ Docker inspect
๐ JSON Path Finder
โ Final Tips
Use
jq
for cleaner, structured JSON parsing.Combine
docker inspect
withawk
,xargs
,grep
, orsed
to build powerful inspection scripts.Combine with monitoring and alerting for proactive container management.
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!