DEV Community

Ajit Kumar
Ajit Kumar

Posted on

Breathing Room: Expanding Your AWS EC2 Storage for a Smooth Docker Deployment

It's a familiar scenario for developers: you've deployed your awesome Django app to AWS EC2 using Docker, everything's running smoothly, then suddenly your disk fills up. Docker builds fail, databases halt, and your once-snappy app grinds to a halt. Often, the culprit is an undersized root volume on your EC2 instance.

In this guide, we'll walk through the process of safely expanding your EC2 storage, verifying the changes, and setting up automated cleanup routines to keep your Docker environment lean and performant.

The Problem: Running Out of Disk Space

Your EC2 instance (especially if you started with the default 8GB) can quickly fill up with:

  • Docker Images: Each docker compose build adds layers to your image cache.
  • Container Logs: Even with basic logging, these can grow to gigabytes.
  • Database Data: Postgres, over time, will consume more space.
  • Application Logs & Cache: Django's internal logging or any caching can accumulate.

When you hit 90% disk utilization, things get unstable. At 100%, your server stops functioning correctly.

Identifying the Problem: Before Expansion

Let's check your current disk status. SSH into your EC2 instance and run:

df -h

Enter fullscreen mode Exit fullscreen mode

Example Output (Before Expansion):

Filesystem      Size  Used Avail Use% Mounted on
/dev/root       6.8G  4.7G  2.1G  70% /  <-- Problem area!
tmpfs           1.9G     0  1.9G   0% /dev/shm
...

Enter fullscreen mode Exit fullscreen mode

Here, /dev/root is at a concerning 70% utilization, with only 2.1GB remaining. This is a ticking time bomb for any Dockerized application.

The Solution: Expanding Your EBS Volume

This process involves two main steps: first, telling AWS to allocate more "hardware" space, and then telling your Linux operating system to recognize and use that new space.

Part 1: Increase Volume Size in AWS Console

This step modifies the underlying Elastic Block Storage (EBS) volume.

  1. Log in to your AWS Management Console.
  2. Navigate to EC2 > Elastic Block Store > Volumes.
  3. Select the EBS volume attached to your running EC2 instance. It's usually labeled with your instance ID or name. (For our example, it was initially 8 GiB).
  4. Click Actions > Modify Volume.
  5. In the "Modify Volume" dialog, change the Size to your desired new size (e.g., from 8 GiB to 20 GiB).
  6. Click Modify and confirm the change.

AWS will now begin allocating the new space. You'll see the volume status change from "modifying" to "optimizing" in the console. You can proceed to the next steps while it's optimizing; there's no need to wait for it to complete.

Part 2: Expand the Filesystem in Ubuntu

Even though AWS has added more space, your Ubuntu OS is still operating on the old partition size. We need to tell it to "stretch" its main partition (/dev/root) to fill the newly available space.

Step 2.1: Verify New Disk Size

First, let's confirm that Linux sees the larger disk, even if it hasn't used it yet. SSH into your EC2 instance and run:

lsblk

Enter fullscreen mode Exit fullscreen mode

Example Output (After AWS Modification, Before OS Expansion):

NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
...
nvme0n1     259:0    0   20G  0 disk  <-- Disk now 20G!
├─nvme0n1p1 259:1    0    7G  0 part /   <-- But partition 1 is still 7G
├─nvme0n1p14 259:2   0    4M  0 part
├─nvme0n1p15 259:3   0  106M  0 part /boot/efi
└─nvme0n1p16 259:4   0  913M  0 part /boot

Enter fullscreen mode Exit fullscreen mode

Notice how nvme0n1 (the disk) now shows 20G, but nvme0n1p1 (the root partition mounted at /) is still 7G.

Step 2.2: Expand the Partition

Now, use growpart to expand the partition. The command structure is sudo growpart /dev/[DISK_NAME] [PARTITION_NUMBER].
From our lsblk output, the disk name is nvme0n1 and the root partition number is 1.

sudo growpart /dev/nvme0n1 1

Enter fullscreen mode Exit fullscreen mode

You should see output similar to: CHANGED: partition=1 start=2048 old: size=14680064 new: size=41940958. This confirms the partition table has been updated.

Step 2.3: Resize the Filesystem

Finally, tell the filesystem (usually ext4 on Ubuntu) to use the newly expanded partition space.

sudo resize2fs /dev/nvme0n1p1

Enter fullscreen mode Exit fullscreen mode

You'll see a message like The filesystem on /dev/nvme0n1p1 is now 41940958 (4k) blocks long. indicating success.

Verification: After Expansion

Run df -h again to confirm the new, larger available space:

df -h

Enter fullscreen mode Exit fullscreen mode

Example Output (After Complete Expansion):

Filesystem      Size  Used Avail Use% Mounted on
/dev/root        19G  4.7G   14G  26% /  <-- Beautiful!
tmpfs           1.9G     0  1.9G   0% /dev/shm
...

Enter fullscreen mode Exit fullscreen mode

Fantastic! Your root partition is now 19GB, with a healthy 14GB free, and a comfortable 26% utilization. This gives your Sioux Web App plenty of room to breathe, grow, and execute Docker builds without crashing.

Proactive Maintenance: Automated Docker Cleanup

Even with expanded storage, Docker builds can leave behind "dangling" images and stopped containers that slowly consume disk space. To keep your system pristine, set up a weekly cleanup.

1. Create a Cleanup Script

nano ~/clean_docker.sh

Enter fullscreen mode Exit fullscreen mode

Paste the following content:

#!/bin/bash
# This script performs a system-wide cleanup of Docker resources
# and prunes old system journal logs to free up disk space.

echo "Starting Docker system prune..."
# Removes:
#   - All stopped containers
#   - All networks not used by at least one container
#   - All dangling images (images not associated with any container)
#   - All dangling build cache
# The -f flag forces the removal without confirmation.
docker system prune -af

echo "Pruning Docker volumes..."
# Removes unused local volumes.
# CAUTION: Ensure no critical data is in unreferenced volumes before running in production.
# This command requires manual confirmation to prevent accidental data loss.
docker volume prune -f

echo "Cleaning up old system journal logs (older than 7 days)..."
# Frees up space by removing system logs.
sudo journalctl --vacuum-time=7d

echo "Disk cleanup complete."
df -h # Show disk usage after cleanup

Enter fullscreen mode Exit fullscreen mode

Save and exit (Ctrl+O, Enter, Ctrl+X).

2. Make the Script Executable

chmod +x ~/clean_docker.sh

Enter fullscreen mode Exit fullscreen mode

3. Schedule with Cron

We'll use cron to schedule this script to run automatically, for example, every Sunday at midnight.

crontab -e

Enter fullscreen mode Exit fullscreen mode

If prompted, select 1 for nano. Add the following line at the very end of the file:

0 0 * * 0 /home/ubuntu/clean_docker.sh

Enter fullscreen mode Exit fullscreen mode

Save and exit. This line means:

  • 0 0: At 0 minutes past 0 hours (midnight)
  • * *: Every day of the month, every month
  • 0: On the 0th day of the week (Sunday)
  • /home/ubuntu/clean_docker.sh: Execute your cleanup script

Conclusion

By following these steps, you've not only expanded your EC2 instance's storage but also implemented a robust maintenance strategy. Your Sioux Web App can now operate efficiently without the constant threat of disk space exhaustion, providing a more stable and reliable experience for your users.

Top comments (0)