From 1dc5f0881383705b417a54830b19802dc9cd5d3c Mon Sep 17 00:00:00 2001 From: Zertonixa Date: Wed, 14 Oct 2026 06:29:12 +0300 Subject: [PATCH] build(docker): add Compose stack with migrations and persistent storage --- .dockerignore | 5 +++ .env.example | 14 ++++--- .gitattributes | 4 ++ .gitignore | 7 ++-- Dockerfile | 54 ++++++++++++++++++++++++++ Dockerfile.example | 8 ---- compose.example.yaml | 10 ----- compose.yaml | 90 ++++++++++++++++++++++++++++++++++++++++++++ scripts/restart.cmd | 10 +++++ scripts/restart.sh | 16 ++++++++ 10 files changed, 190 insertions(+), 28 deletions(-) create mode 100644 .gitattributes create mode 100644 Dockerfile delete mode 100644 Dockerfile.example delete mode 100644 compose.example.yaml create mode 100644 compose.yaml create mode 100644 scripts/restart.cmd create mode 100755 scripts/restart.sh diff --git a/.dockerignore b/.dockerignore index bc16e55..1aab37b 100644 --- a/.dockerignore +++ b/.dockerignore @@ -16,5 +16,10 @@ backups .venv __pycache__ node_modules +src/generated +dist +coverage +*.tsbuildinfo +.pnpm-store evidence .DS_Store diff --git a/.env.example b/.env.example index 714c807..aad7985 100644 --- a/.env.example +++ b/.env.example @@ -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_PORT=8080 APP_ENV=lab SESSION_TTL_SECONDS=3600 -# Choose credentials locally. File paths contain no secret values. -DATABASE_URL_FILE=/run/secrets/database_url -# Optional on 3, required use on 4 in lab2; Redis OR Valkey: +POSTGRES_DB=chat +POSTGRES_USER=app +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 -CACHE_URL_FILE=/run/secrets/cache_url diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..c52d61a --- /dev/null +++ b/.gitattributes @@ -0,0 +1,4 @@ +test/*.sh text eol=lf +test/*.cmd text eol=crlf +scripts/*.sh text eol=lf +scripts/*.cmd text eol=crlf diff --git a/.gitignore b/.gitignore index b5a7189..df37236 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -# Local/private state .env .env.* !.env.example @@ -13,7 +12,6 @@ certs/private/ volumes/ data/ backups/ -# Runtime/build artifacts .venv/ __pycache__/ *.py[cod] @@ -24,9 +22,10 @@ target/ bin/ obj/ coverage/ +*.tsbuildinfo +.pnpm-store/ +src/generated/prisma/ *.log .DS_Store .idea/ .vscode/ -# Keep small, sanitized evidence summaries in evidence/ under version control. -# Inspect git diff --cached: ignore rules are not a secret scanner. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5a5cc6e --- /dev/null +++ b/Dockerfile @@ -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"] diff --git a/Dockerfile.example b/Dockerfile.example deleted file mode 100644 index e333a12..0000000 --- a/Dockerfile.example +++ /dev/null @@ -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. diff --git a/compose.example.yaml b/compose.example.yaml deleted file mode 100644 index 52524b0..0000000 --- a/compose.example.yaml +++ /dev/null @@ -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: {} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..f929162 --- /dev/null +++ b/compose.yaml @@ -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: diff --git a/scripts/restart.cmd b/scripts/restart.cmd new file mode 100644 index 0000000..1bc9b61 --- /dev/null +++ b/scripts/restart.cmd @@ -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% diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 0000000..c706e4b --- /dev/null +++ b/scripts/restart.sh @@ -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