EC2 and Docker Volume Workflow


1.The Data Flow

User type "Some Text" on browser

⬇️

Spring Boot catches it and sends SQL to Postgres Container (Docker)

⬇️

Postgres writes the data to the Docker Volume (Virtual Folder)

⬇️

Ubuntu (EC2) Server sees that volumes as a real folder on its hard drive (var/lib/docker/...)

⬇️

AWS stores that Ubuntu folder on the EBS Volume (Physical Disk)

Backup⬇️

Use EBS Snapshot (backup copy of entire hard drive including the Docker Volume)




2.The Deletion Scenarios

Scenario A: Delete the Container

docker rm postgres-db

Result: Data is SAFE

Why?: Data has been saved in Ubuntu EBS Volume which is physical disk

Scenario B: Delete the EC2 Instance

AWS Console -> Terminate

Result: Data is GONE FOREVER

Why?: Entire Ubuntu computer has been destroyed, including EBS volumes on Ubuntu.


💡Tip: How to save data from being destroyed due to EC2 Instance deletion

  1. AWS Console -> EC2 -> Volumes
  2. Right-click the volume -> Create Snapshot
  3. This makes a backup copy of the entire hard drive (including Docker Volume) and stores it in S3 (hidden in the background)
  4. Terminate EC2 instance
  5. Create a new Volume from that Snapshot and attach it in a new EC2 instance


💡Tip: Check current EC2 Volume Status

df -h

df: Disk Free

-h: Human readable (GB/MB)


💡Tip: You can buy an additional EBS Volume and attach it to the EC2 and tell Docker to put the SQL data volume in a new one.

  • Pros: Safety if the database fills up the drive, the OS keeps running fine. You can also unplug this data drive and move it to a bigger server easily. Prevent databases grows up and fills up the entire server. Which makes the OS crashes, the logs crash, everything dies!
  • Cons: COSTS MORE MONEY and need to mount manually to EC2 Instance on Ubuntu
← Back to Learning Journey