Part2: 2-tier-app-deployment

·

3 min read

In today's blog, let us try to deploy the same flask app through docker-compose instead of Dockerfile.

Why are we doing this?

Well, as we had already seen in my last blog, there were n number of steps involved in deploying a 2-tier app through a docker file like creating containers for each of them separately, creating a network and then attaching a network and so on

What if I say, the same deployment can be done much more easily in less number of steps through Docker-compose?

Dive into the blog to understand more.

Let's start

First, create an EC2 Ubuntu instance on which we shall start working:

  1. First, make sure docker and docker-compose are installed, if not they can be installed through the below shell script. You can check this blog for more information

     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"
    
  2. Once the docker setup is complete, we shall start writing the Docker compose file:

    1. In the docker file, we will first add the version of docker-compose and the services for which we are creating the containers:

    2. Next, we shall add command that will perform the following tasks :

      1. pull the image from dockerhub

      2. build a container out of it

      3. includes port no's to be bound

      4. environment variables required for both flask app and mysql db

        version: '3'  # version of docker compose that is being used
        services: # services that need to be created
          backend:
            image: sowmyabm/flask-app:latest #image that should be pulled from dockerhub
            ports:
              - "5000:5000"
            environment:
              MYSQL_HOST: mysql
              MYSQL_USER: admin
              MYSQL_PASSWORD: admin
              MYSQL_DB: myDb
          mysql:
            image: mysql:5.7
            ports:
              - "3306:3006"
            environment:
              MYSQL_ROOT_PASSWORD: admin
              MYSQL_DATABASE: myDb
              MYSQL_USER: admin
              MYSQL_PASSWORD: admin
  1. Now, as we had already seen in the previous blog, mysql db needs table to be created.

  2. So we shall write a sql script(message.sql) to create a table and add this script to docker's entrypoint so that this srcipt runs simultaneously when the docker conatiner is created.

     CREATE TABLE messages (
         id INT AUTO_INCREMENT PRIMARY KEY,
         message TEXT
     );
    
  3. Now we shall write a command that binds the mysql script in the current folder to docker's entry point (./message.mysql:/docker-entrypoint-initdb/message.sql))

  4. Next, we shall create a volume and attach it to mysql(mysql-data:/var/lib/mysql)

       volumes:
         - ./message.sql:/docker-entrypint-initdb.d/message.sql
         - mysql-data:/var/lib/mysql
     volumes:
       mysql-data:
    
  5. So the contents of docker-compose file will be as follows:

    
     version: '3'  # version of docker compose that is being used
     services: # services that need to be created
       backend:
         image: sowmyabm/flask-app:latest #image that should be pulled from dockerhub
         ports:
           - "5000:5000" #ports to be bound
         environment:    # environment variables required
           MYSQL_HOST: mysql
           MYSQL_USER: admin
           MYSQL_PASSWORD: admin
           MYSQL_DB: myDb
       mysql:
         image: mysql:5.7 #image to be pulled from dockerhub
         ports:
           - "3306:3006" # ports to be bound
         environment:    # environment variables required
           MYSQL_ROOT_PASSWORD: admin
           MYSQL_DATABASE: myDb
           MYSQL_USER: admin
           MYSQL_PASSWORD: admin
    
         volumes: 
           - ./message.sql:/docker-entrypint-initdb.d/message.sql #attaching mysql script tp dockers entrypoint
           - mysql-data:/var/lib/mysql #mounting the container;s volume with that of laptop
    
     volumes:
       mysql-data:
    
  6. Set the inbound rules to open port no 5000 for the incoming traffic

  7. We have setup everything that is necessary for the application, now we shall deploy the whole application through a single command docker-compose up -d

  8. The application is successfully containerised

  9. We can check if teh app is successfully deplpoyed by accessing it via the web browser on our server's IP

Hence successfully deployed application through docker-compose.

Visit my next blog to understand how to make this application fault tolerant