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.
- Install Dependencies: Install Python, Flask, Nginx, and Certbot.
- Configure Flask: Create a simple Flask application.
- Configure Nginx: Set up Nginx to reverse proxy to the Flask application.
- Configure Certbot: Install Certbot and configure it to obtain and renew SSL certificates.
- Dockerfile Creation: Write a Dockerfile that combines all these components.
- Run the Container: Build and run the Docker container.
Create a directory for your project and navigate into it. Then create the following files:
# 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
Flask
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;
}
}
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 the Docker Image:
docker build -t my_flask_app .
-
Run the Docker Container:
docker run -p 80:80 -p 443:443 my_flask_app
- Certbot Email and Domain: Replace
[email protected]
andyourdomain.com
with your actual email and domain name. - Volumes for Certificates: To persist SSL certificates, you might want to map volumes for
/etc/letsencrypt
and/var/lib/letsencrypt
.
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.