Part1: 2-tier-app-deployment

Dockerizing the application

·

4 min read

Hello everyone! This blog is on deploying a 2 tier application that includes flask-app as the back end and MySQL as the database. We shall be learning this through a series of blogs.

The first blog will be on Dockerizing the application which includes, creating a docker image through the docker file, pushing the image to the docker hub and then deploying it

Let's start:

Launch an EC2 instance

Lunch an EC2 instance on which we shall start workingI have created a t2.micro ubuntu image and created an instance

Install docker and docker-compose

  1. Install docker and docker-compose through a shell script docker-install.sh.The contents for docker-install.sh are provided in the code block below.

     sudo apt update
     sudo apt-get install docker.io -y
     sudo systemctl start docker
     sudo systemctl enable docker
     sudo apt-get install docker-compose -y
    
     echo "Docker and docker-compose installed"
    

We shall add docker to the current user group and then perform a reboot before getting started with docker

Writing Dockerfile

  1. Let us start writing the docker file that will create an image for our flask-app and mysql chart.

     # 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 -r requirements.txt
    
     # Copy the rest of the application code
     COPY . .
    
     # Specify the command to run your application
     CMD ["python", "app.py"]
    

Creating an image

  1. Once we write the docker file, we shall build it through docker-build command

We can check if the image was successfully created through docker images command

Creating the container:

  1. As the image is succesfully created, we shall now move on to creating the container through docker run command. This command binds the port no 5000 of the application with the port no 5000 of container and we shall use the "latest" tag to create this image.

Now, the container for flask app is successfully created. We shall open the port no 5000 for inbound traffic in the instances's security group and check if the app is running on it

Checking if the application is running

Application is running however it in not able to connect to the local server

Debugging the error:

  1. The reason for the error is that flask app needs a mysql db to work but we have not connected it to any db server.

Now, let us create a mysql container thorugh the myself:5.7 image that i publivly available to everyone through dockerhub . Here we are passing two variables:

  1. name: the name that we weish to give to ur data base ie mysql

  2. env: mysql needs environmentv vaiables to work hence we are passing them as well through this command

Creating the mysql container

  1. So. the command to create a mysql container will be as follows:

    docker run -d -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD="admin" mysql:5.7

  2. As in the fisrt line following of the run command, we can see that the image was pulled form dockerhub as it was not available locally and a container has been succesfully created

  3. Now, we have successfully created both flask app and mysql containers. All we have to do now is connect these two by creating a network using docker network create command

  4. We shall delete the old containers as they did not have network

  5. Now, let us create a new container for both flask-app and mysql using the same old image and attach it with the network and name them as mysql and flask-app. We shall also pass some environment variables according to the code.

  6. Let us get into mysql container and add a new table. The script to add new table is as follows:

     CREATE TABLE messages (
         ->     id INT AUTO_INCREMENT PRIMARY KEY,
         ->     message TEXT
         -> );
    

Checking if the application is running:

  1. Now let us try to access the application on instance's IP address : 35.86.79.146 and port no 5000

We can see that the app is successfully deployed

Pushing the image to docker hub:

  1. Now we shall push this flask-app image to docker hub. Login into docker hub through the docker login below command, enter your credentials

  2. Once, you are logged in, first tag the image the image that you wish to push with your docker username docker tag flask-app sowmyabm/flask-app:latest and then push the image with the command docker push sowmyabm/flask-app:latest

  3. Let us check for image in dockerhub

We can see that the image has been successfully pushed to dockerhub