Flask App with MySQL Docker Setup
This is a simple Flask app that interacts with a MySQL database. The app allows users to submit messages, which are then stored in the database and displayed on the frontend.
Steps to follow up:
Step 1: Start E2 instance in AWS
Firstly i started E2 instance in AWS with the name Two-tier Application.
Step2: Updating, Upgrading and installing docker.io and docker-compose.v2
After i interlink ec2 instance i update and upgrade the machine using command sudo apt-get update && sudo apt-get upgrade and then i install docker.io
Step3: Add current user into docker group
The current user need to be in docker group and only we are able to use docker commands and execute it.
Step 4: Create new directory two-tier-appication
Step 5: Create new network name two-tier to bridge 2 applications
Step6: Download Mysql from server
I didn’t had mysql images therefore it downloaded from server and link the same created network providing root as a password and devops as a database
Now you can see the mysql image has been downloaded from the server And with command docker ps we can see its running in 3306 port.
Step 7: Get Flask-app from github by cloning
Now you can see that call the required documents are cloned
Step 8: Create a docker file
On the Dockerfile python:3.9-slim base image is used and working on /app directory. It will update and upgrade the system and install gcc default-limmysqlclient to run the mysql and connect into it. By using the docker file of flask-app, i created the image
Docker-compose.yml file created for mysql and flask-app services if you want to automate the code to build and run the images.
docker build -t flaskapp . command will help you to build the image from dockerfile with the tagged name flaskapp of same location dockerfile.
As you can see in the image that flaskapp and python image has been created.
Step 9 : Run and Connect flask-app with network two-tier, port 5001:5001 and with mysql.
Before you connect you need to see the mysql environment. The environment looks like
As you can see in the image that flaskapp is working on port 5000
Everything is working fine in the network
Lets inspect the network either two application are working on same network or not with the command:
docker network inspect two-tier
You can see that port Name: mysql and vibrant_borg(flask app) both container are on same network.
Step 10: Open port on AWS
Now you need to open the port 3306 and 5000 to execute on the browser
Step 11: Check on brower using ip and port number
As you can see that flask app is working perfectly
In addition lets push Docker Images into Docker Hub
To push the docker image into docker hub you need to use dockerhub username / image name repository. Eg: kubebibek/two-tier-flask, Therefore, Docker image tag flaskapp:latest kubebibek/two-tier-flask-app:latest helps to change flaskapp into kubebibek/two-tier-flask
To push the docker image repository you need to authenticate your username and password. As given in the picture.
My login was not authenticate therefore i need to create PAT(Personal Access Token). Icreate personal access token from docker hub as you can see in the picture and applied on my local terminal.
Now login succeeded.
To push the local docker image to docker hub use command: Docker push kubebibek/two-tier-flask-app
As you can see in the docker hub new repository has been created.
Check in the database either its persistent or not.
TO BE CONTINUED