Refactor Docker setup and migrate to PostgreSQL

Streamlined Dockerfiles with clearer ENV variables and build args. Switched backend database from MySQL to PostgreSQL, updated configurations accordingly, and added robust Docker Compose services for better orchestration, including health checks and persistent storage.
This commit is contained in:
GotthardG
2025-04-09 15:09:22 +02:00
parent 248085b3c4
commit bb6cca4f23
11 changed files with 192 additions and 95 deletions

View File

@@ -1,8 +1,9 @@
version: "3.9"
services:
backend:
container_name: backend
build:
context: . # Build the image from the parent directory
context: . # Build the image from the parent directory
dockerfile: backend/Dockerfile
ports:
@@ -11,22 +12,76 @@ services:
- ./backend:/app/backend # Map backend directory to /app/backend
- ./app:/app/app # Map app directory to /app/app
- ./config_dev.json:/app/backend/config_dev.json # Explicitly map config_dev.json
- ./backend/ssl:/app/backend/ssl # clearly mount SSL files explicitly into Docker
working_dir: /app/backend # Set working directory to backend/
command: python main.py # Command to run main.py
depends_on: # ⬅️ New addition: wait until postgres is started
- postgres
healthcheck:
test: [ "CMD-SHELL", "curl -k -f https://localhost:8000/openapi.json || exit 1" ]
interval: 5s
timeout: 5s
retries: 5
environment: # ⬅️ Provide DB info to your backend
ENVIRONMENT: dev
DB_USERNAME: dev_user
DB_PASSWORD: dev_password
DB_HOST: postgres
DB_NAME: aare_dev_db
postgres: # ⬅️ New service (our PostgreSQL database)
image: postgres:16
environment:
POSTGRES_USER: dev_user
POSTGRES_PASSWORD: dev_password
POSTGRES_DB: aare_dev_db
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
frontend:
depends_on:
backend:
condition: service_healthy
build:
context: ./frontend
dockerfile: Dockerfile
args:
- VITE_OPENAPI_BASE_DEV=${VITE_OPENAPI_BASE_DEV}
- VITE_SSL_KEY_PATH=${VITE_SSL_KEY_PATH}
- VITE_SSL_CERT_PATH=${VITE_SSL_CERT_PATH}
- NODE_ENV=${NODE_ENV}
ports:
- "5173:5173" # Map container port 5173 to host
- "5173:5173"
volumes:
- ./frontend:/app
- /app/node_modules # ⬅️ explicit exclusion! ensures Docker-provided modules retain explicitly.
- ./backend/ssl:/app/backend/ssl
- ./backend/config_dev.json:/app/backend/config_${ENVIRONMENT}.json # Dynamically maps config based on environment
command: sh -c "npm run dev & ENVIRONMENT=dev npm run watch:openapi"
logistics_frontend:
build:
context: ./logistics
dockerfile: Dockerfile
args: # 👈 explicitly pass build args from .env
- VITE_OPENAPI_BASE_DEV=${VITE_OPENAPI_BASE_DEV}
- VITE_SSL_KEY_PATH=${VITE_SSL_KEY_PATH}
- VITE_SSL_CERT_PATH=${VITE_SSL_CERT_PATH}
- NODE_ENV=${NODE_ENV}
ports:
- "3000:3000"
depends_on:
- frontend # Ensure OpenAPI models are available
- frontend # Ensure OpenAPI models are available
volumes:
- ./logistics/src:/app/src # explicitly for active dev (hot reload)
- ./backend/ssl:/app/backend/ssl # clearly mount SSL files explicitly into Docker
environment:
- VITE_OPENAPI_BASE_DEV=${VITE_OPENAPI_BASE_DEV}
volumes: # ⬅️ Persistent storage for PostgreSQL data
pgdata: