Refactor Dockerfile for separate migration and seed process

Updated Dockerfile to change the startup process for the API. Migrations and seeding now run separately in CI before deployment.
This commit is contained in:
Yonas Tewabe
2026-06-29 10:34:47 +03:00
committed by GitHub
parent 3a0b34d3b6
commit 6e3d2bc0d2

View File

@@ -1,29 +1,25 @@
# syntax=docker/dockerfile:1 # syntax=docker/dockerfile:1
# Build from monorepo root: docker build -f apps/edr-passenger-api/Dockerfile . # Build from monorepo root: docker build -f apps/edr-passenger-api/Dockerfile .
# On start: runs prisma migrate deploy + seed, then the API. # On start: runs the API only — migrations/seed now run separately via the
# `migration` stage, invoked as a one-shot container in CI before deploy.
FROM node:24.15.0-alpine AS base FROM node:24.15.0-alpine AS base
RUN apk add --no-cache libc6-compat RUN apk add --no-cache libc6-compat
RUN corepack enable RUN corepack enable
WORKDIR /app WORKDIR /app
FROM base AS pruner FROM base AS pruner
COPY . . COPY . .
RUN pnpm dlx turbo prune "@edr/passenger-api" --docker RUN pnpm dlx turbo prune "@edr/passenger-api" --docker
FROM base AS installer FROM base AS installer
COPY --from=pruner /app/out/json/ . COPY --from=pruner /app/out/json/ .
COPY --from=pruner /app/out/pnpm-lock.yaml ./pnpm-lock.yaml COPY --from=pruner /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
RUN --mount=type=secret,id=npmrc,target=./.npmrc,required=false \ RUN --mount=type=secret,id=npmrc,target=./.npmrc,required=false \
--mount=type=cache,id=pnpm,target=/pnpm/store \ --mount=type=cache,id=pnpm,target=/pnpm/store \
pnpm install --frozen-lockfile pnpm install --frozen-lockfile
FROM base AS builder FROM base AS builder
COPY --from=installer /app/ . COPY --from=installer /app/ .
COPY --from=pruner /app/out/full/ . COPY --from=pruner /app/out/full/ .
RUN pnpm --filter "@edr/passenger-api" exec prisma generate RUN pnpm --filter "@edr/passenger-api" exec prisma generate
RUN pnpm turbo build --filter="@edr/passenger-api..." RUN pnpm turbo build --filter="@edr/passenger-api..."
FROM base AS deployer FROM base AS deployer
COPY --from=builder /app/ . COPY --from=builder /app/ .
RUN pnpm deploy --filter="@edr/passenger-api" --legacy /deploy RUN pnpm deploy --filter="@edr/passenger-api" --legacy /deploy
@@ -32,20 +28,24 @@ RUN if [ -d node_modules/.prisma ]; then \
cp -r node_modules/.prisma /deploy/node_modules/.prisma; \ cp -r node_modules/.prisma /deploy/node_modules/.prisma; \
fi fi
# --- Migration image: built in CI, run as a one-shot `docker run --rm --env-file ...`
# against the real DB, as its own gated step *before* the app image is built/deployed.
# Runs the same generate + migrate + seed sequence the old entrypoint used to run at
# container start, but now as a throwaway container, before the app container exists.
FROM deployer AS migration
WORKDIR /deploy
RUN corepack enable && corepack prepare pnpm@11.1.1 --activate
ENV CI=true
ENV COREPACK_ENABLE_DOWNLOAD_PROMPT=0
CMD ["sh", "-c", "npm run prisma:generate && npm run prisma:migrate && npm run prisma:seed"]
FROM node:24.15.0-alpine AS runner FROM node:24.15.0-alpine AS runner
RUN apk add --no-cache libc6-compat RUN apk add --no-cache libc6-compat
RUN corepack enable && corepack prepare pnpm@11.1.1 --activate
ENV NODE_ENV=production ENV NODE_ENV=production
WORKDIR /app WORKDIR /app
RUN addgroup --system --gid 1001 nodejs \ RUN addgroup --system --gid 1001 nodejs \
&& adduser --system --uid 1001 --ingroup nodejs nestjs && adduser --system --uid 1001 --ingroup nodejs nestjs
COPY --from=deployer /deploy . COPY --from=deployer --chown=nestjs:nodejs /deploy .
COPY apps/edr-passenger-api/docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh \
&& chown -R nestjs:nodejs /app
USER nestjs USER nestjs
ENV CI=true
ENV COREPACK_ENABLE_DOWNLOAD_PROMPT=0
EXPOSE 4000 EXPOSE 4000
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["node", "dist/main.js"] CMD ["node", "dist/main.js"]