Building Docker Images for a Python Flask Server - A Step-by-Step Guide

Creating a Docker image for a Python Flask application can streamline deployment and ensure consistency across environments. Docker encapsulates the application and its dependencies into a single, portable container. In this blog post, we will walk through the process of building a Docker image for a simple Python Flask server.

2024 04 25

Step 1: Create Your Flask Application

First, you need a Flask application. Here's a simple example to get you started:

# app.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Welcome to the Flask App!"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

This code sets up a basic Flask server that responds with "Welcome to the Flask App!" at the root URL.

Step 2: Set Up the Requirements File

Next, create a requirements.txt file that lists the Flask library (and any other dependencies your application might have). This file tells Docker which Python packages are needed to run your application.

Flask==2.2.2
Werkzeug==2.2.2

Step 3: Create a Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Here’s how to set one up for your Flask application:

# Dockerfile
FROM python:3.10-slim

# Set the working directory in the container
WORKDIR /app

# Copy the dependencies file to the working directory
COPY requirements.txt .

# Install any dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the content of the local src directory to the working directory
COPY . .

# Specify the command to run on container start
CMD [ "python", "./app.py" ]

This Dockerfile does the following:

  • Starts from a Python 3.10 slim image.

  • Sets the working directory inside the container to /app.

  • Copies the requirements.txt file and installs the Python dependencies.

  • Copies the rest of your application's code into the container.

  • Specifies the command to run the application.

Step 4: Build the Docker Image

With the Dockerfile in place, you can build the Docker image. Open a terminal and run the following command from the directory where your Dockerfile is located:

docker build -t flask-app .

This command builds a new Docker image locally, tagging it as flask-app.

Step 5: Run the Flask Application in a Docker Container

To run your Flask application inside a Docker container, use the following command:

docker run -p 5000:5000 flask-app

This tells Docker to run the flask-app image as a container and map port 5000 on your local machine to port 5000 on the container, allowing you to access your Flask server via localhost:5000 in your web browser.

Conclusion

Congratulations! You have successfully containerized a Python Flask application using Docker. This setup not only simplifies the development and testing phases but also aids in production deployments, ensuring that your application runs the same way everywhere. Docker provides a robust and scalable solution for deploying web applications, making it an excellent choice for modern software development workflows.