Skip to content

TyrelCB/boilerplate_nginx_flask_certbot_contianer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Setting up Nginx, Certbot, and Flask in a single Docker container involves creating a Dockerfile that installs all the necessary dependencies and configures the services correctly. Below is a step-by-step guide and an example of a Dockerfile for this setup.

Step-by-Step Guide

  1. Install Dependencies: Install Python, Flask, Nginx, and Certbot.
  2. Configure Flask: Create a simple Flask application.
  3. Configure Nginx: Set up Nginx to reverse proxy to the Flask application.
  4. Configure Certbot: Install Certbot and configure it to obtain and renew SSL certificates.
  5. Dockerfile Creation: Write a Dockerfile that combines all these components.
  6. Run the Container: Build and run the Docker container.

Example Dockerfile

Create a directory for your project and navigate into it. Then create the following files:

Dockerfile

# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Install dependencies
RUN apt-get update && apt-get install -y \
    nginx \
    certbot \
    python3-certbot-nginx \
    && apt-get clean

# Set the working directory
WORKDIR /app

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

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

# Copy Nginx configuration file
COPY nginx.conf /etc/nginx/sites-available/default

# Expose port 80 and 443
EXPOSE 80 443

# Command to run the Flask application and Nginx
CMD service nginx start && certbot --nginx --non-interactive --agree-tos --email [email protected] -d yourdomain.com && python app.py

requirements.txt

Flask

nginx.conf

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1: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;
    }

    location ~ /.well-known/acme-challenge {
        allow all;
    }
}

app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

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

Build and Run the Docker Container

  1. Build the Docker Image:

    docker build -t my_flask_app .
  2. Run the Docker Container:

    docker run -p 80:80 -p 443:443 my_flask_app

Notes

  1. Certbot Email and Domain: Replace [email protected] and yourdomain.com with your actual email and domain name.
  2. Volumes for Certificates: To persist SSL certificates, you might want to map volumes for /etc/letsencrypt and /var/lib/letsencrypt.

Example with Volumes

To ensure that your certificates are persisted, you can modify the run command:

docker run -p 80:80 -p 443:443 -v /etc/letsencrypt:/etc/letsencrypt -v /var/lib/letsencrypt:/var/lib/letsencrypt my_flask_app

This setup should give you a basic Docker container running Nginx, Certbot, and Flask. Make sure to adjust configurations according to your specific needs.

About

Boilerplate for a TLS backend API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages