Firstly, we have to pull the mysql server. Steps to do this:
Open your EC2 terminal.
Run:
docker pull mysql
Then, clone the application to your EC2 server.
Open your EC2 terimal.
Run:
git clone
https://github.com/prem14choudhary/two-tier-flask-app.git
Now, we have to build the docker image. for this run: docker build -t two-tier-backend .
By ādocker images
ā command you can see docker images.
Create a docker network by this command: docker network create two-tier -d bridge
Now we are going to give network to our 2 containers; flask app and mysql. Steps
Run for mysql server in two-tier nework:
docker run -d --name mysql --network two-tier -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=devops mysql
Run for flask app in two-tier nework:
docker run -d -p 5000:5000 --network two-tier -e MYSQL_HOST=mysql -e MYSQL_USER=root -e MYSQL_PASSWORD=roo t -e MYSQL_DB=devops two-tier-backend:latest
Now edit your inbound rules and allow port number 5000.
Enter any thing in the message box.
Now, we will see this messages in our mysql database.
Steps for this:
Run:
docker exec -it <mysql-container-id> bash
Run:
mysql -u root -p
Enter password:
root
mysql code: 1.
show databases;
2.use devops;
3.select * from messages;
Your message database will be there.
If we delete the mysql image the meassges we have given then all will be lost.
For solution of this we have to make a volume so that the data will be stored in that volume.
Make a folder in EC2 instance: mkdir volumes
In vloumes mkdir mysql
Run:
pwd
<copy path>Firstly, stop and remove the existing mysql:
docker stop mysql && docker rm mysql
Run:
docker run -d --name mysql --network two-tier -v <copy path>:/var/lib/mysql/ -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=devops mysql
Now, even after deleting mysql your data is safe.
Hereās we completed our two-tier-flask-app with dockerš ā¦!!!
Hari Om tatsat