Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Technical Guide: Setting Up A Flask Web Application With Docker, PostgreSQL, and Nginx

Technical Guide: Setting up a Flask Web Application with Docker, PostgreSQL, and Nginx

Uploaded by

Dex
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Technical Guide: Setting Up A Flask Web Application With Docker, PostgreSQL, and Nginx

Technical Guide: Setting up a Flask Web Application with Docker, PostgreSQL, and Nginx

Uploaded by

Dex
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Technical Guide: Setting up a Flask Web Application with Docker, PostgreSQL, and

Nginx
Introduction

In this guide, we will walk through the process of setting up a basic Flask web
application in a Dockerized environment with PostgreSQL as the database and Nginx
as the reverse proxy server. This setup is ideal for scalable production-ready
environments.
Prerequisites

Before starting, ensure you have the following installed:

Docker
Docker Compose
Basic understanding of Python, Flask, and Docker

1. Setting Up the Flask Application

Create a directory structure for your project:

bash

my_flask_app/
├── app/
│ ├── __init__.py
│ ├── routes.py
├── Dockerfile
├── requirements.txt
└── docker-compose.yml

1.1 Flask App Code

Create app/__init__.py:

python

from flask import Flask

def create_app():
"""Initialize the core application"""
app = Flask(__name__)

with app.app_context():
from . import routes
return app

Create app/routes.py:

python

from flask import jsonify

@app.route('/')
def home():
"""Route to return a simple JSON response."""
return jsonify({"message": "Welcome to the Flask App!"})

1.2 Requirements
Create requirements.txt to specify the Python packages needed for the Flask app:

txt

Flask==1.1.2
psycopg2-binary==2.8.6
gunicorn==20.0.4

2. Docker Setup
2.1 Dockerfile

Create a Dockerfile to define how the Flask app will be containerized:

Dockerfile

# Use an official Python runtime as the base image


FROM python:3.8-slim

# Set the working directory in the container


WORKDIR /app

# Copy the current directory contents into the container at /app


COPY . /app

# Install any required packages


RUN pip install --no-cache-dir -r requirements.txt

# Make port 5000 available to the world outside this container


EXPOSE 5000

# Define the environment variable


ENV FLASK_APP=app
ENV FLASK_RUN_HOST=0.0.0.0

# Run the application


CMD ["gunicorn", "-b", "0.0.0.0:5000", "app:create_app()"]

2.2 docker-compose.yml

docker-compose.yml will orchestrate services for Flask, PostgreSQL, and Nginx:

yaml

version: '3'

services:
web:
build: .
ports:
- "5000:5000"
depends_on:
- db
environment:
- DATABASE_URL=postgresql://postgres:password@db/postgres
networks:
- app-network

db:
image: postgres:12.0
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- app-network

nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- web
networks:
- app-network

volumes:
postgres_data:

networks:
app-network:

3. Configuring Nginx

Create nginx.conf for reverse proxy settings:

nginx

server {
listen 80;

location / {
proxy_pass http://web:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

4. Running the Application


4.1 Building and Running Docker Containers

To build and run the containers, navigate to the root project directory
(my_flask_app/) and run:

bash

docker-compose up --build

This command will:

Build the Flask application


Start PostgreSQL
Configure Nginx as a reverse proxy
You can access your Flask app at http://localhost.
5. Connecting Flask with PostgreSQL

To connect Flask to PostgreSQL, modify the database URI inside your Flask
configuration.

Update __init__.py to include the database connection:

python

import os
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

def create_app():
app = Flask(__name__)

# Configure PostgreSQL connection


app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL')

db.init_app(app)

with app.app_context():
from . import routes
db.create_all()
return app

Conclusion

You now have a working Flask application with PostgreSQL and Nginx, all managed
through Docker. This setup can be expanded and adapted for larger-scale production
applications, with additional security and optimization practices for performance.

You might also like