Dockerize a Python-Flask Web Application
Introduction:
Python is a popular programming language used for developing web applications, and Flask is one of the popular Python frameworks for web development. Docker is a containerization technology that allows developers to package their applications and dependencies in a portable container that can be run on any platform. In this blog, we will discuss how to create a Dockerfile for a Python-Flask web application, and then we will use Docker Compose to run the application in a container.
Creating a Dockerfile for Python-Flask Web Application:
The Dockerfile is a script that contains a set of instructions for building a Docker image. Here is the Dockerfile for our Python-Flask web application:
#Installing base image
FROM python:3.10-slim
FROM ubuntu:20.04
#import a timezone for java installation
ENV TZ=Asia/Kolkata
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt update
RUN apt install -y tzdata
#choosing the working dir and copying files
WORKDIR /app
COPY . /app
#running port
EXPOSE 8000
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1
#installing packages
RUN apt-get clean && apt-get update && apt-get install -y apt-transport-https \
&& apt-get install -y curl sudo \
&& apt-get install -y build-essential zip unzip wget \
&& apt-get install -y python3 \
&& apt-get install -y python3-pip
#installing java
RUN apt-get install -y openjdk-11-jdk
ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk -amd64
ENV PATH $PATH:$JAVA_HOME/bin
# Install pip requirements
COPY requirements.txt .
RUN python3 -m pip install -r requirements.txt
#running the application
ENTRYPOINT ["python3", "api.py"]
Let’s go through the instructions step-by-step:
- FROM python:3.10-slim — This command sets the base image to the official Python 3.10 slim image.
- FROM ubuntu:20.04 — This command sets the base image to Ubuntu 20.04.
- ENV TZ=Asia/Kolkata — This command sets the timezone to Asia/Kolkata.
- RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone — This command sets the timezone in the container.
- RUN apt update — This command updates the package list in the container.
- RUN apt install -y tzdata — This command installs the timezone data in the container.
- WORKDIR /app — This command sets the working directory to /app.
- COPY . /app — This command copies the current directory to the /app directory in the container.
- EXPOSE 8000 — This command exposes port 8000 in the container.
- ENV PYTHONDONTWRITEBYTECODE=1 — This command prevents Python from generating .pyc files in the container.
- ENV PYTHONUNBUFFERED=1 — This command turns off buffering for easier container logging.
- RUN apt-get clean && apt-get update && apt-get install -y apt-transport-https \ && apt-get install -y curl sudo \ && apt-get install -y build-essential zip unzip wget \ && apt-get install -y python3 \ && apt-get install -y python3-pip — This command installs packages required for the application to run.
- RUN apt-get install -y openjdk
After creating the Dockerfile, we need to create a docker-compose.yml file to define the services that make up our application.
Here is the docker-compose.yml file:
version: "3.9"
services:
webapp:
build: .
ports:
- "5050:8000"
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
driver: flocker
driver_opts:
size: "10GiB"
Let’s go through the configuration step-by-step:
- version: “3.9” — This specifies the version of the docker-compose file format.
- services — This section defines the services that make up our application.
- webapp — This is the name of our service.
- build: . — This specifies the location of the Dockerfile that we created earlier.
- ports: — This specifies the ports that we want to expose on our container.
- “5050:8000” — This maps port 8000 inside the container to port 5050 on the host.
- volumes: — This specifies the volumes that we want to mount inside our container.
- db-data:/var/lib/mysql — This mounts the db-data volume inside the /var/lib/mysql directory in our container.
- volumes — This section defines the volumes that we want to create.
- db-data — This is the name of our volume.
- driver: flocker — This specifies the volume driver that we want to use.
- driver_opts: — This specifies the driver options.
- size: “10GiB” — This sets the size of our volume to 10 gigabytes.
Conclusion:
In this blog, we have created a Dockerfile for a Python-Flask web application and a docker-compose.yml file to define the services that make up our application. By using Docker Compose, we can easily run our application in a container and manage our application’s dependencies. Containerization allows developers to create portable applications that can be run on any platform, making it an essential tool for modern application development.