Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions packages/backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules
dist
coverage
test
.env
.env.*
*.log
Dockerfile
.dockerignore
49 changes: 49 additions & 0 deletions packages/backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
Loading