How to use the command 'docker inspect' (with examples)
Docker Inspect command is used to return low-level information on Docker objects such as containers, images, and volumes. It provides detailed information about the object in JSON format, allowing users to understand the internal configuration and network settings of the Docker object.
Use case 1: Show help
Code:
docker inspect
Motivation:
This use case is useful when you need a quick reminder of the available options and how to use them with the docker inspect
command.
Explanation:
Running docker inspect
without any additional arguments will display the help message, which gives an overview of the command and its usage.
Example Output:
Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Return low-level information on Docker objects
...
Use case 2: Display information about a container, image, or volume using a name or ID
Code:
docker inspect container|image|ID
Motivation: This use case is helpful when you need to retrieve detailed information about a specific Docker object, such as a container, image, or volume, using its name or ID.
Explanation:
Replace container|image|ID
in the command with the name or ID of the container, image, or volume you want to inspect. This command will provide comprehensive details about the object in JSON format.
Example Output:
[
{
"Id": "1234abcd5678",
"Created": "2021-01-01T00:00:00Z",
...
}
]
Use case 3: Display a container’s IP address
Code:
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container
Motivation: This use case allows you to retrieve the IP address of a container. It can be useful for debugging or interacting with the container on the network.
Explanation:
The --format
flag is used to specify the output format. In this example, --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
is used to iterate over the container’s network settings and retrieve the IP address.
Example Output:
172.17.0.2
Use case 4: Display the path to the container’s log file
Code:
docker inspect --format='{{.LogPath}}' container
Motivation: This use case is helpful when you need to access the log file of a container for troubleshooting or analysis purposes.
Explanation:
The --format='{{.LogPath}}'
argument is used to access the log file path of the container.
Example Output:
/var/lib/docker/containers/1234abcd5678/1234abcd5678-json.log
Use case 5: Display the image name of the container
Code:
docker inspect --format='{{.Config.Image}}' container
Motivation: This use case allows you to retrieve the image name of a container, which can be useful for managing and identifying containers based on their underlying images.
Explanation:
The --format='{{.Config.Image}}'
argument is used to access the image name from the container’s configuration.
Example Output:
nginx:latest
Use case 6: Display the configuration information as JSON
Code:
docker inspect --format='{{json .Config}}' container
Motivation: This use case provides the container’s configuration information as JSON. It can be used to retrieve and parse specific configuration parameters programmatically.
Explanation:
The --format='{{json .Config}}'
argument is used to format the output as JSON, specifically accessing the Config
field of the container.
Example Output:
{
"Hostname": "",
"Domainname": "",
...
}
Use case 7: Display all port bindings
Code:
docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}}{{$p}} -> (index $conf 0).HostPort{{end}}' container
Motivation: This use case allows you to retrieve all port bindings of a container, which can be helpful in scenarios where you want to verify or expose the container’s ports.
Explanation:
The --format='{{range $p, $conf := .NetworkSettings.Ports}}{{$p}} -> (index $conf 0).HostPort{{end}}'
argument uses Go templates to iterate over the container’s network settings and retrieve the port bindings.
Example Output:
8080 -> 32768
Conclusion:
The docker inspect
command is a powerful tool for retrieving detailed information about Docker objects. By using various options and formatting arguments, you can get precise details such as IP address, log path, image name, and more. It is beneficial for troubleshooting, debugging, and managing Docker containers, images, and volumes.