forked from mtusi2026/mtusi2026lab2
build(docker): add Compose stack with migrations and persistent storage
This commit is contained in:
@@ -16,5 +16,10 @@ backups
|
|||||||
.venv
|
.venv
|
||||||
__pycache__
|
__pycache__
|
||||||
node_modules
|
node_modules
|
||||||
|
src/generated
|
||||||
|
dist
|
||||||
|
coverage
|
||||||
|
*.tsbuildinfo
|
||||||
|
.pnpm-store
|
||||||
evidence
|
evidence
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|||||||
+8
-6
@@ -1,11 +1,13 @@
|
|||||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
|
||||||
# These are logical interface suggestions; map actual framework names in REPORT.md.
|
|
||||||
APP_HOST=0.0.0.0
|
APP_HOST=0.0.0.0
|
||||||
APP_PORT=8080
|
APP_PORT=8080
|
||||||
APP_ENV=lab
|
APP_ENV=lab
|
||||||
SESSION_TTL_SECONDS=3600
|
SESSION_TTL_SECONDS=3600
|
||||||
# Choose credentials locally. File paths contain no secret values.
|
POSTGRES_DB=chat
|
||||||
DATABASE_URL_FILE=/run/secrets/database_url
|
POSTGRES_USER=app
|
||||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
POSTGRES_PASSWORD=local_dev_password
|
||||||
|
POSTGRES_PORT=5432
|
||||||
|
DATABASE_URL=postgresql://app:local_dev_password@localhost:5432/chat
|
||||||
|
CACHE_URL=redis://localhost:6379/0
|
||||||
|
REDIS_PORT=6379
|
||||||
|
CACHE_PREFIX=messenger:cache:v1
|
||||||
SESSION_STORE=postgres
|
SESSION_STORE=postgres
|
||||||
CACHE_URL_FILE=/run/secrets/cache_url
|
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
test/*.sh text eol=lf
|
||||||
|
test/*.cmd text eol=crlf
|
||||||
|
scripts/*.sh text eol=lf
|
||||||
|
scripts/*.cmd text eol=crlf
|
||||||
+3
-4
@@ -1,4 +1,3 @@
|
|||||||
# Local/private state
|
|
||||||
.env
|
.env
|
||||||
.env.*
|
.env.*
|
||||||
!.env.example
|
!.env.example
|
||||||
@@ -13,7 +12,6 @@ certs/private/
|
|||||||
volumes/
|
volumes/
|
||||||
data/
|
data/
|
||||||
backups/
|
backups/
|
||||||
# Runtime/build artifacts
|
|
||||||
.venv/
|
.venv/
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[cod]
|
*.py[cod]
|
||||||
@@ -24,9 +22,10 @@ target/
|
|||||||
bin/
|
bin/
|
||||||
obj/
|
obj/
|
||||||
coverage/
|
coverage/
|
||||||
|
*.tsbuildinfo
|
||||||
|
.pnpm-store/
|
||||||
|
src/generated/prisma/
|
||||||
*.log
|
*.log
|
||||||
.DS_Store
|
.DS_Store
|
||||||
.idea/
|
.idea/
|
||||||
.vscode/
|
.vscode/
|
||||||
# Keep small, sanitized evidence summaries in evidence/ under version control.
|
|
||||||
# Inspect git diff --cached: ignore rules are not a secret scanner.
|
|
||||||
|
|||||||
+54
@@ -0,0 +1,54 @@
|
|||||||
|
FROM node:24.20.0-bookworm-slim AS base
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
RUN apt-get update \
|
||||||
|
&& apt-get install -y --no-install-recommends openssl \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
RUN npm install --global pnpm@10.14.0
|
||||||
|
|
||||||
|
FROM base AS dependencies
|
||||||
|
|
||||||
|
COPY package.json pnpm-lock.yaml ./
|
||||||
|
RUN pnpm install --frozen-lockfile
|
||||||
|
|
||||||
|
FROM dependencies AS migrate
|
||||||
|
|
||||||
|
COPY prisma ./prisma
|
||||||
|
COPY prisma.config.ts ./
|
||||||
|
CMD ["pnpm", "db:deploy"]
|
||||||
|
|
||||||
|
FROM migrate AS build
|
||||||
|
|
||||||
|
COPY nest-cli.json tsconfig.json tsconfig.build.json ./
|
||||||
|
COPY src ./src
|
||||||
|
RUN pnpm build
|
||||||
|
|
||||||
|
FROM base AS production-dependencies
|
||||||
|
|
||||||
|
COPY package.json pnpm-lock.yaml ./
|
||||||
|
RUN pnpm install --prod --frozen-lockfile
|
||||||
|
|
||||||
|
FROM node:24.20.0-bookworm-slim AS runtime
|
||||||
|
|
||||||
|
ENV NODE_ENV=production \
|
||||||
|
APP_HOST=0.0.0.0 \
|
||||||
|
APP_PORT=8080
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
RUN groupadd --system --gid 10001 app \
|
||||||
|
&& useradd --system --uid 10001 --gid app --home-dir /app --shell /usr/sbin/nologin app
|
||||||
|
|
||||||
|
RUN mkdir -p /app/data && chown 10001:10001 /app/data
|
||||||
|
|
||||||
|
COPY --from=production-dependencies --chown=10001:10001 /app/node_modules ./node_modules
|
||||||
|
COPY --from=build --chown=10001:10001 /app/dist ./dist
|
||||||
|
COPY --from=build --chown=10001:10001 /app/package.json ./package.json
|
||||||
|
|
||||||
|
USER 10001:10001
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=15s --retries=3 \
|
||||||
|
CMD node -e "fetch('http://127.0.0.1:' + (process.env.APP_PORT || '8080') + '/health/ready').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
|
||||||
|
|
||||||
|
CMD ["node", "dist/main.js"]
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
# Teaching outline, not a runnable Dockerfile. Implement your own Dockerfile.
|
|
||||||
# 1. Choose a runtime/toolchain version and pin the base image tag/digest.
|
|
||||||
# 2. Copy dependency manifests first; install from the language lockfile.
|
|
||||||
# 3. Copy source and build. For compiled languages use a separate runtime stage.
|
|
||||||
# 4. Run the HTTP process as PID 1; listen on 0.0.0.0:8080.
|
|
||||||
# 5. Add non-root, signals and healthcheck according to the current rubric.
|
|
||||||
# 6. Pass runtime config/secrets at startup, never COPY .env or secret files.
|
|
||||||
# This file deliberately has no FROM/CMD: selecting/implementing them is the task.
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
# Topology worksheet, deliberately NOT a runnable stack (no application supplied).
|
|
||||||
# Implement compose.yaml or stack.yaml; do not submit this empty file as deployment.
|
|
||||||
# Service names below are logical roles, not imposed container names.
|
|
||||||
x-lab-number: 2
|
|
||||||
x-required-roles:
|
|
||||||
- api
|
|
||||||
- postgres
|
|
||||||
# TODO: implement real images/builds, network wiring, volumes, healthchecks,
|
|
||||||
# credentials and bootstrap. Details: contracts/infrastructure.json and README.md.
|
|
||||||
services: {}
|
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
services:
|
||||||
|
backend:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
target: runtime
|
||||||
|
restart: unless-stopped
|
||||||
|
init: true
|
||||||
|
environment:
|
||||||
|
NODE_ENV: production
|
||||||
|
APP_ENV: ${APP_ENV:-lab}
|
||||||
|
APP_HOST: 0.0.0.0
|
||||||
|
APP_PORT: 8080
|
||||||
|
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-local_dev_password}@postgres:5432/${POSTGRES_DB:-chat}
|
||||||
|
CACHE_URL: redis://redis:6379/0
|
||||||
|
CACHE_PREFIX: ${CACHE_PREFIX:-messenger:cache:v1}
|
||||||
|
SESSION_STORE: ${SESSION_STORE:-postgres}
|
||||||
|
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||||
|
APP_DATA_DIR: /app/data
|
||||||
|
ports:
|
||||||
|
- '127.0.0.1:${APP_PORT:-8080}:8080'
|
||||||
|
volumes:
|
||||||
|
- backend_data:/app/data
|
||||||
|
depends_on:
|
||||||
|
migrate:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
redis:
|
||||||
|
condition: service_started
|
||||||
|
healthcheck:
|
||||||
|
test:
|
||||||
|
- CMD
|
||||||
|
- node
|
||||||
|
- -e
|
||||||
|
- "fetch('http://127.0.0.1:8080/health/ready').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
|
||||||
|
interval: 10s
|
||||||
|
timeout: 3s
|
||||||
|
start_period: 20s
|
||||||
|
retries: 5
|
||||||
|
stop_grace_period: 30s
|
||||||
|
|
||||||
|
migrate:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
target: migrate
|
||||||
|
environment:
|
||||||
|
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-local_dev_password}@postgres:5432/${POSTGRES_DB:-chat}
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
|
||||||
|
postgres:
|
||||||
|
image: postgres:17.6-bookworm
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
POSTGRES_DB: ${POSTGRES_DB:-chat}
|
||||||
|
POSTGRES_USER: ${POSTGRES_USER:-app}
|
||||||
|
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-local_dev_password}
|
||||||
|
ports:
|
||||||
|
- '127.0.0.1:${POSTGRES_PORT:-5432}:5432'
|
||||||
|
volumes:
|
||||||
|
- postgres_data:/var/lib/postgresql/data
|
||||||
|
healthcheck:
|
||||||
|
test: ['CMD-SHELL', 'pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"']
|
||||||
|
interval: 5s
|
||||||
|
timeout: 3s
|
||||||
|
start_period: 10s
|
||||||
|
retries: 10
|
||||||
|
stop_grace_period: 30s
|
||||||
|
|
||||||
|
redis:
|
||||||
|
image: redis:7.4.5-alpine
|
||||||
|
restart: unless-stopped
|
||||||
|
command:
|
||||||
|
['redis-server', '--appendonly', 'yes', '--appendfsync', 'everysec']
|
||||||
|
ports:
|
||||||
|
- '127.0.0.1:${REDIS_PORT:-6379}:6379'
|
||||||
|
volumes:
|
||||||
|
- redis_data:/data
|
||||||
|
healthcheck:
|
||||||
|
test: ['CMD', 'redis-cli', 'ping']
|
||||||
|
interval: 5s
|
||||||
|
timeout: 3s
|
||||||
|
retries: 10
|
||||||
|
stop_grace_period: 30s
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres_data:
|
||||||
|
redis_data:
|
||||||
|
backend_data:
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
@echo off
|
||||||
|
setlocal
|
||||||
|
if defined GIT_BASH goto run
|
||||||
|
set "GIT_BASH=%ProgramFiles%\Git\bin\bash.exe"
|
||||||
|
if exist "%GIT_BASH%" goto run
|
||||||
|
echo Git Bash was not found. Set GIT_BASH to the full path to bash.exe. 1>&2
|
||||||
|
exit /b 2
|
||||||
|
:run
|
||||||
|
"%GIT_BASH%" "%~dp0restart.sh"
|
||||||
|
exit /b %ERRORLEVEL%
|
||||||
Executable
+16
@@ -0,0 +1,16 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
project_dir=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||||
|
cd "$project_dir"
|
||||||
|
|
||||||
|
for service in backend postgres redis; do
|
||||||
|
if [ -z "$(docker compose ps --all --quiet "$service")" ]; then
|
||||||
|
printf '%s\n' "Missing container for $service; start the Compose stack first." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
docker compose stop --timeout 10 backend postgres redis
|
||||||
|
docker compose up --detach --no-recreate --no-build --no-deps --wait --wait-timeout 45 postgres redis
|
||||||
|
docker compose up --detach --no-recreate --no-build --no-deps backend
|
||||||
Reference in New Issue
Block a user