How to prevent Docker bloat

If you use Docker long enough, you will eventually run out of disk space. Docker rarely deletes anything on its own, so old files pile up quietly until your computer starts slowing down or running out of room.

Docker makes running apps simple, but it can quickly fill up your drive. Here is how to see what is taking up space and how to clean it up safely without breaking your daily work.


Why Docker Eats Your Storage

Every time you build or run apps, Docker leaves behind files:

  • Dangling Images: Old image layers that are no longer used or labeled.
  • Stopped Containers: Old test runs that are shut down but still saved on disk.
  • Build Cache: Temporary files Docker keeps to make future builds faster.
  • Unused Volumes: Storage folders left behind after containers are deleted.

Step 1: Check Your Disk Usage First

Before deleting anything, see where your space is actually going:

docker system df

This command gives you a clear summary of your images, containers, and cache. Look at the RECLAIMABLE column—that shows how much space you can easily free up.

If you want to see a detailed list of every single item:

docker system df -v

Step 2: Clean Up Old Files

Option A: Safe Routine Cleanup

Use this regularly to clean up junk without touching anything you actively use:

docker system prune

What this deletes:

  • All stopped containers.
  • Unused networks.
  • Dangling images (layers without names).
  • Old build cache files.

Option B: Deep Clean (When You Need Lots of Space)

If you are very low on drive space, run a full cleanup:

docker system prune -a --volumes
⚠️ Watch Out: The -a flag deletes every image not currently running in an active container. The next time you start a project, Docker will have to download base images again.

3 Simple Habits to Stop Bloat Early

  • Use multi-stage builds: Only copy final program files into your final container, not heavy build tools.
  • Add a .dockerignore file: Keep large local folders like node_modules or .git out of your build.
  • Use --rm for quick tests: When running temporary containers, add --rm so Docker deletes them the moment they stop:
    docker run --rm -it alpine sh

Running out of space is rarely a hardware problem—it is usually just buildup. Taking a few seconds to run routine cleanups will keep your machine fast and prevent surprise errors down the road.

Comments

Popular posts from this blog

How to Check for NVIDIA GPU and Set It Up in Ubuntu

Use the Google Custom Search JSON API to retrieve images

How to Remove a File or Folder from Git Without Losing It Locally