Installing jenkins, SonarQube and Docker on EC2 Instance
Firstly we need to create three EC2 Instances for Jenkins, SonarQube and Docker respectively.
Log in to your AWS account.
Now, Navigate to the EC2 instance and then click "Launch Instance".
Name: Jenkins
Number of Instances: 3
Application and OS image: Ubuntu
Instance type: t2.micro
Key pair: create a new one or use the existing one
Keep the rest of the things as default and click on "Launch Instance"
Now, we will ssh to our Jenkins Instance using the command:-
ssh -i {path of pem file} ubuntu@{Public IPv4 address}
version: 0.2
env:
parameter-store:
DOCKER_REGISTRY_USERNAME: /myapp/docker-credentials/username
DOCKER_REGISTRY_PASSWORD: /myapp/docker-credentials/password
DOCKER_REGISTRY_URL: /myapp/docker-registry/url
phases:
install:
runtime-versions:
python: 3.11
pre_build:
commands:
- echo "Installing dependencies..."
- pip install -r day-13/simple-python-app/requirements.txt
build:
commands:
- echo "Running tests..."
- cd day-13/simple-python-app/
- echo "Building Docker image..."
- echo "$DOCKER_REGISTRY_PASSWORD" | docker login -u "$DOCKER_REGISTRY_USERNAME" --password-stdin "$DOCKER_REGISTRY_URL"
- docker build -t "$DOCKER_REGISTRY_URL/$DOCKER_REGISTRY_USERNAME/simple-python-flask-app:latest" .
- docker push "$DOCKER_REGISTRY_URL/$DOCKER_REGISTRY_USERNAME/simple-python-flask-app:latest"
post_build:
commands:
- echo "Build completed successfully!"
artifacts:
files:
- '**/*'
base-directory: ../simple-python-app
Install Docker:
sudo apt update
sudo apt install docker.io -y
Start Docker daemon
You use the below command to verify if the docker daemon is actually started and Active
sudo systemctl status docker
sudo systemctl start docker
Grant Access to your user to run docker commands
To grant access to your user to run the docker command, you should add the user to the Docker Linux group. Docker group is create by default when docker is installed
sudo usermod -aG docker $USER
Docker is Installed, up and running 🥳🥳
Use the same command again, to verify that docker is up and running.
docker run hello-world
Output should look like
....
....
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
...
Now, we will clone the repository which contains our application, for frontend and backend we will use flask and MySql as our Database.
git clone https://github.com/Ketangrover14/two-tier-flask-app.git
Now, Navigate inside the repository and you will see the index.html file which contains our frontend and app.py which contains our backend and database.
We need to Dockerize the application and for that, we will create two containers for Flask and Database and both of these containers will talk to each other using Docker networking.
Now, we will write a Dockerfile from which we will create a Docker image for both the backend and the database.
# 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"]
Now, we will create docker image from our docker.
docker build -t flaskapp .
Now, make sure that you have created a network using the following command because using a network we can communicate with containers
docker network create twotier
Now, we will finally create the container one for Mysql and another for Flask.
i) MySQL container
docker run -d --name mysql --network=twotier -e MYSQL_DATABASE=mydb -e MYSQL_USER=root -e MYSQL_ROOT_PASSWORD="admin" -p 3306:3360 mysql:5.7
ii) Backend container
docker run -d --name flaskapp --network=twotier -e MYSQL_HOST=mysql -e MYSQL_USER=root -e MYSQL_PASSWORD=admin -e MYSQL_DB=mydb -p 5000:5000 flaskapp:latest
After creating the containers successfully we will create Mysql table which will be used to store data in our database.
Now, to go inside your container use the command:
docker exec -it {container id} bash
mysql -u root -p
Now to get inside your mysql use the command:
mysql -u root -p
now it will ask you for password that you have created which creating container(admin)
Use the command:
show databases;
#and then use mysql db
use mydb;
Create the messages
table in your MySQL database:
CREATE TABLE messages (
id INT AUTO_INCREMENT PRIMARY KEY,
message TEXT
);