Deployment of Login Page with DB
using Docker Compose

Enthusiastic about DevOps tools like Docker, Kubernetes, Maven, Nagios, Chef, and Ansible and currently learning and gaining experience by doing some hands-on projects on these tools. Also, started learning about AWS and GCP (Cloud Computing Platforms).
This is a basic login page which is configured with a MySQL database. So, the data of username and password is being stored in the database tables which you can further check also.
Part 1: Run in Local Server or EC2

Install MySQL on the EC2 Instance
sudo apt update
sudo apt install mysql-server -y
Configure MySQL
- Start the MySQL service
sudo systemctl start mysql
sudo systemctl enable mysql
- Set the MySQL root password:
sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;
- Create the database, user and table:
CREATE DATABASE mydb;
USE mydb;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL
);
CREATE USER 'root'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON mydb.* TO 'root'@'%';
FLUSH PRIVILEGES;
Allow Remote Connections to MySQL
- Edit the MySQL configuration file:
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
- Change it to:
bind-address = 0.0.0.0
- Restart MySQL:
sudo systemctl restart mysql
- Set the
public-ipin app.py file
db = mysql.connector.connect(
host="3.27.149.191", # Replace with your EC2 instance's public IP
user="root",
password="password",
database="mydb"
Run Your Flask App
- Install python3
sudo apt install python3 -y
sudo apt install python3-pip -y
sudo apt install python3.12-venv -y
- Create a virtual environment
python3 -m venv myvirtual
- To access the virtual environment
source myvirtual/bin/activate
- Install the required Python packages:
pip install -r requirements.txt
- Start the Flask application:
python3 app.py
- To run the application in detached mode
nohup python3 app.py &
Part 2: Dockerize

Step 1: Install & Create a Docker Network
- Install Docker
sudo apt install docker.io -y
sudo chmod 777 /var/run/docker.sock
- First, create a custom Docker network to allow communication between the MySQL and Flask containers.
docker network create git-network
Step 2: Create and run the MySQL Container
docker run -d \
--name mysql_container \
--network git-network \
-e MYSQL_ROOT_PASSWORD=password \
-e MYSQL_DATABASE=mydb \
-p 3306:3306 \
mysql:8.0

Verify MySQL Setup
- Check if the MySQL container is running:
docker ps -a
- Enter the MySQL container to check if the database was created:
docker exec -it mysql_container mysql -u root -p
- Use
passwordwhen prompted and run:
SHOW DATABASES;
USE mydb;
Step 3: Update Flask App's app.py
vi app.py
db = mysql.connector.connect(
host="mysql_container", # MySQL container's name
user="root",
password="password",
database="mydb"
)
- Create Table is not present
CREATE DATABASE mydb;
USE mydb;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL
);
CREATE USER 'root'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON mydb.* TO 'root'@'%';
FLUSH PRIVILEGES;
Step 4: Create Dockerfile for Flask App
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python3","app.py"]
Step 5: Build and Run the Flask App Container
- Build the Flask app image:
docker build -t flask-app .
- Run the Flask app container connected to the same Docker network:
docker run -d \
--name flask_app_container \
--network git-network \
-p 5000:5000 \
flask-app
Step 6: Test the Application
http://<your-ec2-public-ip>:5000
Step 7: Check database if details are saved or not
docker exec -it mysql_container mysql -u root -p
use mydb;
show tables;
select * from users;

Part 3: Run Using Docker-Compose File

- Install Docker Compose
sudo apt install docker-compose -y
- Run the Application
docker-compose up -d
- Down the Container
docker-compose down
Part 4: Automate through Jenkins Pipeline
- Install Trivy for Image Scan
sudo apt-get install wget apt-transport-https gnupg lsb-release
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add -
echo deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main | sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update
sudo apt-get install trivy
- Install Jenkins
sudo apt install default-jre -y
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins
- Check the status of the Jenkins Service
sudo systemctl status jenkins
- Create a job
flaskinpipelinecategory with following code
pipeline {
agent any
stages {
stage('checkout') {
steps {
git branch: 'main', url: 'https://github.com/Fir3eye/Login-Page-DB.git'
}
}
stage('Trivy') {
steps {
script{
sh "trivy fs . >> trivy.txt"
}
}
}
stage('deploy') {
steps {
script{
sh "docker compose up -d"
}
}
}
}
}

Then Build the job


SUCCESS





