Dockerizing a Python Flask Web App with Docker Compose

In today’s developed world, containerization has become a standard for building and deploying applications. If we look at all this, one of the most powerful tools we have chosen is Docker, and combined with Docker Compose, it simplifies the management of multi-container applications.

In this Papayacoders blog, we will explain how to Dockerize a Python Flask web app using Docker Compose.

What is Flask?

Flask is a lightweight and flexible Python web framework and is perfect for small applications and prototyping, yet powerful enough for production-grade applications.

What is Docker & Docker Compose?

  • Docker allows developers to package applications as well as dependencies into containers that can run continuously on any system.
  • Docker Compose is a tool used to define as well as manage multi-container Docker applications. You can use the docker-compose.yml file to configure your application’s services, network, and volumes.

Step-by-Step Guide to Dockerize a Flask App

1. Project Structure

Let’s assume your project looks like this:

flask-app/
│
├── app.py
├── requirements.txt
├── Dockerfile
└── docker-compose.yml

2. Create the Flask App

app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, Docker World!"

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

3. Add Dependencies

requirements.txt

Flask==2.3.2

This file tells Docker which Python packages to install inside the container.

4. Write the Dockerfile

Dockerfile

# Use an official Python base image
FROM python:3.10-slim

# Set work directory
WORKDIR /app

# Copy the current directory contents into the container
COPY . .

# Install the dependencies
RUN pip install -r requirements.txt

# Expose the port Flask runs on
EXPOSE 5000

# Run the application
CMD ["python", "app.py"]

5. Set Up Docker Compose

docker-compose.yml

version: '3.8'

services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/app
    environment:
      - FLASK_ENV=development

With this file, Docker Compose will:

  • Build the image using the Dockerfile.
  • Map port 5000 of your machine to port 5000 inside the container.
  • Mount the local code directory for live updates (great for development).

6. Run the Application

From the terminal, navigate to the flask-app directory and run:

docker-compose up

Once the build completes, you should see your Flask app running at:

http://localhost:5000

Benefits of Dockerizing Flask Apps

  • Consistency: Same environment across development, testing, and production.
  • Portability: Runs anywhere Docker is installed.
  • Isolation: Keeps dependencies contained in their own environment.
  • Scalability: Easy to scale using Compose or Docker Swarm.

Cleaning Up

To stop and remove containers, networks, and volumes defined in the Compose file, use:

docker-compose down

Read also:-

Conclusion:-

Dockerizing a Python Flask web app with Docker Compose not only greatly simplifies the deployment process but also enhances portability, scalability, and team collaboration. Whether you’re developing locally or deploying to the cloud, containers are a modern indispensable tool for web applications.

Start small, experiment with services, and you’ll quickly realize how powerful Docker Compose can be for your Python projects.

Share this post :

Leave a Reply

Your email address will not be published. Required fields are marked *