PyTupli Deployment
This document outlines the steps to deploy the PyTupli application using Docker Compose for both development and production environments.
Prerequisites
Docker and Docker Compose installed.
Git (for cloning the repository).
Ports 80, 443 accessible from the Internet (for production only).
Directory Structure
docker_compose/: Contains Docker Compose files and related scripts.docker-compose.yaml: For development.docker-compose.prod.yaml: For production.Dockerfile: Defines the Pytupli API server image.init-letsencrypt.sh: Script to initialize SSL certificates for production.env.template: Template for API server environment variables.env.mongo.template: Template for MongoDB environment variables.env.nginx.template: Template for Nginx environment variables.
data/: Contains persistent data for services, including Nginx configurations and Certbot certificates (once acquired).
Environment Configuration
Before running the application, you need to set up environment variable files. In the deployment/docker_compose/ directory:
API Server Configuration: Copy
env.templateto.envand fill in the required values.cp env.template .env
MongoDB Configuration: Copy
env.mongo.templateto.env.mongoand update values if desired.cp env.mongo.template .env.mongo
Nginx Configuration (Production Only): Copy
env.nginx.templateto.env.nginxand set yourDOMAINandEMAILfor SSL certificate generation.cp env.nginx.template .env.nginx
Production Deployment
The production setup includes the API server, MongoDB, Nginx as a reverse proxy, and Certbot for SSL certificate management with Let’s Encrypt.
Services
api_server: The Pytupli FastAPI application.mongo: MongoDB database instance.nginx: Nginx reverse proxy for handling HTTP/HTTPS traffic and SSL termination.certbot: Manages SSL certificates using Let’s Encrypt.
Steps
Navigate to the
deployment/docker_compose/directory:cd deployment/docker_compose
Ensure your
.env,.env.mongo, and.env.nginxfiles are configured. Pay special attention toDOMAINin.env.nginxfor SSL.Initialize SSL Certificates (First-time setup): Make the
init-letsencrypt.shscript executable and run it:chmod +x init-letsencrypt.sh ./init-letsencrypt.sh
This script will:
Download recommended TLS parameters.
Create a dummy certificate for initial Nginx startup.
Start Nginx.
Request a Let’s Encrypt certificate for your domain.
Reload Nginx with the new certificate.
Set up automatic certificate renewal.
Start Production Services: For subsequent starts, use the production Docker Compose file:
docker compose -f docker-compose.prod.yaml -p pytupli-stack --profile with_mongo up --build -d
The
-p pytupli-stackflag sets a project name to avoid conflicts. The--profile with_mongoensures themongoservice defined indocker-compose.prod.yamlis started. If you wish to use an external MongoDB instance, you can omit this profile and configure theMONGO_CONNECTION_STRINGin your.envfile to point to your external database. Theinit-letsencrypt.shscript uses thewith_mongoprofile by default when starting services.
The application will be accessible via http://<your_domain> and https://<your_domain>.
Data Persistence
MongoDB: Data is persisted in a Docker volume named
mongo_data.Nginx: Configuration is mounted from
../data/nginx.Certbot: Certificates and configuration are mounted from
../data/certbot.
Ensure these volumes are backed up as needed.
Development Deployment
The development setup runs the Pytupli API server and a MongoDB database.
Services
api_server: The Pytupli FastAPI application.mongo: MongoDB database instance.
Steps
Navigate to the
deployment/docker_compose/directory:cd deployment/docker_compose
Ensure your
.envand.env.mongofiles are configured.Build and start the services. To include the local MongoDB container, use the
--profile with_mongoflag:docker compose -f docker-compose.yaml --profile with_mongo up --build
To run in detached mode, add the
-dflag:docker compose -f docker-compose.yaml --profile with_mongo up --build -d
If you wish to use an external MongoDB instance, you can omit the
--profile with_mongoflag and ensure theMONGO_CONNECTION_STRINGin your.envfile points to your external database.
The API server will be accessible at http://localhost:${PORT} (default 8080, as specified in your .env or Dockerfile).