Dockerfile Multi-stage

Small, Secure Image

A multi-stage build compiles in one stage and copies only dist/ and production dependencies into the final image (see the Docker material for the basics).

# ---- deps: install all dependencies (cached while package*.json is unchanged)
FROM node:22-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci

# ---- build: compile and generate the Prisma client, then drop dev dependencies
FROM node:22-alpine AS build
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npx prisma generate && npm run build && npm prune --omit=dev

# ---- runtime: only what is needed to run
FROM node:22-alpine AS runtime
WORKDIR /app
ENV NODE_ENV=production
RUN addgroup -S app && adduser -S app -G app
COPY --from=build --chown=app:app /app/package.json ./
COPY --from=build --chown=app:app /app/node_modules ./node_modules
COPY --from=build --chown=app:app /app/dist ./dist
COPY --from=build --chown=app:app /app/prisma ./prisma
USER app
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://localhost:3000/api/health || exit 1
CMD ["node", "dist/main.js"]

Example

docker build -t shopease-api:1.4.0 .
docker images shopease-api

Example output

[+] Building 48.2s (17/17) FINISHED
REPOSITORY     TAG     IMAGE ID       CREATED          SIZE
shopease-api   1.4.0   9c1d2e3f4a5b   10 seconds ago   212MB