Streamline Flask Application Deployment: Flask, Nginx, and Docker Compose
In the dynamic world of web development, deploying applications efficiently and securely is paramount. This article explores how to leverage Flask, Nginx, and Docker Compose to streamline your deployment process, ensuring scalability, reliability, and ease of management.
Understanding the Stack: Flask, Nginx, and Docker Compose
Before diving into the implementation, let's briefly understand each component:
- Flask: A lightweight Python web framework, perfect for small applications and APIs.
- Nginx: A high-performance web server and reverse proxy, handling HTTP requests and serving static files.
- Docker Compose: A tool for defining and running multi-container Docker applications, enabling easy scaling and management.
Setting Up the Project Structure
First, create a project directory and navigate into it. Then, set up the following structure:

flask-nginx-docker/ |-- app/ | |-- __init__.py | |-- routes.py | |-- templates/ | |-- index.html |-- Dockerfile |-- docker-compose.yml |-- requirements.txt
Creating the Flask Application
In the app directory, create a simple Flask application with an index.html template. Add the following content to __init__.py and routes.py:
# app/__init__.py
from flask import Flask
app = Flask(__name__)
# Import and register blueprints here
from . import routes
if __name__ == '__main__':
app.run(debug=True)
# app/routes.py
from flask import render_template
from . import app
@app.route('/')
def index():
return render_template('index.html')
Configuring Nginx
Create an nginx.conf file in the project root with the following content:
user nginx
worker_processes auto;
events { worker_connections 1024; }
http {
sendfile on;
server {
listen 80;
location / {
proxy_pass http://flask_app:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
Dockerizing the Application
Create a Dockerfile in the project root to build the Docker image for your Flask application:

# Dockerfile FROM python:3.8-slim-buster WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . CMD ["python", "-m" "flask", "run", "--host=0.0.0.0"]
Defining Services with Docker Compose
Create a docker-compose.yml file in the project root to define the services for your application:
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/app
nginx:
image: nginx:stable
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- web
Running the Application
With the project structure set up, run the following command to start your application:
docker-compose up -d
Access your application at http://localhost in your web browser.

Scaling and Managing with Docker Compose
Docker Compose enables easy scaling and management of your application. To scale the Flask service, add the following line to your docker-compose.yml file:
web:
...
deploy:
mode: replicated
replicas: 3
Then, run docker-compose up -d to start three instances of your Flask application.
Docker Compose also provides convenient commands for managing your application, such as stopping (docker-compose stop), restarting (docker-compose restart), and removing containers (docker-compose rm).






















