in this blog we are going to deploy our flask app on aws ec2 instance using Github,Docker,DockerHub, Docker-compose and aws ec2 instance.
Prerequisites :-
Here are some prerequisites for this project are given below:
AWS EC2
Git & GitHub
Docker
DockerHub
Follow the steps:
First of all, go to the AWS portal, and create a new instance.
Now, connect to the EC2 instance that you have created. Copy the SSH from server:
Go to the download folder, where the .pem file is placed and open the terminal in the same location, and paste the SSH.
Run the following command for the Package update and Docker Installation.
sudo apt-get update -y sudo apt install docker.io -y
Troubleshoot the docker permission denied problem.
Clone the Repository from the git hub, copy the SSH and paste it into the terminal here is the command.
git clone https://github.com/Dilip31/two-tier-flask-app
Write a Dockerfile for the Python installation and Mysql Client.
# Use an official Python runtime as the base image FROM python:3.9-slim # Set the working directory in the container WORKDIR /app # install required packages for system RUN apt-get update \ && apt-get upgrade -y \ && apt-get install -y gcc default-libmysqlclient-dev pkg-config \ && rm -rf /var/lib/apt/lists/* # Copy the requirements file into the container COPY requirements.txt . # Install app dependencies RUN pip install mysqlclient RUN pip install --no-cache-dir -r requirements.txt # Copy the rest of the application code COPY . . # Specify the command to run your application CMD ["python", "app.py"]
After Writing the Docker file Now build it with the following commands it includes Steps which I have Mentioned below.
docker build -t flaskapp-2tier . docker images
Now Run the docker Container with the help of Docker images with Port Number with the help of the Below command.
docker run -d -p 5000:5000 flaskapp-2tier:latest docker ps
Now open the Port in our Security Groups for Incoming Traffic and allow port no 5000.
-
Now Access the app with the help of the Public IP of Ec2 Instance with port no
here is the Syntax:- http://<Your IP address>:5000
We need to create a MySQL Container.
docker run -d -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD="myadmin" mysql:5.7 docker ps
Now Access the app with the help of the Public IP of Ec2 Instance with port no
here is the Syntax:- http://<Your IP address>:5000
Both containers are not communicating with each other because both containers are not in the same network. To solve this problem we have to create a custom bridge network.
We need to create a docker network 2tier to connect MySQL Container and MySQL Database.
docker network create twotier docker network ls
Stop previous running containers, before creating new containers.
docker kill <container-id-1> <container-id-2> docker rm <container-id-1> <container-id-2>
As per the Diagram need a MySQL Container with username and Password to use these commands to run I have to use the Environment Variable in these with -e.
sudo 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 flaskapp-2tier:latest
As per the Diagram need a MySQL Database username and Password use these commands to run I have used the Environment Variable in these with -e and run the mysql image latest.
sudo 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 docker ps
Check the Network of Both Containers and Inspect both containers on the Network with the help of the below command.
docker network ls docker network inspect twotier
Now your MySql Container and MySql Database are connected.
http://<Your IP address>:5000
Create the Message table in your MySQL Database but before creating a table access the MySql Database and then enter in the mysql with the following commands.
sudo docker exec -it <container-id> bash mysql -u root -p enter password
Check the Databases, use myDb and then create a table with the help of the following commands. use the syntax directly copy and paste to your terminal.
show databases; use myDb; CREATE TABLE messages ( id INT AUTO_INCREMENT PRIMARY KEY, message TEXT );
After Creating the table access your 2-Tier Flask Deployment App on your browser: with the help of the following syntax:
http://<ec2-instance-public-ip>:5000
Add some messages and submit them for testing purposes.
Verify that data in the database.
select * from messages;
Push Your Docker Image To DockerHub
docker login Username: <dockerhub_username> Password: <dockerhub_password>
Again tag your docker image with your dockerhub username.
docker images //docker tag <local-imagename> dilip31/<name by which yoy want to push> docker tag flaskapp-2tier:latest dilip31/2-tier-flask-app:latest docker images
Push your tagged docker image to your docker hub account.
docker push dilip31/2-tier-flask-app:latest
We can use Docker-compose to reduce our commands by simultaneously spinn two containers
install docker copose
sudo apt install docker-compose
Write a Docker-compose file
vim docker-compose.yml
version: '3' services: backend: build: context: . ports: - "5000:5000" environment: MYSQL_HOST: mysql MYSQL_USER: admin MYSQL_PASSWORD: admin MYSQL_DB: myDb depends_on: - mysql mysql: image: mysql:5.7 ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: myDb MYSQL_USER: admin MYSQL_PASSWORD: admin volumes: - ./message.sql:/docker-entrypoint-initdb.d/message.sql # Mount sql script into container's /docker-entrypoint-initdb.d directory to get table automatically created - mysql-data:/var/lib/mysql # Mount the volume for MySQL data storage volumes: mysql-data:
up docker-compose file
docker-compose up -d
access your 2-Tier Flask Deployment App on your browser: with the help of the following syntax:
http://<ec2-instance-public-ip>:5000
Video reference
https://www.youtube.com/playlist?list=PLlfy9GnSVerRpz3u8casjjv1eNJr9tlR9
( Dont forgot to Delete the instances.)
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Hope you found this helpful. Do connect/ follow for more such content.
~Dilip khunti