Breathing Room: Expanding Your AWS EC2 Storage for a Smooth Docker Deployment
Source: Dev.to
The Problem: Running Out of Disk Space
Your EC2 instance (especially if you started with the default 8 GB) can quickly fill up with:
- Docker Images – each
docker compose buildadds layers to your image cache. - Container Logs – even basic logging can grow to gigabytes.
- Database Data – PostgreSQL will consume more space over time.
- Application Logs & Cache – Django’s internal logging or any caching can accumulate.
When you hit 90 % disk utilization, things get unstable. At 100 %, the server stops functioning correctly.
Identifying the Problem: Before Expansion
Check your current disk status. SSH into the EC2 instance and run:
df -h
Example Output (Before Expansion)
Filesystem Size Used Avail Use% Mounted on
/dev/root 6.8G 4.7G 2.1G 70% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
...
/dev/root is at a concerning 70 % utilization, with only 2.1 GB remaining. This is a ticking time bomb for any Dockerized application.
The Solution: Expanding Your EBS Volume
The process involves two main steps:
- Tell AWS to allocate more “hardware” space (modify the EBS volume).
- Tell Linux to recognize and use that new space (grow the partition and resize the filesystem).
Part 1 – Increase Volume Size in the AWS Console
- Log in to the AWS Management Console.
- Navigate to EC2 → Elastic Block Store → Volumes.
- Select the EBS volume attached to your running EC2 instance (usually labeled with the instance ID or name).
- Click Actions → Modify Volume.
- In the dialog, change Size to the desired new size (e.g., from 8 GiB to 20 GiB).
- Click Modify and confirm.
AWS will begin allocating the new space. The volume status will change from modifying to optimizing. You can proceed to the next steps while it’s optimizing—no need to wait for completion.
Part 2 – Expand the Filesystem in Ubuntu
Even though AWS has added more space, Ubuntu is still using the old partition size. We need to stretch the main partition (/dev/root) to fill the newly available space.
Verify New Disk Size
lsblk
Example Output (After AWS Modification, Before OS Expansion)
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
...
nvme0n1 259:0 0 20G 0 disk
Grow the Partition and Resize the Filesystem
# Install cloud‑init tools if not present
sudo apt-get update && sudo apt-get install -y cloud-guest-utils
# Grow the partition (assumes /dev/nvme0n1p1; adjust if different)
sudo growpart /dev/nvme0n1 1
# Resize the filesystem (ext4)
sudo resize2fs /dev/nvme0n1p1
Verify the Expansion
df -h
You should now see the increased capacity reflected for /dev/root.
Summary
- Resize the EBS volume in the AWS console.
- Grow the partition with
growpart. - Resize the filesystem with
resize2fs. - Verify the new space with
df -h. - Implement automated Docker cleanup to prevent future disk‑space emergencies.
By following these steps, your Django app on EC2 will have ample storage for Docker builds, logs, and database growth, while the cleanup routine ensures the environment stays tidy over time.
Docker & System Cleanup Script
#!/bin/bash
# clean_docker.sh – Remove unused Docker resources and old journal logs
echo "Pruning Docker system (containers, images, build cache)..."
docker system prune -af
echo "Pruning Docker volumes..."
docker volume prune -f
echo "Cleaning up old system journal logs (older than 7 days)..."
sudo journalctl --vacuum-time=7d
echo "Disk cleanup complete."
df -h
1. Save the Script
Create the file ~/clean_docker.sh and paste the script above into it. In nano, save and exit with:
Ctrl+O (Enter to confirm)
Ctrl+X
2. Make the Script Executable
chmod +x ~/clean_docker.sh
3. Schedule with Cron
We’ll use cron to run the script automatically, e.g., every Sunday at midnight.
crontab -e
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
Explanation
| Field | Meaning |
|---|---|
0 0 | At 0 minutes past 0 hours (midnight) |
* * | Every day of the month, every month |
0 | On the 0‑th day of the week (Sunday) |
/home/ubuntu/clean_docker.sh | Execute your cleanup script |
Save and exit (Ctrl+O, Enter, Ctrl+X).
Conclusion
By following these steps you have:
- Expanded your EC2 instance’s storage.
- Implemented a robust maintenance strategy that automatically removes unused Docker resources and old system logs.
Your Sioux Web App can now run efficiently without the constant threat of disk‑space exhaustion, delivering a more stable and reliable experience for your users.