Docker

Python Flask in Docker

In this tutorial, we are going to learn how to containerize a Python Flask application using Docker.

Containerization is an epic feature that allows us the developers to package an application along with the required dependencies into a single unit. We can then move the container and deploy the application across environments without needing to worry about compatibility.

What Is Flask?

Flask is a minimalistic and lightweight micro web framework for Python. It provides the essential feature and libraries that are required to build the lightweight web application using the Python language.

Flask follows the Web Server Gateway Interface or WSGI standard which allows us to incorporate a minimalistic design with a flexible patter which can handle the HTTP request and other features. For example, Flask supports the web features such as routing, databases, and more.

Prerequisites:

To follow along with the code and commands that are provided in this tutorial, ensure that you have the following tools:

  1. Installed Python interpreter on your machine (version 3.11 and above is recommended)
  2. Installed Docker engine on the host machine (version 23 and above is recommended)
  3. A text editor or IDE of your choice

With the given requirements met, we can proceed with this tutorial.

Create the Flask Application

As you can guess, the first step is creating the Python application that we wish to containerize. For our case, we demonstrate it with a minimalistic app that prints “hello world”.

Create a new directory to store the source code for your project and create a Python file named “app.py”.

Edit the “app.py” with your favorite text editor or IDE and add the code for your application as follows:

from flask import Flask

app = Flask(__name__)
@app.route("/")
def hello_world():
  return "<p>Hello, World!</p>"

Create a Dockerfile

Next, we need to define the instructions that allow us to package the application as a Docker image. We use the Dockerfile which contains all the instructions and tools to setup the image.

In the same directory as “hello.py”, create a new file called “Dockerfile” without extension. Run the following command:

$ touch Dockerfile

Edit the file and add the code as follows:

Use the official Python image as the base image.

FROM python:3.12-rc-slim
WORKDIR /app
COPY . /app
# Install Flask
RUN pip install --no-cache-dir -r requirements.txt
# Expose port 5000 for the Flask app
EXPOSE 5000
# run the Flask application
CMD ["python", "app.py"]

The previous Dockerfile performs the following operations:

  • Uses the official Python 3.12 slim image as the base image
  • Sets the working directory inside the container to “/app”
  • Copies the contents of the project directory into the container
  • Installs Flask and any other dependencies by running “pip install” using the “requirements.txt” file
  • Exposes port 5000 for the Flask application
  • Defines the command to run the Flask application

Ensure that the “requirements.txt” file exists in the project directory and add the contents of the file as shown in the following:

Flask==2.3.3

In this case, we specify that we wish to install the Flask version 2.3.3.

Build the Docker Image

Now that we have the Flask application and Dockerfile ready, we can proceed and build the image with the following command:

docker build -t flask-docker-app .

Ensure that you are in the project directory before running the previous command. You can replace the flask-docker-app with the name that you wish to assign to your image.

Run the Docker Container

With the Docker image built, we can run the container based on the image using the “docker run” command as follows:

docker run -p 5000:5000 flask-docker-app

This should create a container and map the port 5000 from the container to port 5000 on the host system.

Once executed, open the browser and navigate to http://localhost:5000.

You should see the “Hello, World!” message from the Flask application.

Conclusion

In this tutorial, you learned how to containerize a simple Python Flask application using Docker.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list