How to use the command 'docker system' (with examples)
Docker is a popular platform for containerization, and the ‘docker system’ command is used to manage Docker data and display system-wide information. It provides various subcommands to perform different tasks related to Docker system operations. In this article, we will explore each of these use cases with examples to better understand how to use the ‘docker system’ command effectively.
Use case 1: Show help
Code:
docker system
Motivation: The ‘docker system’ command without any subcommand displays the help message, providing an overview of the available subcommands and how to use them. This is helpful for beginners who are getting started with Docker and need information about the command and its available options.
Explanation:
docker system
: This is the main command to manage Docker data and display system-wide information.
Example output: The output will contain a brief description of the ‘docker system’ command and a list of available subcommands along with a short description of each subcommand.
Use case 2: Show docker disk usage
Code:
docker system df
Motivation: As you start using Docker and creating containers, it’s important to keep track of your disk usage. The ‘docker system df’ command provides valuable information about the disk space used by Docker, including the total space, used space, and available space. This helps in managing the disk usage efficiently and identifying any potential space constraints.
Explanation:
docker system
: The main command to manage Docker data and display system-wide information.df
: The subcommand to display Docker disk usage.
Example output:
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 5 2 3.56GB 1.23GB (34%)
Containers 3 1 452B 0B (0%)
Local Volumes 1 1 2.46MB 1.23MB (50%)
The output consists of different types of Docker resources, including images, containers, and local volumes, along with details such as the total count, active count, size, and reclaimable space.
Use case 3: Show detailed information on disk usage
Code:
docker system df --verbose
Motivation: By default, the ‘docker system df’ command provides a summary of disk usage. However, sometimes you may need more detailed information. The ‘–verbose’ flag allows you to get more detailed information about each resource type, such as individual image sizes and container sizes. This can be useful when troubleshooting disk space issues or identifying large resources that can be cleaned up.
Explanation:
docker system
: The main command to manage Docker data and display system-wide information.df
: The subcommand to display Docker disk usage.--verbose
: An optional flag to enable verbose output, providing detailed information on each resource type.
Example output:
TYPE NAME SIZE RECLAIMABLE
Image nginx 2.13GB 1.32GB (62%)
Image alpine 1.42GB 493MB (34%)
Container brave_visvesvaraya 12B 0B (0%)
The output includes the name of each resource (image or container), its size, and the reclaimable space. This information can help in identifying specific resources that may be consuming a significant amount of disk space.
Use case 4: Remove unused data
Code:
docker system prune
Motivation: Over time, Docker may accumulate unused resources, such as images, containers, and volumes, taking up valuable disk space. The ‘docker system prune’ command allows you to remove these unused data, helping to free up disk space and improve the overall performance of your Docker environment.
Explanation:
docker system
: The main command to manage Docker data and display system-wide information.prune
: The subcommand to remove unused data.
Example output:
Deleted Containers:
e14ac4199deb8f9058b20fdd9fe8be1c24b0cda87476d992f7712cb74a525b6e
f6217a5b983bcef9a608133a834a6c01a9a6cd1361df9dcbabac6e3726150844
Deleted Networks:
bridge
my-network
Total reclaimed space: 5.7MB
The output provides a list of deleted containers and networks along with the total amount of disk space reclaimed by removing the unused data.
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: Sometimes you may want to remove only the unused data that was created more than a specific amount of time ago. The ‘–filter’ option with the ‘until’ argument allows you to specify the time interval for removing the unused resources. This can be useful when you want to clean up old resources automatically and keep your Docker environment tidy.
Explanation:
docker system
: The main command to manage Docker data and display system-wide information.prune
: The subcommand to remove unused data.--filter="until=1h30m"
: An optional filter to remove only the unused data created more than 1 hour and 30 minutes ago.
Example output:
Deleted Images:
sha256:3b0aee263fe78e8c43055815bcc2fcf6f5a3de0fc89466eced64e4644c6017f7
sha256:793fb8d3c389cb4fb983efcb38294c727d636316a97ac9679b7f62bb0b02f9cf
Total reclaimed space: 2.62GB
The output includes the list of deleted images and the total amount of disk space reclaimed by removing the unused data.
Use case 6: Display real-time events from the Docker daemon
Code:
docker system events
Motivation: Monitoring the events happening within Docker can be crucial for troubleshooting, debugging, and understanding the overall behavior of your Docker environment. The ‘docker system events’ command allows you to view real-time events emitted by the Docker daemon, providing valuable insights into container creation, network changes, and other important activities.
Explanation:
docker system
: The main command to manage Docker data and display system-wide information.events
: The subcommand to display real-time events from the Docker daemon.
Example output:
2022-01-01T12:00:00Z container create alpine (image=sha256:9876abcxyz)
2022-01-01T12:10:00Z network connect bridge (container=alpine, network=bridge)
The output shows a stream of events with a timestamp and a description of the event, such as container creation and network connection.
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 some cases, you may need to parse the Docker events programmatically or integrate them with other tools and systems. The ‘–format’ option with the ‘json’ argument allows you to stream the events as valid JSON Lines, making it easier to process and extract relevant information from the events.
Explanation:
docker system
: The main command to manage Docker data and display system-wide information.events
: The subcommand to display real-time events.--filter 'type=container'
: An optional filter to show only the events related to containers.--format 'json .'
: An optional format option to stream the events as valid JSON Lines.
Example output:
{"status":"create","id":"98765","from":"busybox"}
{"status":"die","id":"12345","from":"nginx"}
The output represents JSON objects where each line corresponds to an event, including information such as the status, ID, and image used.
Use case 8: Display system-wide information
Code:
docker system info
Motivation: Getting an overall view of the Docker system configuration and resources can be useful for troubleshooting, performance optimization, and understanding the capabilities of your Docker environment. The ‘docker system info’ command provides detailed information about the Docker system, including version, storage driver, CPU architecture, and more.
Explanation:
docker system
: The main command to manage Docker data and display system-wide information.info
: The subcommand to display system-wide information.
Example output:
Client:
Context: default
Debug Mode: false
Server:
Containers: 15
Running: 5
Paused: 2
Images: 26
Server Version: 20.10.7
Storage Driver: overlay2
Platform: linux/amd64
The output includes information about the Docker client, server, container count (running and paused), image count, server version, storage driver, and platform details.
Conclusion:
The ‘docker system’ command provides a wide range of functionality to manage Docker data and display system-wide information. It allows you to monitor disk usage, remove unused data, view real-time events, and get detailed system information. By understanding each of these use cases and their examples, you can effectively utilize the ‘docker system’ command to streamline your Docker operations and maintain a healthy Docker environment.