2-Tier Application Deployment with Docker

GitHub URL - https://github.com/Hemantjangir53/two-tier-flask-app.git

sudo apt install docker.io
sudo chown $USER /var/run/docker.so

Dockerfile

# Getting an official Python runtime as the base image from dockerHUb
FROM python:3.9-slim

# Set the working directory in the container where all code will be kept
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(system -> your container)
COPY requirements.txt .

# Install app dependencies and required library or to make a container 
RUN pip install mysqlclient
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application code (system -> your container)
COPY . .

# Specify the command to run your application or to run a container 
CMD ["python", "app.py"]

Now build a docker file

docker build . -t flaskapp

run flaskapp container

docker run -d -p 5000:5000 flaskapp:latest

run mysql container

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

Create docker network

docker network — it connect the 2 or more containers among themselves so that they can communicate to each other. like here backend and database connect with the help of docker network.

docker network create <twotier(network_name)>

run Flaskapp container inside the docker-network

docker run -d -p 5000:5000 --network=twotier -e MYSQL_HOST=mysql -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin -e MYSQL_DB=myDB --name=flaskapp flaskapp:latest

docker run -d -p 5000:5000 --network=twotier -e MYSQL_HOST=mysql -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin -e MYSQL_DB=myDB --name=flaskapp flaskapp:latest

docker run -d -p 3306:3306 --network=twotier -e MYSQL_DATABASE=myDB -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin -e MYSQL_ROOT_PASSWORD=admin --name=mysql mysql:5.7

docker run -d -p 3306:3306 --network=twotier -e MYSQL_DATABASE=myDB -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin -e MYSQL_ROOT_PASSWORD=admin --name=mysql mysql:5.7

Check running docker-container inside the docker-network

docker network ls 

docker network inspect twotier

Image push to dockerHub

You need to tag your docker image

docker image tag <image_name>:<tag> <DockerHub_username>/<image_name>:<tag>

Now Image is pushed to dockerHub

docker push <DockerHub_username>/<image_name>:<tag>

Example:

docker image tag two-tier-app:latest hemantjangir/two-tier-app:latest

docker push hemantjangir/two-tier-app:latest

create docker-compose file

docker-compose.yml

version: '3'
services:  
  backend:
    build:
      context: .
    ports:
      - "5000:5000"
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
      MYSQL_DB: myDb
    depends_on:       # when docker-compose run, first mysql container create then flask app. without this both database not connect
      - mysql

  mysql:
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: myDb
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
    volumes:
      - ./message.sql:/docker-entrypoint-initdb.d/message.sql  # Mount sql script into container's /docker-entrypoint-initdb.d directory to get table automatically created
      - mysql-data:/var/lib/mysql  # Mount the volume for MySQL data storage

volumes:          # volume is the entity that allow you store data from container
  mysql-data:    # here the mysql-data create and above side assign (- mysql-data: )

Using docker-compose automatically docker network create and both MySQL and Flask container connected.

dcoker-compose up -d

So, we have successfully make and run a 2-tier application and connect backend and database.