diff --git a/README.md b/README.md index df0c99a..768330f 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,18 @@ A cloud instance can't run a simulator. Use it for type-checking and linting, or [`packages/backend/railway.toml`](./packages/backend/railway.toml) builds with Nixpacks (`npm run build`) and starts `node dist/main`. Set the root directory to `packages/backend`, add the env vars above in Railway, and run `npx prisma migrate deploy` before or as part of each release. Railway's health check calls `GET /health`, which also checks the database. +### Running the backend as a container + +[`packages/backend/Dockerfile`](./packages/backend/Dockerfile) builds the same API as an image for any host that runs containers (Railway itself still uses Nixpacks): + +```bash +cd packages/backend +docker build -t grit-backend . +docker run --rm -p 3000:3000 --env-file .env grit-backend +``` + +The image runs `node dist/main` as a non-root user, includes `data/` (the exercise library is read from disk), and never contains `.env`. Migrations are not run on start; run `npx prisma migrate deploy` as a separate release step. + ### CI [`.github/workflows/ci.yml`](./.github/workflows/ci.yml) runs on every pull request and push to `main`: the backend job applies the migrations to a throwaway Postgres, checks they match the schema, and runs prettier, eslint, tsc, unit tests, database tests and the build; the mobile job runs tsc, eslint and unit tests; the food-data job runs the importers' lint, typecheck, unit tests and database tests. diff --git a/packages/backend/.dockerignore b/packages/backend/.dockerignore new file mode 100644 index 0000000..7083b8e --- /dev/null +++ b/packages/backend/.dockerignore @@ -0,0 +1,9 @@ +node_modules +dist +coverage +test +.env +.env.* +*.log +Dockerfile +.dockerignore diff --git a/packages/backend/Dockerfile b/packages/backend/Dockerfile new file mode 100644 index 0000000..cd941aa --- /dev/null +++ b/packages/backend/Dockerfile @@ -0,0 +1,49 @@ +# Build context: packages/backend (the same root directory Railway uses). +# Railway still builds with Nixpacks; this image is for running the API +# anywhere else that runs containers. + +# ---- build ---- +FROM node:22-slim AS build +WORKDIR /app + +# Prisma's query engine needs OpenSSL, which the slim image doesn't ship. +RUN apt-get update -y \ + && apt-get install -y --no-install-recommends openssl \ + && rm -rf /var/lib/apt/lists/* + +# Install first so this layer is cached until package.json changes. +# There is no lockfile in the repo, so versions resolve fresh on each build. +COPY package.json ./ +COPY prisma ./prisma +RUN npm install --no-audit --no-fund + +COPY . . +# The build script runs prisma generate before nest build. +RUN npm run build \ + && test -f dist/main.js \ + && npm prune --omit=dev + +# ---- runtime ---- +FROM node:22-slim +WORKDIR /app + +RUN apt-get update -y \ + && apt-get install -y --no-install-recommends openssl \ + && rm -rf /var/lib/apt/lists/* + +ENV NODE_ENV=production +ENV PORT=3000 + +COPY --from=build --chown=node:node /app/package.json ./ +COPY --from=build --chown=node:node /app/node_modules ./node_modules +COPY --from=build --chown=node:node /app/prisma ./prisma +COPY --from=build --chown=node:node /app/dist ./dist +# Read at runtime by the exercises module (data/exercises.json). +COPY --from=build --chown=node:node /app/data ./data + +USER node +EXPOSE 3000 + +# Run node directly (not via npm) so SIGTERM reaches the app and its +# shutdown hooks run. +CMD ["node", "dist/main"]