Managing Docker System
1. Viewing System Information
| Command | Description | Example |
|---|---|---|
| docker info | Displays system-wide information about Docker | docker info |
| docker version | Shows the Docker version information | docker version |
| docker system df | Shows disk usage of Docker objects | docker system df |
| docker system prune | Removes unused containers, networks, images, and build cache | docker system prune |
Note: Use
docker system prune -a to remove all unused images, not just dangling ones.2. Viewing Version Information
| Command | Description | Output Example |
|---|---|---|
| docker version | Shows Docker client and server versions | |
| docker info | Displays system-wide information about Docker | |
| docker --version | Prints the Docker version in a simple format | Docker version 27.4.0, build abc12345 |
Example: Check Docker version
$ docker version
Client: Docker Engine - Community
Version: 27.4.0
Context: default
Debug Mode: false
Server: Docker Engine - Community
Engine:
Version: 27.4.0
API version: 1.45 (minimum version 1.24)
3. Checking Disk Usage
| Command | Description | Notes |
|---|---|---|
| docker system df | Shows disk usage of Docker objects | Displays space used by images, containers, volumes, and build cache |
| docker system df -v | Verbose disk usage output | Includes detailed breakdown of each object type and size |
| docker system prune | Remove unused Docker objects | Removes stopped containers, networks, images, and build cache |
| docker system prune -a | Remove all unused objects | Removes all images not referenced by containers, including dangling ones |
Example: Check disk usage
$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 2 0 1.2GB 1.2GB (100%)
Containers 3 0 500MB 500MB (100%)
Local Volumes 2 0 300MB 300MB (100%)
Build Cache 1 0 200MB 200MB (100%)
Example: Prune unused objects
$ docker system prune -a
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all images not used by at least one container
- all build cache
Are you sure you want to remove all unused objects? [y/N] y
Total reclaimed space: 2.2GB
4. Monitoring System Events
| Option | Description | Default |
|---|---|---|
| --since | Filter events from a specific time ago (e.g., 1h, 30m) | None |
| --until | Filter events until a specific time (e.g., 2024-01-01T12:00:00) | None |
| --filter | Filter events by specific criteria (e.g., event=stop, container=nginx) | None |
| --format | Format output using Go template syntax | {{.Time}} {{.Status}} |
Note: The
docker events command streams real-time system events from the Docker daemon.Example: Format Events with Custom Template
docker events --format "{{.Time}} | {{.Type}} | {{.Action}} | {{.Container}}" --since 10m
5. Pruning System Resources
| Command | Description | Flags |
|---|---|---|
| docker system prune | Remove unused containers, networks, images (dangling and intermediate), and optionally build cache | -a to remove all unused images not just dangling ones |
| docker system prune -a | Remove all unused containers, networks, images (dangling and intermediate), and build cache | --force to skip confirmation prompt |
| docker system prune --volumes | Remove unused volumes as well | -a to remove all unused images and build cache |
Note: Running
docker system prune will remove all stopped containers, unused networks, dangling images, and build cache. Use -a flag to also remove all unused images.
6. Cleaning Up Build Cache
| Command | Description | Flags |
|---|---|---|
| docker builder prune | Remove all unused build cache | -a to remove all cache, --filter to filter by age or label |
| docker builder prune --all | Remove all build cache including images | --force to skip confirmation |
Note: This command removes build cache but does not remove images or containers. Use
docker system prune to remove all unused resources.
7. Removing All Stopped Containers
| Command | Description | Notes |
|---|---|---|
| docker container prune | Remove all stopped containers | Does not remove containers that are running |
Note: Use
docker container prune -f to skip confirmation prompt.
8. Removing All Unused Resources
| Command | Description | Flags |
|---|---|---|
| docker system prune | Remove unused containers, networks, images, and build cache | -a or --all to remove all unused resources |
| docker system prune -a | Remove all unused resources including stopped containers, networks, images, and build cache | -f or --force to skip confirmation prompts |
Note: The
docker system prune -a command removes all unused Docker resources, including stopped containers, networks, images, and build cache. Use with caution as it is irreversible.
Warning: This command removes all unused resources, including images that are not tagged or referenced by any container. Ensure you have backed up any important data before running this command.
Summary of docker system prune
- Prune containers: Remove stopped containers
- Prune networks: Remove unused networks
- Prune images: Remove unused images
- Prune build cache: Remove build cache
9. Forcing Removal
Note: The following commands force removal of Docker objects, bypassing normal safety checks.
| Command | Description | Flags |
|---|---|---|
| docker rm | Remove one or more containers | -f to force removal of running containers |
| docker rmi | Remove one or more images | -f to force removal of images that are in use |
| docker volume rm | Remove one or more volumes | -f to force removal of volumes in use |
| docker network rm | Remove one or more networks | -f to force removal of networks in use |
10. Filtering Resources for Pruning
| Resource Type | Filter Option | Description |
|---|---|---|
| Images | docker image prune --filter |
Prune unused images based on filters |
| Containers | docker container prune --filter |
Prune stopped containers based on filters |
| Volumes | docker volume prune --filter |
Prune unused volumes based on filters |
| Networks | docker network prune --filter |
Prune unused networks based on filters |
| Build Cache | docker builder prune --filter |
Prune build cache based on filters |
11. Viewing Live Resource Usage
| Option | Description | Default |
|---|---|---|
| --no-stream | Do not stream stats, only display once | false |
| --format | Format output using a Go template | |
| --all | Display stats for all containers | false |
Note: The
docker stats command provides real-time metrics including CPU, memory, network I/O, and disk I/O for running containers.
12. Managing Docker Data Directory
Note: The Docker data directory is where Docker stores all its data including images, containers, volumes, and networks. By default, this is located at
/var/lib/docker on Linux systems.| Configuration Option | Default Value | Description |
|---|---|---|
| data-root | /var/lib/docker |
Root directory for Docker data storage |
Example: Changing Docker Data Directory
# Stop Docker service
sudo systemctl stop docker
# Move existing data to new location (if needed)
sudo mv /var/lib/docker /new/path/to/docker
# Update Docker daemon configuration
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<EOF
{
"data-root": "/new/path/to/docker"
}
EOF
# Restart Docker service
sudo systemctl start docker
Workflow: Migrating Docker Data Directory
- Stop the Docker service to prevent data changes during migration
- Backup existing Docker data if necessary
- Move the contents of
/var/lib/dockerto the new location - Update
/etc/docker/daemon.jsonwith the newdata-rootpath - Restart Docker daemon to apply changes
Warning: Ensure sufficient disk space in the new location and that Docker has proper permissions to access it. Incorrect configuration can lead to Docker failing to start or data loss.