🚀Deploying a Two-Tier Flask App Using Docker on Linux

·

2 min read

🚀Deploying a Two-Tier Flask App Using Docker on Linux

As technology advances, the world of containerization has become fundamental to modern software development. In this blog post, we'll explore deploying a two-tier Flask app utilizing Docker on a Linux system. This deployment will involve creating a Docker network, a volume, and a Docker file to encapsulate the application.

Prerequisites:

  1. Basic understanding of Docker concepts.

  2. Python and Flask are installed on the local system.

  3. Docker is installed on a Linux operating system.

  4. MySQL server installed locally or accessible externally.

Step 1: Setting Up the Flask Application:

Clone the repository from the GITHub.

Screenshot clone.png

Sep 2: Creating a Dockerfile

Develop a Dockerfile specifying the app's environment, dependencies, and commands needed to run the Flask app

# 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

RUN pip install --no-cache-dir -r requirements.txt

# Specify the command to run your application

CMD ["python", "app.py"]

Step 3: Implementing Docker Volume and Network

Set up a Docker volume to persist MySQL data, ensuring data persistence across container restarts.

mkdir volume

cd flask-app

cd volume

docker volume create --name [volume name] --opt type=none --opt device=home/ubuntu/flask-app/volume --opt o=bind
#Now attach volume

docker run -d -p <host_port>:<container_port> --mount source=[volume name], target=/data imagename:tag

#List all networks 

docker network ls

#Inspect a network 

 docker network inspect <network_name>

#Create a new network

 docker network create --driver <driver_type> <network_name>

#Connect containers to a network 

 docker network connect <network_name> <container_name>

#Disconnect containers from a network 

 docker network disconnect <network_name> <container_name>

#Remove a network 

 docker network rm <network_name>

s3.png

s7.png

Step 4: Configuring the MySQL Database

Set up a MySQL database and create a table to store messages submitted by users

docker exec -it [container ID] bash

bash-4.4# mysql -u root -p

enter password

mysql>use testdb;

show tables;

Step 5: Building and Deploying Docker Containers

Build the Docker images for Flask and MySQL using Docker Compose. Deploy the Docker containers, establishing connectivity between the Flask app and the MySQL database.

Screenshot (213).png

Step 6: Testing the Application Access

The deployed Flask app, via the browser, submits messages and verifies if they are stored in the MySQL database and displayed on the frontend.

s8.png

#Happy learning

Â