How to use the command 'docker system' (with examples)
The docker system
command provides a suite of functions to help you manage Docker data and view system-wide information. By using this command, you can easily inspect disk usage, gain insights into real-time events, and remove unused data to optimize your Docker environment. The utility of docker system
extends to offering detailed discretion and control over your Docker operations, making it an essential tool for developers and system administrators managing Docker containers and images.
Use case 1: Display help
Code:
docker system
Motivation:
You might be new to Docker or wish to familiarize yourself with the functions that the docker system
command offers. By displaying the help documentation, you can quickly access the sub-commands and their descriptions, giving you a roadmap of how to navigate Docker system operations effectively.
Explanation:
- The command
docker system
alone prompts the Docker CLI to provide a list of available sub-commands and options under thedocker system
umbrella. This is akin to seeking an instruction manual, providing clarity on how to use each option in the command suite.
Example Output:
Usage: docker system COMMAND
Manage Docker data and display system-wide information
Commands:
df Show docker disk usage
events Get real-time events from the server
info Display system-wide information
prune Remove unused data
Run 'docker system COMMAND --help' for more information on a command.
Use case 2: Show Docker disk usage
Code:
docker system df
Motivation: As Docker containers and images accumulate, it’s crucial to keep track of your disk space. Over time, Docker components can consume a substantial amount of storage, potentially affecting system performance. By frequently checking disk usage, you can ensure efficient resource management and avoid unexpected storage shortages.
Explanation:
df
: This stands for “disk free,” and it’s used in the context of Docker to show how much space is occupied by Docker images, containers, and volumes, presenting a quick overview of storage consumption.
Example Output:
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 15 3 4.12GB 2.54GB (61%)
Containers 2 1 0B 0B
Local Volumes 10 2 1.3GB 1.0GB (76%)
Use case 3: Show detailed information on disk usage
Code:
docker system df --verbose
Motivation: If the basic disk usage report isn’t sufficient, or if you need to diagnose storage issues further, opting for a detailed view can be extremely beneficial. This detailed breakdown provides insights that help in troubleshooting, identifying which specific images or containers are using the most space.
Explanation:
--verbose
: This option extends the basic disk usage functionality, providing a more detailed account of disk usage statistics, including information on each specific image, container, or volume.
Example Output:
Images space usage:
REPOSITORY TAG IMAGE ID CREATED SIZE SHARED SIZE UNIQUE SIZE CONTAINERS
nginx latest 6678c7c2e56c 4 hours ago 133MB 133MB 0B 1
Containers space usage:
CONTAINER ID IMAGE COMMAND CREATED STATUS SIZE NAMES
f2f1c75b1c4e nginx "/docker-entrypoint.…" 4 hours ago Up 4 seconds 19.6kB vibrant_mestorf
Local Volumes space usage:
VOLUME NAME LINKS SIZE
mydata 1 200MB
Use case 4: Remove unused data
Code:
docker system prune
Motivation: Unused Docker data, such as stopped containers, dangling images, and networks, can unnecessarily clutter your system over time. Cleaning up this unused data is vital for maintaining an organized and efficient Docker environment, freeing up storage, and reducing potential security vulnerabilities.
Explanation:
prune
: Theprune
command is a cleanup utility to remove all stopped containers, unused networks, and dangling images in one sweep, simplifying the de-cluttering process.
Example Output:
Deleted Containers:
f2f1c75b1c4e
Deleted Images:
untagged: nginx:latest
deleted: sha256:6678c7c2e56c6b3e770f5ac6
Deleted Networks:
network-name
Total reclaimed space: 1.53GB
Use case 5: Remove unused data created more than a specified amount of time in the past
Code:
docker system prune --filter "until=1h30m"
Motivation: When managing a large-scale Docker environment, there might be a need to preserve certain data that is relatively fresh while clearing out older unused data. By specifying a time filter, you target data older than the given time, optimizing storage efficiently without affecting recent operations.
Explanation:
--filter "until=1h30m"
: This filter specifies the age threshold for removing unused data. In this context, it will remove all unused data that was created more than 1 hour and 30 minutes ago, providing greater control over the pruning process.
Example Output:
Deleted Containers:
d9b100f2f636
729b2c6488a6
Deleted Images:
untagged: myapp:old
deleted: sha256:b7ea58f1ab04
Deleted Networks:
old_network
Total reclaimed space: 500MB
Use case 6: Display real-time events from the Docker daemon
Code:
docker system events
Motivation: Monitoring real-time events within Docker is key for understanding actions happening within the daemon. This is critical in debugging, auditing, or simply gaining insights into the operational lifecycle of containers.
Explanation:
events
: This command listens for and displays events that occur within the Docker environment in real-time, providing continuous feedback on operations being performed, such as container starts or stops.
Example Output:
2019-08-14T22:13:21.947864940Z container create d9e7c6c8a7dc
2019-08-14T22:13:21.954656982Z container start d9e7c6c8a7dc
2019-08-14T22:14:00.123984012Z container exec_start d9e7c6c8a7dc
Use case 7: Display real-time events from containers streamed as valid JSON Lines
Code:
docker system events --filter 'type=container' --format 'json .'
Motivation: In certain scenarios, you might want a machine-readable format for processing container events. This is particularly useful for integrating Docker monitoring into automated scripts or logging systems where JSON format is commonly used for data interchange.
Explanation:
--filter 'type=container'
: Filters the output to show events only for containers, reducing noise from other types of events.--format 'json .'
: Formats the event output as JSON Lines, a format that makes it easy to parse with tools that support JSON.
Example Output:
{"status":"start","id":"d9e7c6c8a7dc","from": "nginx:latest","Type":"container","Action":"start","Actor":{"ID":"d9e7c6c8a7dc","Attributes":{"image":"nginx:latest","name":"vibrant_mestorf"}}}
{"status":"exec_start","id":"d9e7c6c8a7dc","from": "nginx:latest","Type":"container","Action":"exec_start","Actor":{"ID":"d9e7c6c8a7dc","Attributes":{"image":"nginx:latest","name":"vibrant_mestorf"}}}
Use case 8: Display system-wide information
Code:
docker system info
Motivation: Understanding the state of your Docker installation is vital for maintaining its stability and performance. Displaying system-wide information enables you to check environment settings, Docker version, available resources, and system load, providing a comprehensive overview of Docker’s current state on your machine.
Explanation:
info
: This command aggregates and displays general information about the Docker environment, including versions of Docker components, runtime information, and available resources such as CPUs and memory.
Example Output:
Containers: 2
Running: 1
Paused: 0
Stopped: 1
Images: 15
Server Version: 20.10.8
Storage Driver: overlay2
...
Architecture: x86_64
CPUs: 4
Total Memory: 7.775GiB
Conclusion:
The docker system
command is a versatile tool that aids in the deep management and monitoring of your Docker infrastructure. From inspecting disk usage to receiving real-time event updates, this command helps streamline Docker operations, optimize resource usage, and offers a window into the performance and health of your Docker environment. Whether you’re cleaning up storage or monitoring ongoing activities, mastering these sub-commands can empower you to efficiently manage your Docker systems with confidence.