Docker Cleanup & Disk Space Crisis

How to Safely Free Disk Space on an EC2 Free Tier Server

Introduction

Disk space issues are one of the most common problems on AWS EC2 Free Tier servers.
When disk space runs out, Docker commands stop working, containers fail to start, and deployments break.

Because of this, even a healthy application can suddenly go down.

In this blog, we will solve a real-world Docker disk space crisis.
We will learn how to identify disk usage issues and clean unused Docker resources safely, without stopping running applications.

This guide is written especially for freshers and junior DevOps engineers.


Problem Statement

Your company’s EC2 Free Tier server suddenly stops working because the disk becomes full, leading to multiple operational issues. As a result, Docker commands begin to fail, new containers cannot be created, and deployments stop unexpectedly. This situation typically occurs when Docker resources are not cleaned up on a regular basis, causing old Docker images to remain on disk, stopped containers to accumulate, and unused volumes to consume valuable storage space. As a Junior DevOps Engineer, the task is to identify the root cause of the disk usage problem and safely clean up unused Docker resources to restore normal system operation.


Why This Happens on Free Tier Servers

AWS Free Tier EC2 instances have very limited disk space (often 8–10 GB).

Because of this:

  • Every unused image matters
  • Every stopped container adds risk
  • Every unused volume wastes space

If Docker cleanup is ignored, disk space slowly fills up until the server crashes.


Check Overall Disk Usage

First, we confirm that disk space is the real issue.

Run the following command:

df -h
What this shows
  • Total disk size
  • Used space
  • Available space

If usage is close to 100%, disk space is the problem.


Check Docker Disk Usage

Next, we check how much space Docker is consuming.

docker system df
This command shows:
  • Space used by images
  • Space used by containers
  • Space used by volumes
  • Reclaimable space

This gives a clear picture of what is filling the disk.


List Running Containers (Safety Check)

Before deleting anything, we must protect running applications.

Run:

docker ps
Why this matters
  • Running containers should never be removed
  • Cleanup must not affect production services

Always check this before cleanup.


Remove Stopped Containers

Stopped containers often pile up and waste disk space.

To list all containers (including stopped ones):

docker ps -a

To remove only stopped containers:

docker container prune

Docker will ask for confirmation.
Type y and press Enter.

Running containers remain safe.


Remove Unused Docker Images

Old images consume a large amount of disk space.

To remove unused images only:

docker image prune

To remove all unused images (recommended on Free Tier):

docker image prune -a

This command removes:

  • Images not used by any container
  • Old and dangling images

As a result, disk space is freed safely.


Remove Unused Docker Volumes

Unused volumes are dangerous because they silently consume disk space.

To remove unused volumes:

docker volume prune
Important:
  • Only unused volumes are removed
  • Volumes attached to running containers stay safe

One-Command Cleanup (Use Carefully)

Docker also provides a full cleanup command.

docker system prune

This removes:

  • Stopped containers
  • Unused networks
  • Dangling images

For aggressive cleanup on Free Tier servers:

docker system prune -a

Use this only after checking running containers.


Verify Disk Space After Cleanup

After cleanup, always verify disk usage again.

df -h

You should now see significantly more free space.

Also confirm Docker usage:

docker system df

Best Practices to Avoid Disk Space Crises

To prevent future issues:

  • Clean Docker resources weekly
  • Avoid building unnecessary images
  • Remove unused containers after testing
  • Monitor disk usage regularly
  • Use docker system df often

Small habits prevent big outages.


What You Learn from This Task

After completing this task, you gain a clear understanding of how Docker uses disk storage and how images, containers, and volumes move through their respective lifecycles. You also learn how to safely clean up unused Docker resources without impacting running services, which is a critical skill when managing limited environments such as Free Tier servers. More importantly, this task builds practical experience in handling disk-full incidents, a common real-world problem in DevOps roles. These skills are essential for maintaining stable systems and are highly valued in real DevOps jobs.


Conclusion

Docker disk space issues are common in real-world environments, especially on resource-limited servers, but they become much easier to handle once the fundamentals are clearly understood. By identifying unused images, stopped containers, and orphaned volumes, and cleaning them safely, a DevOps engineer can quickly restore server health without causing downtime or disrupting running applications. On Free Tier servers, where disk space is extremely limited, this knowledge is not optional—it is essential for maintaining system stability. Mastering Docker cleanup practices not only prevents unexpected failures but also reflects the discipline and responsibility expected from a reliable DevOps engineer.