- Published on
Dockerizing Flask Application: A Step-by-Step Guide
- Authors
- Name
- Python Roadmap
- @PythonRoadmap
Table of contents
- Introduction
- What is Docker, and Why is it Needed?
- Dockerizing your Flask Application
- Step 1: Create a Flask Application
- Step 2: Create a Dockerfile
- Step 3: Build the Docker Image
- Step 4: Run the Docker Container
- Conclusion
- Related Links
Introduction
You have probably heard about Docker a lot already. Docker is an open-source "containerization" platform that allows you to package and deploy applications in a portable and scalable way. Dockerizing your Flask application has several benefits, including improved consistency, faster deployment, and simplified dependencies management.
In this blog post, we will give you a little idea on what Docker is, why it is needed and then we'll explain a step-by-step guide on how to Dockerize your Flask application.
What is Docker, and Why is it Needed?
Docker is a runtime that allows you to run containers. A container is a lightweight, standalone executable package that contains everything the application needs to run, including the code, runtime, libraries, and system tools. The container provides an isolated environment that guarantees consistency and reproducibility, making it easier to deploy and manage applications across different environments.
Docker containers are also scalable and portable, which means you can deploy them on any platform or infrastructure, whether it's your local machine, a cloud provider, or a hybrid environment. Containers can also be versioned, which means you can track and roll back changes easily, making it easier to maintain and update your application.
Dockerizing your Flask Application
Before we start, make sure you have Docker installed on your system. You can download and install Docker :
- using our beginner-friendly guide on How to Install docker on Ubuntu/Debian
- from the official website https://www.docker.com/.
Step 1: Create a Flask Application
For the purpose of this tutorial, we will create a simple Flask application. If you already have an existing Flask application, you can skip this step.
To create a new Flask application, follow these steps:
Open a terminal and create a new directory for your application.
mkdir myflaskapp cd myflaskapp
Create a new virtual environment for your application.
python3 -m venv venv source venv/bin/activate
Install Flask and create a new file
app.py
with the following code:from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run(host="0.0.0.0")
Run the application using the following command:
flask run
Open a web browser and navigate to http://localhost:5000/ to see the "Hello World!" message.
Step 2: Create a Dockerfile
A Dockerfile is a script that contains instructions on how to build a Docker image. To create a Dockerfile for our Flask application, follow these steps:
- Create a new file named
Dockerfile
in the root directory of your Flask application. - Add the following content to the
Dockerfile
:
FROM python:3.9-slim-buster
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]
Let's go through each line of the Dockerfile:
FROM python:3.9-slim-buster
specifies the base image for our container. We will use Python 3.9 with a slim version of Debian Buster as the base image.WORKDIR /app
sets the working directory to/app
.COPY requirements.txt requirements.txt
copies therequirements.txt
file from the current directory to the/app
directory in the container.RUN pip install --no-cache-dir -r requirements.txt
installs the Python dependencies listed in therequirements.txt
file.COPY . .
copies the entire content of the current directory to the/app
directory in the container.EXPOSE 5000
exposes port 5000, which is the port that the Flask application is running on.CMD ["python", "app.py"]
specifies the command to run when the container starts. In this case, we are running theapp.py
file using Python.
Step 3: Build the Docker Image
To build the Docker image, run the following command in the same directory as the Dockerfile
:
docker build -t myflaskapp .
This command builds a new Docker image with the tag myflaskapp
. The .
at the end of the command specifies that the build context is the current directory.
Step 4: Run the Docker Container
To run the Docker container, run the following command:
docker run -p 5000:5000 myflaskapp
This command starts a new Docker container using the myflaskapp
image we built earlier. The -p
flag maps port 5000 from the container to port 5000 on the host machine.
Open a web browser and navigate to http://localhost:5000/ to see the "Hello World!" message.
Conclusion
And there you go! Now you've learnt how to create a docker image for your flask application and run it. Dockerizing your Flask application can provide several benefits, including improved consistency, faster deployment, and simplified dependencies management.
In this tutorial, we provided a step-by-step guide on how to Dockerize your Flask application. We hope you found this tutorial helpful, and if you have any questions or comments, feel free to leave them below.