Two-tier-flask-app deployment with dockeršŸ ...!!

Ā·

2 min read

Two-tier-flask-app deployment with dockeršŸ ...!!

Firstly, we have to pull the mysql server. Steps to do this:

  1. Open your EC2 terminal.

  2. Run: docker pull mysql

Then, clone the application to your EC2 server.

  1. Open your EC2 terimal.

  2. 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

  1. 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

  2. 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:

  1. Run: docker exec -it <mysql-container-id> bash

  2. Run: mysql -u root -p

  3. Enter password: root

  4. 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.

  1. Make a folder in EC2 instance: mkdir volumes

  2. In vloumes mkdir mysql

  3. Run:pwd <copy path>

  4. Firstly, stop and remove the existing mysql: docker stop mysql && docker rm mysql

  5. 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

Ā