diff --git a/CHANGELOG.md b/CHANGELOG.md index 075e2d5..8d73555 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,10 @@ step 1 (Hugging Face downloader) is released as a minor version. - Release pipeline (`.gitea/workflows/release.yaml`): a `vX.Y.Z` tag runs checks, pushes the Docker image to the Gitea registry (`X.Y.Z`, `X.Y`, `latest`) and creates a release from this changelog. -- `deploy/compose.yaml` for the server, using the released image (`KANAME_VERSION` to pin). +- `deploy/compose.yaml` for the server, using the released image (`KANAME_VERSION` to pin). About + 20 lines: the image now has an entrypoint with roles (`web`/`worker`), runs migrations itself + under a file lock (no start-order dependency between the services) and has a built-in + healthcheck. - The app version is shown next to the app name (`APP_VERSION`, set at image build time). ### Changed diff --git a/Dockerfile b/Dockerfile index 238da3d..a54672a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -# One image for both services: `web` (next start) and `worker` (node dist/worker.mjs). +# One image for both services; the first argument picks the role: `web` (default) or `worker`. FROM node:24-trixie-slim AS node @@ -46,7 +46,10 @@ COPY --from=build --chown=app:app /app/node_modules ./node_modules COPY --from=build --chown=app:app /app/.next ./.next COPY --from=build --chown=app:app /app/dist ./dist COPY --from=build --chown=app:app /app/drizzle ./drizzle +COPY --chmod=755 docker-entrypoint.sh docker-healthcheck.sh /usr/local/bin/ USER app EXPOSE 3000 -CMD ["node_modules/.bin/next", "start"] +HEALTHCHECK --interval=15s --timeout=5s --start-period=30s --retries=3 CMD ["docker-healthcheck.sh"] +ENTRYPOINT ["docker-entrypoint.sh"] +CMD ["web"] diff --git a/README.md b/README.md index f3b46f1..ca79f01 100644 --- a/README.md +++ b/README.md @@ -35,16 +35,20 @@ Pushing a tag `vX.Y.Z` (must equal `version` in `package.json` and have a `## [X Requirements: an `act_runner` registered for the repo/user with Docker access, and a repo secret `REGISTRY_TOKEN` (personal access token with *package: read and write*). -On the server, use `deploy/compose.yaml` (pulls the released image instead of building): +On the server you only need `deploy/compose.yaml` and a filled-in `.env` (from `.env.example`) in +one folder; no source code, no Dockerfile: ```bash -# in a folder with deploy/compose.yaml and a filled-in .env next to it docker compose pull && docker compose up -d ``` Pin a version with `KANAME_VERSION=0.4.0` in `.env`; the running version is shown next to the app name in the header. +The image picks its role from the first argument: `web` (default) or `worker`. Both run pending +DB migrations at startup (serialized with a file lock on `/data`), and the image has a built-in +healthcheck for both roles. + ## Local development ```bash diff --git a/compose.yaml b/compose.yaml index 8b4131f..8b6d920 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,44 +1,28 @@ +# Builds the image from this checkout. For the server use deploy/compose.yaml (released image). name: kaname x-app: &app image: kaname:local build: . env_file: .env - # Container paths override whatever local-dev paths are in .env. + # Container paths win over local-dev paths in .env. environment: DATABASE_URL: file:/data/app.db MODELS_DIR: /models HF_HOME: /data/hf-home restart: unless-stopped - init: true # reaps child processes (hf) and forwards signals services: worker: <<: *app - command: ["node", "dist/worker.mjs"] - volumes: - - data:/data - - models:/models - # Give running downloads time to stop cleanly (they are marked "interrupted"). - stop_grace_period: 20s - healthcheck: - test: ["CMD-SHELL", "find /tmp/kaname-worker-alive -mmin -1 | grep -q ."] - interval: 10s - timeout: 5s - start_period: 30s - retries: 3 + command: worker + volumes: [data:/data, models:/models] web: <<: *app - command: ["node_modules/.bin/next", "start"] - ports: - - "${WEB_PORT:-3000}:3000" - volumes: - - data:/data - depends_on: - worker: - condition: service_healthy # worker applies DB migrations on startup + ports: ["${WEB_PORT:-3000}:3000"] + volumes: [data:/data] volumes: - data: # SQLite + hf home; keep on local disk (SQLite and NFS don't mix) - models: # downloaded models; will become an NFS mount from TrueNAS + data: + models: diff --git a/deploy/compose.yaml b/deploy/compose.yaml index 011d77d..7f74bdc 100644 --- a/deploy/compose.yaml +++ b/deploy/compose.yaml @@ -1,49 +1,20 @@ -# Production compose for the server: runs the released image from the Gitea registry. -# (The compose.yaml in the repo root builds the image locally instead.) -# -# cp ../.env.example .env # fill in, AUTH_URL = public URL +# Server setup: put this file and a filled-in .env (from .env.example) in a folder, then # docker compose pull && docker compose up -d -# -# Pin a version with KANAME_VERSION=0.4.0 in .env (default: latest). - -name: kaname - -x-app: &app - image: git.linxweiler.xyz/pascallinxweiler/kaname:${KANAME_VERSION:-latest} - env_file: .env - environment: - DATABASE_URL: file:/data/app.db - MODELS_DIR: /models - HF_HOME: /data/hf-home - restart: unless-stopped - init: true - services: worker: - <<: *app - command: ["node", "dist/worker.mjs"] - volumes: - - data:/data - - models:/models - stop_grace_period: 20s - healthcheck: - test: ["CMD-SHELL", "find /tmp/kaname-worker-alive -mmin -1 | grep -q ."] - interval: 10s - timeout: 5s - start_period: 30s - retries: 3 + image: git.linxweiler.xyz/pascallinxweiler/kaname:${KANAME_VERSION:-latest} + command: worker + env_file: .env + volumes: [data:/data, models:/models] + restart: unless-stopped web: - <<: *app - command: ["node_modules/.bin/next", "start"] - ports: - - "${WEB_PORT:-3000}:3000" - volumes: - - data:/data - depends_on: - worker: - condition: service_healthy + image: git.linxweiler.xyz/pascallinxweiler/kaname:${KANAME_VERSION:-latest} + env_file: .env + ports: ["3000:3000"] + volumes: [data:/data] + restart: unless-stopped volumes: - data: # SQLite + hf home; keep on local disk (SQLite and NFS don't mix) - models: # downloaded models; will become an NFS mount from TrueNAS + data: # SQLite + hf home: keep on local disk + models: # downloaded models (later: NFS from TrueNAS) diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100644 index 0000000..6c75a01 --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,20 @@ +#!/bin/sh +# Usage: docker run [web|worker|] (default: web) +set -e + +role="${1:-web}" + +case "$role" in + web|worker) + echo "$role" > /tmp/kaname-role + # web and worker usually start together; the lock makes sure only one runs the migrations. + mkdir -p /data + flock /data/.migrate.lock node dist/migrate.mjs + ;; +esac + +case "$role" in + web) exec node_modules/.bin/next start ;; + worker) exec node dist/worker.mjs ;; + *) exec "$@" ;; +esac diff --git a/docker-healthcheck.sh b/docker-healthcheck.sh new file mode 100644 index 0000000..7da7f89 --- /dev/null +++ b/docker-healthcheck.sh @@ -0,0 +1,8 @@ +#!/bin/sh +# Healthcheck for both roles (see docker-entrypoint.sh). +case "$(cat /tmp/kaname-role 2>/dev/null)" in + # The worker touches this file on every heartbeat (every 5s). + worker) find /tmp/kaname-worker-alive -mmin -1 | grep -q . ;; + web) node -e 'fetch("http://127.0.0.1:" + (process.env.PORT || 3000) + "/login").then((r) => process.exit(r.ok ? 0 : 1), () => process.exit(1))' ;; + *) exit 0 ;; +esac diff --git a/package.json b/package.json index 8e745d0..6583934 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "db:generate": "drizzle-kit generate", "db:migrate": "dotenv -e .env -- tsx src/lib/db/migrate-cli.ts", "test": "tsx --test \"src/**/*.test.ts\"", - "build:worker": "esbuild src/worker/index.ts --bundle --platform=node --target=node24 --format=esm --packages=external --outfile=dist/worker.mjs" + "build:worker": "esbuild src/worker/index.ts --bundle --platform=node --target=node24 --format=esm --packages=external --outfile=dist/worker.mjs && esbuild src/lib/db/migrate-cli.ts --bundle --platform=node --target=node24 --format=esm --packages=external --outfile=dist/migrate.mjs" }, "dependencies": { "@base-ui/react": "^1.8.0",