Skip to main content

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
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)
docker info Displays system-wide information about Docker
Containers: 10
 Images: 25
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
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

Example: View Docker System Events

docker events --since 1h
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: Filter Events by Container

docker events --filter container=web-app --since 5m

Example: Format Events with Custom Template

docker events --format "{{.Time}} | {{.Type}} | {{.Action}} | {{.Container}}" --since 10m

5. Pruning System Resources

Example: Remove unused Docker objects

docker system prune
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.

Example: Prune with force flag

docker system prune -a --force

Example: Prune volumes and images

docker system prune --volumes -a

6. Cleaning Up Build Cache

Example: Remove unused build cache

docker builder prune
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.

Example: Prune with filters

docker builder prune --filter "until=24h"

7. Removing All Stopped Containers

Command Description Notes
docker container prune Remove all stopped containers Does not remove containers that are running

Example: Remove all stopped containers

docker container prune
Note: Use docker container prune -f to skip confirmation prompt.

8. Removing All Unused Resources

Example: Remove unused Docker resources

docker system prune -a
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

Example: Force remove a running container

docker rm -f my-running-container

Example: Force remove an image in use

docker rmi -f my-image:latest

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

Example: Pruning Images by Label

docker image prune --filter "label=environment=dev"

Example: Pruning Containers by Status

docker container prune --filter "status=exited"

Example: Pruning Volumes by Name Pattern

docker volume prune --filter "name=^myapp_"

11. Viewing Live Resource Usage

Example: Monitoring Container Resource Usage

docker stats container-name
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.

Example: Using Custom Format

docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"

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

  1. Stop the Docker service to prevent data changes during migration
  2. Backup existing Docker data if necessary
  3. Move the contents of /var/lib/docker to the new location
  4. Update /etc/docker/daemon.json with the new data-root path
  5. 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.

Was this article helpful?