Bird
Raised Fist0
Dockerdevops~5 mins

Container disk usage management in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
Containers can use a lot of disk space over time, which can slow down your system or fill up your storage. Managing container disk usage helps keep your system clean and running smoothly by removing unused data.
When your system storage is running low due to leftover container data
When you want to free up space by deleting stopped containers and unused images
When you want to check how much disk space your containers and images are using
When you want to clean up unused volumes that take up space
When you want to maintain a healthy environment by regularly removing unused Docker resources
Commands
This command shows how much disk space is used by Docker images, containers, volumes, and build cache. It helps you understand where your disk space is going.
Terminal
docker system df
Expected OutputExpected
TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 5 3 2.5GB 1.2GB (48%) Containers 4 2 500MB 300MB (60%) Local Volumes 3 2 1.1GB 700MB (63%) Build Cache 0 0 0B 0B
This command removes all stopped containers to free up disk space. The -f flag skips the confirmation prompt for faster cleanup.
Terminal
docker container prune -f
Expected OutputExpected
Deleted Containers: 8a7f3c2d4b1e b3c9d7a1e2f4 Total reclaimed space: 300MB
→
-f - Force removal without confirmation prompt
This command removes all unused images, including dangling and unreferenced images, to free up disk space. The -a flag means all unused images, and -f skips confirmation.
Terminal
docker image prune -a -f
Expected OutputExpected
Deleted Images: sha256:abc123def456 sha256:def789abc012 Total reclaimed space: 1.2GB
→
-a - Remove all unused images, not just dangling ones
→
-f - Force removal without confirmation prompt
This command removes all unused volumes that are not referenced by any container, freeing up disk space. The -f flag skips confirmation.
Terminal
docker volume prune -f
Expected OutputExpected
Deleted Volumes: my_unused_volume Total reclaimed space: 700MB
→
-f - Force removal without confirmation prompt
Key Concept

If you remember nothing else from this pattern, remember: regularly check and clean unused Docker resources to keep your disk space healthy.

Common Mistakes
Running prune commands without the -f flag and expecting no prompt
The command will pause and wait for confirmation, which can interrupt automated scripts or workflows.
Use the -f flag to force removal without confirmation when you want non-interactive cleanup.
Removing images without understanding which are in use
You might delete images that are still needed by running containers, causing errors or downtime.
Check active containers and images with 'docker system df' before pruning images.
Ignoring volumes when cleaning up
Unused volumes can take up significant disk space but are not removed by container or image pruning.
Use 'docker volume prune' to clean up unused volumes regularly.
Summary
Use 'docker system df' to see disk usage by Docker resources.
Remove stopped containers with 'docker container prune -f' to free space.
Clean unused images with 'docker image prune -a -f' safely.
Remove unused volumes using 'docker volume prune -f' to reclaim disk space.

Practice

(1/5)
1. What does the docker system df command show you?
easy
A. The disk space used by Docker images, containers, and volumes
B. The list of running Docker containers
C. The network settings of Docker containers
D. The logs of a specific Docker container

Solution

  1. Step 1: Understand the purpose of docker system df and compare options

    This command summarizes disk usage by Docker objects like images, containers, and volumes. Options B (docker ps), C (network settings), and D (logs) describe unrelated commands.
  2. Final Answer:

    The disk space used by Docker images, containers, and volumes -> Option A
  3. Quick Check:

    Disk usage summary = A [OK]
Hint: Remember 'df' means disk free or usage summary [OK]
Common Mistakes:
  • Confusing disk usage with container status
  • Thinking it shows logs or network info
  • Using it to list running containers
2. Which of the following commands correctly removes only all stopped containers to free disk space?
easy
A. docker container prune
B. docker image rm $(docker images -q)
C. docker system prune -a
D. docker container rm -f $(docker ps -a -q)

Solution

  1. Step 1: Identify command to remove only stopped containers and check options

    docker container prune safely removes all stopped containers only. A forcibly removes all containers (running and stopped). B removes images. C removes stopped containers plus unused images and networks (more than containers).
  2. Final Answer:

    docker container prune -> Option A
  3. Quick Check:

    Remove stopped containers = D [OK]
Hint: Use prune commands to safely clean stopped containers [OK]
Common Mistakes:
  • Using force remove on all containers including running ones
  • Confusing image removal with container removal
  • Using system prune when only containers should be removed
3. What will be the output of the command docker system df after running docker image prune -a?
medium
A. Error message about running containers
B. Disk usage report showing fewer images and freed space
C. List of all images including unused ones
D. No change in disk usage report

Solution

  1. Step 1: Understand docker image prune -a effect on docker system df

    This removes all unused images, freeing space, so the report shows fewer images and more free space. No error, no list, and usage changes.
  2. Final Answer:

    Disk usage report showing fewer images and freed space -> Option B
  3. Quick Check:

    Image prune frees space = B [OK]
Hint: Prune unused images to reduce disk usage [OK]
Common Mistakes:
  • Expecting an error when pruning images
  • Thinking prune lists images instead of removing
  • Assuming no change in disk usage after prune
4. You ran docker volume prune but still see high disk usage. What could be the reason?
medium
A. The prune command only cleans network settings
B. The prune command deleted running containers by mistake
C. Docker does not track volume disk usage
D. There are unused images and stopped containers still taking space

Solution

  1. Step 1: Understand docker volume prune and reason for remaining usage

    It removes unused volumes only. Unused images and stopped containers remain, causing high usage. No deletion of running containers or network cleaning.
  2. Final Answer:

    There are unused images and stopped containers still taking space -> Option D
  3. Quick Check:

    Volume prune only removes volumes = A [OK]
Hint: Prune volumes only frees volume space, check images and containers too [OK]
Common Mistakes:
  • Assuming volume prune deletes containers
  • Thinking prune affects network settings
  • Believing Docker ignores volume disk usage
5. You want to free maximum disk space but keep your running containers and their data intact. Which command should you use?
hard
A. docker container rm -f $(docker ps -q)
B. docker system prune --volumes
C. docker system prune -a --volumes
D. docker image rm $(docker images -q)

Solution

  1. Step 1: Analyze options for maximum safe cleanup

    docker system prune -a --volumes removes all stopped containers, all unused images (not just dangling), unused networks, and unused volumes--maximizing space--while keeping running containers and their data intact. C frees less (no -a). A removes running containers. D only images, unsafely and incompletely.
  2. Final Answer:

    docker system prune -a --volumes -> Option C
  3. Quick Check:

    Max prune safely, keep running containers = B [OK]
Hint: Use system prune with volumes to clean safely without stopping containers [OK]
Common Mistakes:
  • Using prune without -a flag, freeing less space
  • Force removing running containers causing downtime
  • Removing images only without cleaning volumes or containers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard