Docker
disk usage
container analysis
Docker monitoring
storage optimization

How to analyze disk usage of a Docker container

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Analyzing the disk usage of a Docker container is critical for maintaining efficient container performance and resource allocation. Understanding how a container consumes disk space can help identify issues related to storage and inform optimizations. This article will guide you through several methods to analyze disk usage within Docker containers, using both Docker CLI commands and third-party tools. Additionally, it includes tips for interpreting and optimizing disk usage data.

Understanding Docker Storage

Docker employs a layered storage architecture where each image and container is composed of layers. These layers represent filesystem changes, such as file additions, modifications, or deletions. Disk usage analysis can target both the Docker image and the running container levels.

Inspecting Disk Usage with Docker CLI

1. docker system df

One of the simplest ways to get an overview of Docker's disk usage is by using the docker system df command. This command provides a summary of space used by images, containers, local volumes, and the build cache.

bash
$ docker system df

Example Output:

plaintext
1TYPE            TOTAL          ACTIVE       SIZE         RECLAIMABLE
2Images          10             5            3.563GB      1.25GB (35%)
3Containers      8              2            400MB        250MB (62%)
4Local Volumes   4              3            120MB        50MB (41%)
5Build Cache     0              0            0B           0B

2. docker ps -s

To look closer at individual container sizes, use docker ps -s. It provides information about the size of each container's writable layer.

bash
$ docker ps -s

Example Output:

plaintext
CONTAINER ID   IMAGE           COMMAND     SIZE         CREATED        STATUS
a2b3c4d5e6f7   my-image:tag    "/bin/bash" 130MB (RW)   30 hours ago   Up 17 hours
  • SIZE column: Reflects the writable layer size.

3. docker stats

While docker stats is primarily for real-time metrics regarding CPU and memory, it also shows disk I/O which can give insights into how a container is interacting with stored data.

bash
$ docker stats

Analyzing Disk Usage Inside a Container

1. Using du and ncdu

To analyze file-level disk usage within a container, you can execute tools like du or ncdu using docker exec.

Example using du:

bash
$ docker exec <container_id> du -sh /path/to/directory

Example using ncdu (Interactive):

  1. Install ncdu inside the container, if not available:
bash
$ docker exec -it <container_id> apt-get update && apt-get install -y ncdu
  1. Run ncdu:
bash
$ docker exec -it <container_id> ncdu /path/to/directory

Optimizing Disk Usage

Understanding and optimizing a container’s disk usage can lead to cost-saving and performance improvements.

1. Prune Unused Data

Docker allows cleaning up unused images, containers, volumes, and networks with the docker system prune command, which helps free up valuable storage:

bash
$ docker system prune -f
  • Use -a to remove all unused images, not just dangling ones.

2. Layer Caching

Optimize Dockerfile to make use of layer caching. Changes in layers can cause redundant data storage. Pay attention to:

  • Ordering of commands or instructions.
  • Minimizing the number of layers by combining commands.

3. Multi-stage Builds

Use multi-stage builds to reduce image sizes by copying only required artifacts to the final image. This helps in limiting the number of unnecessary files in the production image.

dockerfile
1FROM golang:alpine AS builder
2WORKDIR /app
3COPY . .
4RUN go build -o myapp
5
6FROM alpine
7WORKDIR /app
8COPY --from=builder /app/myapp .
9CMD ["./myapp"]

Summarized Data in Table

Here is a summary table that encapsulates key commands and their purposes:

Command/ToolPurposeDescription
docker system dfDisk usage overviewSummarizes images, containers, and volumes.
docker ps -sContainer size informationProvides writable layer size.
docker statsReal-time metricsIncludes disk I/O impact.
duDirectory size checkUse inside container for precise size analysis.
ncduInteractive disk usage viewerEffective for traversing directories.
docker system pruneDisk space cleanupRemoves unused data efficiently.

Conclusion

Regular monitoring and analyzing of Docker container disk usage are essential tools for maintaining optimized and efficient application environments. By leveraging the aforementioned tools and techniques, developers and system administrators can ensure smoother operations and optimize disk space utilization. The effective management of Docker storage translates into cost savings and improved application performance.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.