"Streamline Flask Apps: Nginx & Docker Compose Setup"

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:

How to spin up a simple Flask app using NGINX — in 15 minutes or less
How to spin up a simple Flask app using NGINX — in 15 minutes or less

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:

Python Flask Tutorial 5 - Database with Flask-SQLAlchemy
Python Flask Tutorial 5 - Database with Flask-SQLAlchemy

# 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.

day drinking on rainy days is the goal
day drinking on rainy days is the goal

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).

FLASK!!!! Funny Flask Messages, Etsy Flask, Funny Flask Captions, Funny Flask Quotes, Flask Meme, Eagle Rock, Drink Up, Cocktail Hour, Yacht Club
FLASK!!!! Funny Flask Messages, Etsy Flask, Funny Flask Captions, Funny Flask Quotes, Flask Meme, Eagle Rock, Drink Up, Cocktail Hour, Yacht Club
a silver flask shaped like a pocket watch on a white tableclothed surface
a silver flask shaped like a pocket watch on a white tableclothed surface
Python Tutorial (@aPythonTutorial) on X
Python Tutorial (@aPythonTutorial) on X
Pewter vs. Leather: Which Hip Flask Matches Your Style Best?
Pewter vs. Leather: Which Hip Flask Matches Your Style Best?
michael townsend
michael townsend
an old brass flask with three holes
an old brass flask with three holes
a person holding a flask with the letter r on it
a person holding a flask with the letter r on it
a person holding a flask in their hand
a person holding a flask in their hand
a metal container with a glass cover on top
a metal container with a glass cover on top
a white flask with the words i swear this is water written on it
a white flask with the words i swear this is water written on it
LMETJMA 1 4 5 6 7 8 9 10 oz Stainless Steel Hip Flask with Funnel Pocket Hip Flask Alcohol Wh...
LMETJMA 1 4 5 6 7 8 9 10 oz Stainless Steel Hip Flask with Funnel Pocket Hip Flask Alcohol Wh...
an old black and white photo with various items hanging from it
an old black and white photo with various items hanging from it
MINI HIP FLASK
MINI HIP FLASK
a stainless steel flask with a chain attached to it
a stainless steel flask with a chain attached to it
sunnies flask
sunnies flask
A gent carries a #flask for #winter #warmth and #good #spirits Silver Pocket Lighter, Stainless Hip Flask And Lighter, Hip Flask Drinking Whiskey, Vintage Dirty Old Men Lighter, Stone Hip Flask, Vintage Silver Pocket Lighter, Hand Holding Flask, Flask Aesthetic, Flask That Fits In Pocket
A gent carries a #flask for #winter #warmth and #good #spirits Silver Pocket Lighter, Stainless Hip Flask And Lighter, Hip Flask Drinking Whiskey, Vintage Dirty Old Men Lighter, Stone Hip Flask, Vintage Silver Pocket Lighter, Hand Holding Flask, Flask Aesthetic, Flask That Fits In Pocket
Flat metal flask
Flat metal flask
Hip Flask With Real Leather Holder - Choice of Leather Colors - Etsy
Hip Flask With Real Leather Holder - Choice of Leather Colors - Etsy
How to Clean a Hip Flask
How to Clean a Hip Flask
A George VI Silver Hip Flask, Sheffield 1946, Atkin Bros., 12.5cm, 160g
A George VI Silver Hip Flask, Sheffield 1946, Atkin Bros., 12.5cm, 160g
Please Log In
Please Log In
a person is holding a silver flask in their left hand, with the lid off
a person is holding a silver flask in their left hand, with the lid off
a silver flask with a man playing golf on it
a silver flask with a man playing golf on it