Introduction:
In today's tech landscape, deploying applications efficiently and reliably is essential for success. In this blog post, we'll explore the process of deploying a two-tier application using Flask, Docker, Docker Compose, Docker Hub, MySQL, GitHub, and AWS EC2. We'll cover each step in detail, from containerizing the application to deploying it on the cloud, and share insights into best practices along the way.
We will see how to troubleshoot while making the 2-Tier App.
✅Login to AWS Console.
✅Search EC2 & Click on Launch Instances.
✅Fill the form 1) Name of Instance 2) Select Ubuntu Image 3) Instance Type- t2.micro 4) Create New Key-Pair 5) Launch Instance.
✅Connect the Instance-> You'll get Ubuntu Terminal.
✅Update the System & Install Docker.
sudo apt-get update # Update the System
sudp apt-get install docker.io # Install Docker
✅Changes the Ownership of the Docker socket file to the current user.
sudo chown $USER /var/run/docker.sock
✅To Check the List of the Container.
docker ps # Check list of container
✅Then Go to GitHub, Clone the Repository.
git clone https://github.com/aryankale04/two-tier-flask-app.git # URL of your Repository
✅Change the Directory.
cd two-tier-flask-app/ # Your Clone Directory Name
✅Dockerfile will appear in it.
✅Now create network to add both container in 1 network.
docker network create twotier # Two tier is network name(you can change network nam
✅Then, once again to attach both containers in same network.
docker run -d -p 5000:5000 --network=twotier -e MYSQL_HOST=mysql -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin -e MYSQL_DB=myDb flaskapp:latest #to attach in same network
docker run -d -p 3306:3306 --network=twotier -e MYSQL_DATABASE=myDb -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin -e MYSQL_ROOT_PASSWORD=admin mysql:5.7 #to attach in same network
✅Check, whether both containers are in same network or not
docker network ls
✅To Inspect twotier network but, Still it shows container name different.
docker network inspect twotier
✅Now, go to your EC2 instance security group open port no. 5000 & save it.
✅Now, Build Dockerfile.
docker build . -t two-tier-app
✅Run the both container again
docker run -d -p 5000:5000 --network=twotier -e MYSQL_HOST=mysql -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin -e MYSQL_DB=myDb --name=flaskapp two-tier-app:latest # run again container
docker run -d -p 3306:3306 --network=twotier -e MYSQL_DATABASE=myDb -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin -e MYSQL_ROOT_PASSWORD=admin --name=mysql mysql:5.7 # run the container if not work then remove old mysql container and run again mysql container
✅Now, to go into Mysql database
docker exec -it <containerID> bash # your containerID will be different
✅Enter the Password to enter into the database
mysql -u root -p # when you press the enter then give password to them. You can enter mysql database
✅To create a new database in MySQL
CREATE DATABASE KYC;
✅To check all datases in MYSQL.
show databases;
✅To use particular database.
use KYC; # KYC is particular database name
✅Now in KYC database Create Table Messages & run it.
CREATE TABLE messages (id INT AUTO_INCREMENT PRIMARY KEY, message TEXT); #syntax of create a table in database
✅Now access your flaskapp using your EC2 public-ip:5000
& write something.
✅You can check your data will get save in table.
select * from messages; # to check all data of the table
✅To exit from database.
exit
✅If you want to save your flaskapp image in Dockerhub, login to dockerhub
docker login # Give username & password of your dockerhub
#Once login succeeded
✅Now tag your image to username of docker with imagename
docker tag two-tier-app:latest #adddockerhubusername/flaskapp:latest
✅You can see image with your username
docker images
✅Let's try to push image on dockerhub.
docker push #dockerhubusername/flaskapp:latest
Conclusion
Deploying a two-tier application involves several intricate steps, from containerization to cloud deployment. By leveraging technologies like Flask, Docker, Docker Compose, Docker Hub, MySQL, GitHub, and AWS EC2, you can streamline the deployment process and build robust, scalable applications. Through documentation and sharing experiences, we empower others to navigate similar challenges and drive innovation in the ever-evolving tech landscape.