🔗Configuring EBS Volume for Docker Compose Database Data Persistence on Ubuntu EC2 Instance
It configured (Mounting or Adding) an Elastic Block Store (EBS) volume for Docker Compose database data persistence on an Ubuntu Linux EC2 instance that ensures data durability and availability by providing a reliable storage solution independent of the EC2 instance’s lifecycle.
What do we want?
We have an EC2 instance in AWS, and in this EC2 instance installs docker and docker-compose. In the docker-compose, We run a full stack mern application & for the MongoDB database data persistence we use EBS volume. So, It’s mount or add in EC2 instance.
🛠️Pre Required
- AWS account with required permissions
- Containerize full-stack web application (MERN)
- Basic knowledge of AWS cloud
This article is a bit long. So, I hope you follow & go to the last step and learn how to Configuring EBS Volume for Docker Compose Database Data Persistence on Ubuntu EC2 Instance. Which I deviate this article into six Sections:
Section 1: EC2 Instance ready & install docker
Section 2: Create EBS volume on AWS
Section 3: Attach the EBS volume to the EC2
Section 4: Mount the EBS volume with a directory in the EC2
Section 5: Download & run the MERN app with docker-compose
Section 6: Test again in the new EC2 instance
So, late go, If you have any problem responding to this article & feel free to reach out to me.
📘Section 1: EC2 Instance ready & install docker
Step 1: Create an EC2 instance (Volume Mount Testing), If you don’t know how to create you can follow the instructions here.
Step 2: Connect to the EC2 instance SSH then install the docker & docker-compose
sudo apt update
sudo apt install docker.io docker-compose
Step 3: We check the docker & docker-compose version.
sudo docker -v
sudo docker-compose -v
If you face any issues install docker and docker-compose following docker official site https://docs.docker.com/engine/install/ubuntu/.
Our EC2 instance is ready with docker and docker-composer. Now create an EBS volume and mount with EC2 instance.
📘Section 2: Create EBS volume on AWS
Step 1: Go to the AWS Console then click to “Create volume”.
Step 2: We set the default value of the Volume feature but the main two features are configured:
- Size (GiB): 10
- Availability Zone: us-east-1d
Our EC2 instance running on the “us-east-1d” zone. If we create a volume other Availability Zone we can not mount the volume with an EC2 instance.
If you want to check your EC2 instance “Availability Zone” go to your EC2 instance then Networking scale down and see “Availability Zone”.
Step 3: Now click “Create volume” & Our volume is ready.
📘Section 3: Attach the EBS volume to the EC2
Step 1: Go to the AWS Console then you see your Volumes list.
Step 2: Now, Select the EBS volume you want to attach with the EC2 instance then Click “Actions” -> “Attach Volume”.
Step 3:
- Select the EC2 instance from the “Instance” you want to attach the Volume
- Then Select a Device name from the “Device name” For the Device name have rules (Recommended device names for Linux: /dev/sda1 for root volume. /dev/sd[f-p] for data volumes)
Step 4: Click “Attach volume”
Step 5: Now, check the volume which detected our EC2 instance
- Connect to the EC2 instance using SSH and check
sudo lsblk
Our disk EBS volume successfully connects with our Instance.
📘Section 4: Mount the EBS volume with a directory
Step 1: Create a directory on our EC2 home directory.
sudo mkdir mydata
Step 2: If the EBS volume is not formatted with a file system, you’ll need to create one(Only need when we want to mount the first time).
sudo mkfs.ext4 /dev/xvdf
Step 3: Mount the EBS volume “mydata” directory in the home directory on our EC2.
sudo mount /dev/xvdf ./mydata
📘Section 5: Download & run the MERN app with docker-compose
Step 1: We have a MERN app on GitHub so, Now clone it here.
git clone https://github.com/bjnandi/containerize-full-stack-app-MERN-with-docker-compose.git
Step 2: After cloning the application go to the .env file in the client folder.
# Change directory
cd containerize-full-stack-app-MERN-with-docker-compose/client
# Edit .env file
nano .env
Replace the REACT_APP_SERVER_URL with your ec2_public_ip.
REACT_APP_SERVER_URL = http://54.227.22.145:5000
Step 3: Similarly, Go to the .env file on the server folder.
# Change directory
cd ../server
# Edit .env file
nano .env
Replace the CLOUDINARY_CLOUD_NAME, CLOUDINARY_API_KEY & CLOUDINARY_API_SECRET values with your value.
CLOUDINARY_CLOUD_NAME=your-cloud-name
CLOUDINARY_API_KEY=your-api-key
CLOUDINARY_API_SECRET=your-api-secret
Here I use my CLOUDINARY_CLOUD_NAME, CLOUDINARY_API_KEY, CLOUDINARY_API_SECRET. You use your access credentials for more details here https://cloudinary.com/.
Now, in the docker-compose file, the MongoDB container has a “volumes” section in this section that defines MongoDB database data stored in the current path “mydata” directory in the host machine & the last volumes are defined.
Step 4: Go to containerize-full-stack-app-MERN-with-docker-compose folder then docker-compose build & run.
#Change directory
cd ..
# Run docker-compose
sudo docker-compose up -d
After running the docker-compose our MongoDB data is stored in the “mydata” folder.
#Change directory
cd ../mydata
#Show data
ls
Step 5: Check our app is running & insert data works properly.
http://ec2_public_ip/
At this stage, We have successfully configured EBS Volume for docker-compose.
On this docker-compose file, If we stop the container and remove the image from the ‘host machine ’ then again we create a container image and run that container, we see the old data because our old data (mydata) is ‘backup’ using volumes.
📘Section 6: Test again in the new EC2 instance
Now, We test our MongoDB database data stored in the EBS volume.
Step 1: We down the docker-compose.
#Change directory
cd ../containerize-full-stack-app-MERN-with-docker-compose
#Stop docker-compose
sudo docker-compose down
Step 2: Umount the Volume.
#Change directory
cd ..
#Umount volume
sudo umount mydata
Step 3: Detach Volume from EC2 instance.
Go to the AWS Console then you see your Volumes list.
Select the EBS volume you want to attach with the EC2 instance then Click “Actions” -> “Detach Volume”.
Step 4: Ready a new EC2 instance (Test) & install docker (Section 1: EC2 Instance ready & install docker).
- Creating an EC2 instance follows the previous EC2 (Volume Mount Testing) in which where Availability Zone is used.
- Select the same Availability Zone from the “Network settings” and then Subnet (If we create an EC2 instance in another Availability Zone we can not mount the volume with an EC2 instance).
Step 5: Attach the EBS volume to the new EC2 instance (Section 3: Attach the EBS volume to the EC2).
- In the “Step 3” instance list select the new instance (Test)
Step 6: Mount the EBS volume with a new EC2 instance (Section 4: Mount the EBS volume with a directory).
At the moment we won’t format the EBS volume. if we do this all will data remove from the EBS volume (Step 2).
After mounting the EBS volume we can check old data is shown on the current EC2 instance “mydata” directory.
#Change directory
cd mydata
#Show Data
ls
#Change directory
cd ..
Step 7: Clone the app and run it on a new EC2 instance (Test) (Section 5: Download & run the MERN app with docker-compose)
Step 8: Finally, We can see the same output as an old database.
If you want to understand better see also this article:
🌟Congratulations!!🌟, We have successfully configured EBS Volume for docker-compose database data persistence on Ubuntu EC2 Instance.
#DockerCompose #MERN #FullStack #WebApplication #Containerization #DevOps #ContainerizedApps #Docker #MERNStack #DockerizedWebApp #DockerDeployment #MERNApp #DockerContainers #DevOps
If you face any issues with EBS volume for docker-compose data persistence feel free to contact me. I will try my best. Thank you.
In Plain English 🚀
Thank you for being a part of the In Plain English community! Before you go:
- Be sure to clap and follow the writer ️👏️️
- Follow us: X | LinkedIn | YouTube | Discord | Newsletter
- Visit our other platforms: CoFeed | Differ
- More content at PlainEnglish.io