In this project we will do containerization of a two-tier application using Docker, Docker Compose and image scanning with Docker Scout. We will make a Flask app and MySQL database.
Todo App code (we will use code from github repository) click here for code
sudo apt update
sudo apt install docker.io
-
Now docker is install but if we try command docker ps to check running containers then it will show error because current user is not getting to connect docker. So solve this error add your current user to docker group then our user will get all permission of docker, for this use command as follow :-
sudo usermod -aG docker $USER
sudo reboot
git clone https://github.com/sudhajobs0107/2-Tier-Flask-App-and-MYSQL
cd 2-Tier-Flask-App-and-MYSQL
vim Dockerfile
docker build . -t flaskapp(image_name)
docker images
docker login
docker tag flaskapp sudhajobs0107/flaskapp:latest
docker push sudhajobs0107(dockerhub_username)/flaskapp:latest
Now we have 2 options. One option is we will make two conatiners and one network & then attach that network to containers and Second option we will make containers by docker-compose file and network it will create automatically.(Disclaimer anyone can choose any option.)
docker run -d -p 5000:5000 --name flaskapp flaskapp:latest
docker run -d -p 3306:3306 --name mysql mysql:5.7
docker ps
docker network create twotier
docker network ls
docker run -d -p 5000:5000 --network twotier --name flaskapp -e MYSQL_USER=root -e MYSQL_HOST=mysql -e MYSQL_DB=KYC -e MYSQL_PASSWORD=test@123 flaskapp:latest
docker run -d --network=twotier --name mysql -e MYSQL_PASSWORD=test@123 -e MYSQL_DATABASE=KYC -e MYSQL_ROOT_PASSWORD=test@123 mysql:5.7
docker network inspect twotier
docker exec -it container_id bash
-
Now we are inside the container. Now if we do "ls" and it will show all directories.Now type "mysql -u root -p" enter your "password". User always be root and password which you put while making container. And we will be inside the MYSQL. If we type "show databases;" this will show there is a KYC database that we created while making the container. Now to solve our application error we will use our database. So type "use KYC" and our database will change. Now copy code from message.sql file and paste it in bash.
sudo apt install docker-compose
vim docker-compose
docker-compose up -d
mkdir ~/.docker/cli-plugins
cd /home/ubuntu/.docker/cli-plugins
curl -sSfL https://raw.githubusercontent.com/docker/scout-cli/main/install.sh | sh -s --
docker scout cves sudhajobs0107/Flaskapp:latest
git add Dockerfile
git commit -m "Initial commit"
git push -u origin main
In this we will do monitoring using Grafana and Prometheus of our 2-Tier-Flask-App-and-MYSQL Project that we did earlier. If you're interested in exploring my 2-Tier Flask App with MySQL project, to check out my project : Click here.
-
Prometheus we use for metrics & alerting. Now make sure docker.io & docker-compose is installed. Now to configure Prometheus & cAdvisor use command as given below [cAdvisor (short for container Advisor) analyzes and exposes resource usage and performance data from running containers. cAdvisor exposes Prometheus metrics out of the box] :-
sudo apt-get update
wget https://raw.githubusercontent.com/prometheus/prometheus/main/documentation/examples/prometheus.yml
ls
- job_name: "docker"
static_configs:
- targets: ["cAdvisor:8080"]
sudo apt-get update
sudo apt-get install -y apt-transport-https
sudo apt-get install -y software-properties-common wget
sudo wget -q -O /usr/share/keyrings/grafana.key https://apt.grafana.com/gpg.key
echo "deb [signed-by=/usr/share/keyrings/grafana.key] https://apt.grafana.com stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
sudo apt-get update
sudo apt-get install grafana
sudo systemctl enable grafana-server
sudo systemctl start grafana-server
sudo systemctl status grafana-server
node-exporter:
image: prom/node-exporter:latest
container_name: node-exporter
restart: unless-stopped
volumes:
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /:/rootfs:ro
command:
- '--path.procfs=/host/proc'
- '--path.rootfs=/rootfs'
- '--path.sysfs=/host/sys'
- '--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)'
expose:
- 9100
vim prometheus.yml
- job_name: "NodeExporter"
static_configs:
- targets: ["node-exporter:9100"]