How to use docker system df command to check disk usage

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively monitor and manage the disk space consumed by Docker. We will explore the docker system df command, starting with a summary view of disk usage across images, containers, volumes, and build cache.

Following the summary, you will delve into a more detailed view to identify specific components consuming significant space. Finally, you will learn how to format the output of docker system df as JSON for easier parsing and integration into scripting or monitoring tools.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/SystemManagementGroup(["System Management"]) docker/SystemManagementGroup -.-> docker/system("Manage Docker") subgraph Lab Skills docker/system -.-> lab-555247{{"How to use docker system df command to check disk usage"}} end

Check Docker disk usage summary

In this step, you will learn how to check the summary of Docker disk usage. Docker uses disk space to store images, containers, volumes, and build cache. It's important to monitor disk usage to prevent your system from running out of space.

The docker system df command provides a summary of the disk space used by different Docker components. Let's run this command to see the current disk usage.

docker system df

You should see output similar to this:

TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          X         X         X         X
Containers      X         X         X         X
Local Volumes   X         X         X         X
Build Cache     X         X         X         X

The output shows the total number of items, the number of active items, the total size consumed, and the amount of space that can be reclaimed by removing unused items for Images, Containers, Local Volumes, and Build Cache.

Check detailed Docker disk usage

In the previous step, you saw a summary of Docker disk usage. Now, let's explore how to get more detailed information about the disk space consumed by specific Docker components.

To see detailed information about images, containers, and local volumes, you can use the docker system df -v command. The -v flag provides a more verbose output.

Let's run the command:

docker system df -v

The output will be more detailed than the summary view. You will see lists of individual images, containers, and volumes, along with their size and other relevant information.

For example, the Images section might look like this:

Images space usage:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              abcdef123456        2 weeks ago         72.9MB
nginx               latest              fedcba654321        3 weeks ago         133MB

This detailed view helps you identify which specific images, containers, or volumes are consuming the most disk space.

Format docker system df output as JSON

In the previous steps, you learned how to view Docker disk usage in a human-readable format. Sometimes, you might need to process this information programmatically. Docker allows you to format the output of commands using the --format flag.

To get the output of docker system df in JSON format, you can use the --format json option. This is useful for scripting or integrating Docker disk usage information into other tools.

Let's run the command with the JSON format option:

docker system df --format json

The output will be a JSON array containing objects representing the disk usage of each component (Images, Containers, Local Volumes, Build Cache).

For example, the output might look like this (formatted for readability):

[
  {
    "Type": "Images",
    "Total": 2,
    "Active": 2,
    "Size": 205800000,
    "Reclaimable": 0
  },
  {
    "Type": "Containers",
    "Total": 0,
    "Active": 0,
    "Size": 0,
    "Reclaimable": 0
  },
  {
    "Type": "Local Volumes",
    "Total": 0,
    "Active": 0,
    "Size": 0,
    "Reclaimable": 0
  },
  {
    "Type": "Build Cache",
    "Total": 0,
    "Active": 0,
    "Size": 0,
    "Reclaimable": 0
  }
]

This JSON output can be easily parsed by programming languages or command-line tools like jq for further processing.

Summary

In this lab, you learned how to use the docker system df command to monitor Docker disk usage. You started by checking the summary of disk space consumed by Docker images, containers, volumes, and build cache, understanding the total, active, size, and reclaimable space for each component.

Subsequently, you explored how to obtain more detailed information about Docker disk usage using the -v flag with docker system df. This verbose output provides lists of individual images, containers, and volumes, allowing you to identify specific items consuming disk space.