Dockerize backend, frontend, and database into separate containers

- backend/Dockerfile: multi-stage Python build (builder + slim runtime)
- frontend/Dockerfile: multi-stage Node build + nginx:alpine serving
- frontend/nginx.conf: SPA routing + /api/ reverse proxy to backend
- docker-compose.yml: production compose with health checks and proper dependency ordering
- docker-compose.dev.yml: dev overrides with hot reload via volume mounts

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
curo1305
2026-04-12 15:22:04 +02:00
parent 85f76c70de
commit 114df7162f
7 changed files with 157 additions and 32 deletions
+29 -15
View File
@@ -15,35 +15,51 @@ A fullstack SaaS web application built with FastAPI, React, and PostgreSQL.
- User registration and login (JWT auth) - User registration and login (JWT auth)
- Protected dashboard route - Protected dashboard route
- `/api/users/me` — authenticated user info - `/api/users/me` — authenticated user info
- Vite dev server proxies `/api` to FastAPI — no CORS issues in development - 3 separate Docker containers: `db` (PostgreSQL), `backend` (FastAPI), `frontend` (nginx)
## Containers
| Container | Image | Port | Description |
|---|---|---|---|
| `db` | postgres:16-alpine | 5432 | PostgreSQL database |
| `backend` | custom (python:3.12-slim) | 8000 | FastAPI management API |
| `frontend` | custom (nginx:alpine) | 80 | React UI served by nginx |
The frontend nginx container proxies `/api/*` to the backend container internally — no CORS headers needed in production.
## Installation ## Installation
### Prerequisites ### Prerequisites
- Python 3.11+ - Docker + Docker Compose
- Node.js 18+
- PostgreSQL 16 (or Docker)
### Option A — Docker (recommended) ### Production
```bash ```bash
git clone <repo> git clone <repo>
cd destroying_sap cd destroying_sap
cp .env.example backend/.env cp .env.example backend/.env # edit SECRET_KEY at minimum
docker compose up --build docker compose up --build -d
``` ```
- Frontend: http://localhost:5173 - Frontend: http://localhost
- Backend / API docs: http://localhost:8000/docs - API docs: http://localhost:8000/docs
### Option B — Local ### Development (hot reload)
```bash
docker compose -f docker-compose.yml -f docker-compose.dev.yml up --build
```
- Frontend (Vite): http://localhost:5173
- Backend (uvicorn --reload): http://localhost:8000
### Local (no Docker)
**1. Start PostgreSQL** **1. Start PostgreSQL**
```bash ```bash
docker compose up db -d docker compose up db -d
# or use a local PostgreSQL instance and update backend/.env
``` ```
**2. Backend** **2. Backend**
@@ -52,7 +68,7 @@ docker compose up db -d
cd backend cd backend
python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -e ".[dev]" pip install -e ".[dev]"
cp ../.env.example .env # edit DATABASE_URL and SECRET_KEY as needed cp ../.env.example .env
alembic upgrade head alembic upgrade head
uvicorn app.main:app --reload uvicorn app.main:app --reload
``` ```
@@ -60,9 +76,7 @@ uvicorn app.main:app --reload
**3. Frontend** **3. Frontend**
```bash ```bash
cd frontend cd frontend && npm install && npm run dev
npm install
npm run dev
``` ```
## Environment Variables ## Environment Variables
+26
View File
@@ -0,0 +1,26 @@
# ── Stage 1: dependency installation ─────────────────────────────────────────
FROM python:3.12-slim AS builder
WORKDIR /app
RUN pip install --upgrade pip
COPY pyproject.toml .
RUN pip install --prefix=/install .
# ── Stage 2: runtime ──────────────────────────────────────────────────────────
FROM python:3.12-slim
WORKDIR /app
# Copy installed packages from builder
COPY --from=builder /install /usr/local
# Copy application source
COPY app ./app
COPY alembic ./alembic
COPY alembic.ini .
EXPOSE 8000
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
+19
View File
@@ -0,0 +1,19 @@
# 2026-04-12 — Dockerize all containers
**Timestamp:** 2026-04-12T15:00:00
## Summary
Added proper Dockerfiles for backend and frontend. Split compose setup into production (`docker-compose.yml`) and development (`docker-compose.dev.yml`) modes. Updated README to reflect new container architecture.
## Files Added
- `backend/Dockerfile` — multi-stage build: pip install in builder stage, minimal python:3.12-slim runtime; runs uvicorn in production mode
- `frontend/Dockerfile` — multi-stage build: Node 20 Alpine builds the Vite bundle, nginx:alpine serves the static files
- `frontend/nginx.conf` — nginx config for SPA fallback routing and `/api/` reverse proxy to the backend container
- `docker-compose.dev.yml` — development overrides: mounts source for hot reload, uses Vite dev server instead of nginx
## Files Modified
- `docker-compose.yml` — rewritten: proper `build.context` + `dockerfile` references, health check on db, `depends_on` with `condition: service_healthy`, env var interpolation via `${VAR:-default}`, frontend now served on port 80 via nginx
- `README.md` — updated Current State, added Containers table, replaced install options with Production / Development / Local sections
+20
View File
@@ -0,0 +1,20 @@
# Development overrides — hot reload for backend and frontend
# Usage: docker compose -f docker-compose.yml -f docker-compose.dev.yml up --build
services:
backend:
command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
volumes:
- ./backend:/app
frontend:
build:
context: ./frontend
target: builder # stop at the Node stage, skip nginx
command: npm run dev -- --host 0.0.0.0
ports:
- "5173:5173"
volumes:
- ./frontend:/app
- /app/node_modules
+28 -17
View File
@@ -1,35 +1,46 @@
services: services:
# ── Database ────────────────────────────────────────────────────────────────
db: db:
image: postgres:16 image: postgres:16-alpine
restart: unless-stopped
environment: environment:
POSTGRES_USER: postgres POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: password POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-password}
POSTGRES_DB: destroying_sap POSTGRES_DB: ${POSTGRES_DB:-destroying_sap}
ports: ports:
- "5432:5432" - "5432:5432"
volumes: volumes:
- postgres_data:/var/lib/postgresql/data - postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-postgres}"]
interval: 5s
timeout: 5s
retries: 10
# ── Backend (management) ────────────────────────────────────────────────────
backend: backend:
build: ./backend build:
command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload context: ./backend
volumes: dockerfile: Dockerfile
- ./backend:/app restart: unless-stopped
env_file: ./backend/.env
environment:
DATABASE_URL: postgresql+asyncpg://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-password}@db:5432/${POSTGRES_DB:-destroying_sap}
ports: ports:
- "8000:8000" - "8000:8000"
environment:
DATABASE_URL: postgresql+asyncpg://postgres:password@db:5432/destroying_sap
depends_on: depends_on:
- db db:
condition: service_healthy
# ── Frontend (UI) ────────────────────────────────────────────────────────────
frontend: frontend:
build: ./frontend build:
command: npm run dev -- --host context: ./frontend
volumes: dockerfile: Dockerfile
- ./frontend:/app restart: unless-stopped
- /app/node_modules
ports: ports:
- "5173:5173" - "80:80"
depends_on: depends_on:
- backend - backend
+18
View File
@@ -0,0 +1,18 @@
# ── Stage 1: build ────────────────────────────────────────────────────────────
FROM node:20-alpine AS builder
WORKDIR /app
COPY package.json package-lock.json* ./
RUN npm ci
COPY . .
RUN npm run build
# ── Stage 2: serve with nginx ─────────────────────────────────────────────────
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
+17
View File
@@ -0,0 +1,17 @@
server {
listen 80;
root /usr/share/nginx/html;
index index.html;
# Proxy API calls to the backend container
location /api/ {
proxy_pass http://backend:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
# SPA fallback — all non-asset routes serve index.html
location / {
try_files $uri $uri/ /index.html;
}
}