LinuxCommandLibrary

docker-inspect

Display detailed information about Docker objects

TLDR

Display help

$ docker inspect
copy

Display information about a container, image, or volume using a name or ID
$ docker inspect [container|image|ID]
copy

Display a container's IP address
$ docker inspect [[-f|--format]] '\{\{range.NetworkSettings.Networks\}\}\{\{.IPAddress\}\}\{\{end\}\}' [container]
copy

Display the path to the container's log file
$ docker inspect [[-f|--format]] '\{\{.LogPath\}\}' [container]
copy

Display the image name of the container
$ docker inspect [[-f|--format]] '\{\{.Config.Image\}\}' [container]
copy

Display the configuration information as JSON
$ docker inspect [[-f|--format]] '\{\{json .Config\}\}' [container]
copy

Display all port bindings
$ docker inspect [[-f|--format]] '\{\{range $p, $conf := .NetworkSettings.Ports\}\} \{\{$p\}\} -> \{\{(index $conf 0).HostPort\}\} \{\{end\}\}' [container]
copy

SYNOPSIS

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

PARAMETERS

--format, -f string
    Format the output using a Go template. This is highly flexible for extracting specific data points from the extensive JSON output.

--size, -s
    Display total file sizes if the object is a container. Useful for understanding disk usage.

--type string
    Return JSON for a specific type of object (e.g., container, image, network, volume). Helps narrow down the scope when an ID could potentially refer to multiple object types.

DESCRIPTION

The docker inspect command provides a raw, low-level view of an object's configuration and state as maintained by the Docker daemon.
This command is incredibly useful for debugging, scripting, and automation, as it outputs information in JSON format by default, making it easily parsable by other tools.
It can inspect various Docker objects, including containers, images, networks, volumes, and plugins, by specifying their name or ID.
The output includes extensive metadata such as network settings, mounted volumes, container state, image layers, and more.
Users can also customize the output using Go templates via the --format option, allowing for extraction of specific data points rather than the entire verbose JSON structure. This makes it a powerful tool for integrating Docker information into scripts and applications.

CAVEATS

The output can be extremely verbose and contain a large amount of data, which might be overwhelming for direct human consumption without piping to a JSON formatter like jq.
Care should be taken when sharing docker inspect output as it may contain sensitive configuration details, environment variables, or IP addresses.
Extracting specific data with Go templates can have a learning curve due to the template syntax.

GO TEMPLATE USAGE

The --format option is key for automation and selective data retrieval. For instance, to get a container's IP address: docker inspect -f '{{.NetworkSettings.IPAddress}}' mycontainer.
To check if a container is running: docker inspect -f '{{.State.Running}}' mycontainer.
You can iterate over arrays, e.g., to list all mounted volumes: docker inspect -f '{{range .Mounts}}{{.Source}} -> {{.Destination}}{{end}}' mycontainer.
Templates support conditional logic, loops, and functions, allowing for complex data extraction and formatting.

INSPECTING MULTIPLE OBJECTS

You can inspect multiple objects in a single command by providing multiple names or IDs, like docker inspect container1 container2 image-id. The output will be a JSON array containing the detailed information for each specified object, making it efficient for batch processing.

HISTORY

docker inspect has been a core utility since the early days of Docker's public release around 2013. As Docker revolutionized containerization, the need for a granular understanding of container and image internals became paramount. inspect was designed to provide exactly that, offering a programmatic interface to the metadata stored by the Docker daemon. Its JSON output format aligned well with the rise of automation and scripting, making it an indispensable command for developers and operations teams alike. It has evolved with Docker, accommodating new object types like volumes and plugins.

SEE ALSO

docker ps(1), docker images(1), docker logs(1), jq(1)

Copied to clipboard