How to use docker container inspect command to view container details

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker inspect command to view detailed information about Docker containers. You will explore how to inspect both running and stopped containers, understand the default JSON output format, and discover how to format the output using a Go template for customized views. Additionally, you will learn how to display container size information using the docker inspect command.

Through hands-on exercises, you will gain practical experience in using docker inspect to retrieve valuable configuration, state, and network details, which is essential for troubleshooting and managing your Docker environment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/stop("Stop Container") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/ContainerOperationsGroup -.-> docker/inspect("Inspect Container") subgraph Lab Skills docker/run -.-> lab-555110{{"How to use docker container inspect command to view container details"}} docker/ps -.-> lab-555110{{"How to use docker container inspect command to view container details"}} docker/stop -.-> lab-555110{{"How to use docker container inspect command to view container details"}} docker/rm -.-> lab-555110{{"How to use docker container inspect command to view container details"}} docker/inspect -.-> lab-555110{{"How to use docker container inspect command to view container details"}} end

Inspect a running container

In this step, you will learn how to inspect a running Docker container. The docker inspect command provides detailed information about a container, including its configuration, network settings, and state.

First, let's run a simple container that stays running. We will use the ubuntu image and run a command that keeps the container alive.

docker run -d --name my-running-container ubuntu sleep infinity

In the command above:

  • docker run: This command is used to run a new container.
  • -d: This flag runs the container in detached mode, meaning it runs in the background.
  • --name my-running-container: This assigns a name to the container, making it easier to reference.
  • ubuntu: This is the image we are using. If the image is not available locally, Docker will pull it.
  • sleep infinity: This command is executed inside the container and keeps it running indefinitely.

Now that the container is running, we can inspect it. Use the docker inspect command followed by the container name.

docker inspect my-running-container

This command will output a large JSON object containing all the details about the my-running-container. You can scroll through the output to see the various sections, such as Id, State, Config, NetworkSettings, etc.

To make the output more readable or to extract specific information, you can pipe the output to tools like jq (a lightweight and flexible command-line JSON processor). However, for this lab, we will focus on the basic docker inspect command.

After inspecting the container, you can stop and remove it to clean up your environment.

docker stop my-running-container
docker rm my-running-container
  • docker stop my-running-container: This command stops the running container.
  • docker rm my-running-container: This command removes the stopped container.

Inspect a stopped container

In this step, you will learn that you can also inspect containers that are not currently running. The docker inspect command works on both running and stopped containers, providing access to their configuration and final state.

First, let's run a simple container that will exit after a short time.

docker run --name my-stopped-container ubuntu sleep 5

In this command:

  • docker run: Runs a new container.
  • --name my-stopped-container: Assigns a name to the container.
  • ubuntu: The image to use.
  • sleep 5: The command executed inside the container, which will cause it to exit after 5 seconds.

Wait for a few seconds for the container to finish executing the sleep 5 command and stop. You can verify that the container has stopped by listing all containers (including stopped ones) using docker ps -a.

docker ps -a

You should see my-stopped-container in the list with a status indicating it has exited.

Now, you can inspect the stopped container using the docker inspect command, just like you did with the running container in the previous step.

docker inspect my-stopped-container

You will again see a detailed JSON output. Notice that the State section will show information about the container's exit status and when it finished.

Inspecting stopped containers is useful for debugging or understanding why a container exited.

Finally, clean up the stopped container.

docker rm my-stopped-container

Format inspect output as JSON

In this step, you will learn how to format the output of the docker inspect command specifically as a JSON array. This is particularly useful when inspecting multiple objects (like containers or images) at once, as it provides a structured output that can be easily parsed by other tools or scripts.

First, let's run two simple containers that will stay running.

docker run -d --name container1 ubuntu sleep infinity
docker run -d --name container2 ubuntu sleep infinity

Now, let's inspect both containers and format the output as a JSON array. We can do this by providing multiple container names to the docker inspect command.

docker inspect container1 container2

The output will be a single JSON array containing the inspection details for both container1 and container2. Each element in the array is the JSON object for a single container.

This format is the default when inspecting multiple objects. It's a standard way to represent a list of structured data.

After inspecting, stop and remove the containers.

docker stop container1 container2
docker rm container1 container2

Format inspect output using a Go template

In this step, you will learn how to use Go templates to format the output of the docker inspect command. This allows you to extract specific pieces of information from the detailed JSON output and display them in a custom format.

First, let's run a container that stays running.

docker run -d --name my-templated-container ubuntu sleep infinity

Now, let's inspect this container and use a Go template to display only its IP address. The IP address is located within the NetworkSettings.IPAddress field of the inspect output.

docker inspect --format '{{.NetworkSettings.IPAddress}}' my-templated-container

In this command:

  • --format '{{.NetworkSettings.IPAddress}}': This flag specifies the format using a Go template.
    • {{...}}: These double curly braces denote an action in the Go template.
    • .: This represents the root object being inspected (the container).
    • NetworkSettings: This accesses the NetworkSettings field within the root object.
    • IPAddress: This accesses the IPAddress field within the NetworkSettings object.

The output of this command will be just the IP address of the container.

You can use Go templates to extract various pieces of information. For example, to get the container's name and ID:

docker inspect --format 'Name: {{.Name}}, ID: {{.Id}}' my-templated-container

This will output something like Name: /my-templated-container, ID: <container_id>.

Go templates offer powerful formatting capabilities, including conditional statements, loops, and functions. However, for this basic example, we are just accessing fields.

After inspecting, stop and remove the container.

docker stop my-templated-container
docker rm my-templated-container

Display container size information

In this step, you will learn how to display the size information of your Docker containers. This can be helpful for monitoring disk usage and understanding the footprint of your containers.

By default, the docker ps command does not show the size of the containers. To include size information, you can use the --size or -s flag.

First, let's run a simple container.

docker run -d --name my-size-container ubuntu sleep infinity

Now, list the running containers and include the size information.

docker ps --size

or

docker ps -s

In the output, you will see an additional column labeled SIZE. This column shows two values:

  1. The size of the container's writable layer (the changes made to the container's filesystem since it was created).
  2. The total size of the container, which includes the size of the image layers plus the writable layer.

The size of the writable layer is often small for simple containers that don't write much data. The total size will be larger as it includes the base image size.

You can also see size information when listing all containers (including stopped ones) using docker ps -a --size.

docker ps -a --size

This is useful for seeing the size of containers that have exited but haven't been removed yet.

Understanding container size is important for managing disk space on your Docker host.

Finally, stop and remove the container.

docker stop my-size-container
docker rm my-size-container

Summary

In this lab, you learned how to use the docker inspect command to view detailed information about Docker containers. You practiced inspecting both running and stopped containers, understanding that the command provides comprehensive data regardless of the container's state.

Furthermore, you explored how to format the docker inspect output, specifically by outputting the full details as JSON and by using Go templates to extract and display specific pieces of information. Finally, you learned how to use the --size flag to include container size information in the inspection output.