From 40d9fb8dcfdf3e2434c932b9d973934839bfd88f Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 3 Apr 2026 03:11:28 +0800 Subject: [PATCH] Add production Dockerfile for backend deployment --- backend/.dockerignore | 13 +++++++++++++ backend/Dockerfile | 19 +++++++++++++++++++ backend/README.md | 23 +++++++++++++++++++++++ docs/LOCAL_DEV_RUNBOOK.md | 4 ++++ 4 files changed, 59 insertions(+) create mode 100644 backend/.dockerignore create mode 100644 backend/Dockerfile diff --git a/backend/.dockerignore b/backend/.dockerignore new file mode 100644 index 0000000..4dd4df8 --- /dev/null +++ b/backend/.dockerignore @@ -0,0 +1,13 @@ +.git +.gitignore +.venv +__pycache__ +*.pyc +*.pyo +*.pyd +.pytest_cache +.ruff_cache +tests +.env +.env.development +*.log diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..09af022 --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.12-slim + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 + +WORKDIR /app + +# Install runtime dependencies from local package metadata. +COPY pyproject.toml /app/pyproject.toml +COPY app /app/app +COPY scripts /app/scripts +COPY README.md /app/README.md + +RUN pip install --no-cache-dir --upgrade pip && \ + pip install --no-cache-dir . + +EXPOSE 8000 + +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] diff --git a/backend/README.md b/backend/README.md index b61dee6..671ed23 100644 --- a/backend/README.md +++ b/backend/README.md @@ -12,6 +12,29 @@ psql "$DATABASE_URL" -f scripts/init_schema.sql ./scripts/start_dev.sh ``` +## Docker (VPS / Production) + +Build image: +```bash +cd backend +docker build -t memberapi-backend:latest . +``` + +Run container: +```bash +docker run -d \ + --name memberapi-backend \ + --restart unless-stopped \ + -p 127.0.0.1:8000:8000 \ + --env-file .env \ + memberapi-backend:latest +``` + +Health check: +```bash +curl http://127.0.0.1:8000/healthz +``` + ## Keycloak env - Required: diff --git a/docs/LOCAL_DEV_RUNBOOK.md b/docs/LOCAL_DEV_RUNBOOK.md index 930b746..7d0fbaf 100644 --- a/docs/LOCAL_DEV_RUNBOOK.md +++ b/docs/LOCAL_DEV_RUNBOOK.md @@ -87,3 +87,7 @@ npm run dev 1. 建立 `api_client`。 2. 用 `X-Client-Key` + `X-API-Key` 呼叫 `/internal/*`。 3. 驗證未授權 key 會被拒絕。 + +## 8) VPS Docker 啟動(Backend) +- Dockerfile: [backend/Dockerfile](../backend/Dockerfile) +- 建置與啟動指令:參考 [backend/README.md](../backend/README.md) 的 `Docker (VPS / Production)` 章節。