♻️ Simplify environment configuration (#2413)

This commit is contained in:
Sebastián Ramírez
2026-08-09 22:26:19 +02:00
committed by GitHub
parent 2d04828d6a
commit 9c8983e2a5
6 changed files with 47 additions and 68 deletions
+3 -7
View File
@@ -6,10 +6,8 @@ DOMAIN=localhost
# DOMAIN=localhost.tiangolo.com
# Public frontend URL, used by the backend to generate links in emails.
# Keep the Vite URL for local development; deployments use the application URL.
# Deployments derive this from DOMAIN in compose.deploy.yml.
FRONTEND_HOST=http://localhost:5173
# In staging and production, set this env var to the frontend host, e.g.
# FRONTEND_HOST=https://example.com
# Environment: local, staging, production
ENVIRONMENT=local
@@ -17,7 +15,8 @@ ENVIRONMENT=local
PROJECT_NAME="Full Stack FastAPI Project"
# Backend
BACKEND_CORS_ORIGINS="http://localhost,https://localhost,http://localhost.tiangolo.com"
# Additional allowed CORS origins, separated by commas
BACKEND_CORS_ORIGINS=
SECRET_KEY=changethis
FIRST_SUPERUSER=admin@example.com
FIRST_SUPERUSER_PASSWORD=changethis
@@ -39,6 +38,3 @@ POSTGRES_USER=postgres
POSTGRES_PASSWORD=changethis
SENTRY_DSN=
# Configure these with your own Docker registry images
DOCKER_IMAGE_BACKEND=backend
-1
View File
@@ -19,7 +19,6 @@ jobs:
env:
ENVIRONMENT: production
DOMAIN: ${{ secrets.DOMAIN }}
FRONTEND_HOST: https://${{ secrets.DOMAIN }}
SECRET_KEY: ${{ secrets.SECRET_KEY }}
FIRST_SUPERUSER: ${{ secrets.FIRST_SUPERUSER }}
FIRST_SUPERUSER_PASSWORD: ${{ secrets.FIRST_SUPERUSER_PASSWORD }}
-1
View File
@@ -19,7 +19,6 @@ jobs:
env:
ENVIRONMENT: staging
DOMAIN: ${{ secrets.DOMAIN }}
FRONTEND_HOST: https://${{ secrets.DOMAIN }}
SECRET_KEY: ${{ secrets.SECRET_KEY }}
FIRST_SUPERUSER: ${{ secrets.FIRST_SUPERUSER }}
FIRST_SUPERUSER_PASSWORD: ${{ secrets.FIRST_SUPERUSER_PASSWORD }}
+6
View File
@@ -44,8 +44,14 @@ services:
# Use the Let's Encrypt resolver
- traefik.http.routers.adminer-https.tls.certresolver=le
prestart:
environment:
FRONTEND_HOST: https://${DOMAIN?Variable not set}
backend:
restart: always
environment:
FRONTEND_HOST: https://${DOMAIN?Variable not set}
labels:
# Route HTTPS traffic for this domain
- traefik.http.routers.backend-https.rule=Host(`${DOMAIN?Variable not set}`)
+35 -52
View File
@@ -49,65 +49,36 @@ services:
# Define the port inside of the Docker service to use
- traefik.http.services.adminer.loadbalancer.server.port=8080
prestart:
image: '${DOCKER_IMAGE_BACKEND?Variable not set}:${TAG-latest}'
build:
context: .
dockerfile: backend/Dockerfile
depends_on:
db:
condition: service_healthy
restart: true
command: bash scripts/prestart.sh
env_file:
- .env
environment:
- DOMAIN=${DOMAIN}
- FRONTEND_HOST=${FRONTEND_HOST?Variable not set}
- ENVIRONMENT=${ENVIRONMENT}
- BACKEND_CORS_ORIGINS=${BACKEND_CORS_ORIGINS}
- SECRET_KEY=${SECRET_KEY?Variable not set}
- FIRST_SUPERUSER=${FIRST_SUPERUSER?Variable not set}
- FIRST_SUPERUSER_PASSWORD=${FIRST_SUPERUSER_PASSWORD?Variable not set}
- SMTP_HOST=${SMTP_HOST}
- SMTP_USER=${SMTP_USER}
- SMTP_PASSWORD=${SMTP_PASSWORD}
- EMAILS_FROM_EMAIL=${EMAILS_FROM_EMAIL}
- POSTGRES_SERVER=db
- POSTGRES_PORT=${POSTGRES_PORT}
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_USER=${POSTGRES_USER?Variable not set}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD?Variable not set}
- SENTRY_DSN=${SENTRY_DSN}
backend:
image: '${DOCKER_IMAGE_BACKEND?Variable not set}:${TAG-latest}'
image: backend:latest
depends_on:
db:
condition: service_healthy
restart: true
prestart:
condition: service_completed_successfully
env_file:
- .env
environment:
- DOMAIN=${DOMAIN}
- FRONTEND_HOST=${FRONTEND_HOST?Variable not set}
- ENVIRONMENT=${ENVIRONMENT}
- BACKEND_CORS_ORIGINS=${BACKEND_CORS_ORIGINS}
- SECRET_KEY=${SECRET_KEY?Variable not set}
- FIRST_SUPERUSER=${FIRST_SUPERUSER?Variable not set}
- FIRST_SUPERUSER_PASSWORD=${FIRST_SUPERUSER_PASSWORD?Variable not set}
- SMTP_HOST=${SMTP_HOST}
- SMTP_USER=${SMTP_USER}
- SMTP_PASSWORD=${SMTP_PASSWORD}
- EMAILS_FROM_EMAIL=${EMAILS_FROM_EMAIL}
- POSTGRES_SERVER=db
- POSTGRES_PORT=${POSTGRES_PORT}
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_USER=${POSTGRES_USER?Variable not set}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD?Variable not set}
- SENTRY_DSN=${SENTRY_DSN}
# The YAML anchor lets the prestart service reuse this environment mapping.
environment: &backend-environment
FRONTEND_HOST: ${FRONTEND_HOST?Variable not set}
ENVIRONMENT: ${ENVIRONMENT}
PROJECT_NAME: ${PROJECT_NAME?Variable not set}
BACKEND_CORS_ORIGINS: ${BACKEND_CORS_ORIGINS}
SECRET_KEY: ${SECRET_KEY?Variable not set}
FIRST_SUPERUSER: ${FIRST_SUPERUSER?Variable not set}
FIRST_SUPERUSER_PASSWORD: ${FIRST_SUPERUSER_PASSWORD?Variable not set}
SMTP_HOST: ${SMTP_HOST}
SMTP_USER: ${SMTP_USER}
SMTP_PASSWORD: ${SMTP_PASSWORD}
EMAILS_FROM_EMAIL: ${EMAILS_FROM_EMAIL}
SMTP_TLS: ${SMTP_TLS}
SMTP_SSL: ${SMTP_SSL}
SMTP_PORT: ${SMTP_PORT}
POSTGRES_SERVER: db
POSTGRES_PORT: ${POSTGRES_PORT}
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: ${POSTGRES_USER?Variable not set}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD?Variable not set}
SENTRY_DSN: ${SENTRY_DSN}
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/api/v1/utils/health-check/"]
@@ -129,5 +100,17 @@ services:
- traefik.http.routers.backend-http.rule=Host(`${DOMAIN?Variable not set}`)
- traefik.http.routers.backend-http.entrypoints=http
prestart:
image: backend:latest
build:
context: .
dockerfile: backend/Dockerfile
depends_on:
db:
condition: service_healthy
restart: true
command: bash scripts/prestart.sh
environment: *backend-environment
volumes:
app-db-data:
+3 -7
View File
@@ -58,6 +58,8 @@ Set the `DOMAIN`, by default `localhost` (for development), but when deploying y
export DOMAIN=fastapi-project.example.com
```
The deployment Docker Compose configuration also uses `DOMAIN` to set the public frontend URL used in links generated by the backend.
Set the `POSTGRES_PASSWORD` to something different than `changethis`:
```bash
@@ -72,18 +74,12 @@ export SECRET_KEY="changethis"
Note: you can use the Python command above to generate a secure secret key.
Set the `FIRST_SUPER_USER_PASSWORD` to something different than `changethis`:
Set the `FIRST_SUPERUSER_PASSWORD` to something different than `changethis`:
```bash
export FIRST_SUPERUSER_PASSWORD="changethis"
```
Set the `FRONTEND_HOST` to the application URL. It is used to generate links in emails:
```bash
export FRONTEND_HOST="https://${DOMAIN?Variable not set}"
```
You can set several other environment variables:
* `PROJECT_NAME`: The name of the project, used in the API for the docs and emails.