Episode 2 | 2-Tier Application Deployment with Docker

Episode 2 | 2-Tier Application Deployment with Docker

Dockerizing the App:

Launch an Instance:

Update the instance:

Installation of Docker pkg:

Remove the permission error:

Clone the Git Repository:

git clone https://github.com/LondheShubham153/two-tier-flask-app.git

Build Docker image from DockerFile:

# 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 --no-cache-dir -r requirements.txt

# Copy the rest of the application code
COPY . .

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

Dockerize the Flask Application (Front-End):

Dockerize the MySQL Database (Back-End):

Access the app:

try to access the app x.x.x.x:5000

Expose the port 5000 & 3306 for frontend as well as Database


Create a Docker Network:

Both frontend & backend are running separately due to which they aren't communicating. Both of the container needs to be run in a same N/W for the communication b/w frontend & backend.

Remove the previously created containers :

the containers are running separately.

Create Docker Container of both frontend & backend in a Docker Network:

FrontEnd Container:

DatabaseEnd Container:

Inspect the Docker Network:

If u not define the network type then by default bridge type network created.

try to access the app x.x.x.x:5000

Not finding the messages script under myDb database.

Login to MySQL:

Enter the credentials for login.

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

Create a message table.

try to access the app x.x.x.x:5000

Submit some of the comments from frontend:

Check the submitted messages


Launch app through Docker-Compose:

Push the Docker image of App on Docker-Hub:

Installation of Docker-Compose :

Manifest file (docker-compose.yml) :

version: '3'
services:
    backend:
        image: 'schauham21286/flaskapp:latest'
    ports:
      - "5000:5000"
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
      MYSQL_DB: myDb
    depends_on:
      - 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:
  mysql-data:

Run the manifest file :

Access the app :

Checking the Database:

Thank you for reading! Happy Learning!!

Santosh Chauhan

Did you find this article valuable?

Support Santosh Chauhan's blog by becoming a sponsor. Any amount is appreciated!