chore: better docker build

This commit is contained in:
2025-11-03 14:15:52 -03:00
parent 966c16e2c2
commit 151cb5088b
3 changed files with 53 additions and 25 deletions

View File

@@ -1,27 +1,37 @@
FROM node:22.12-alpine AS base
FROM base AS build
# set a working dir in the base image so all stages inherit it
WORKDIR /app
ENV PATH=/app/node_modules/.bin:$PATH
# Dependency stage: cache node_modules by only copying package files first
FROM base AS deps
# copy package manifests first so this layer is cached when source files change
COPY package*.json ./
RUN npm install
# if you have a lockfile, copy it too (better reproducibility & cacheability)
COPY package-lock.json ./
# Use npm ci for deterministic installs and faster caching
RUN npm ci --prefer-offline --no-audit --no-fund
# Build stage: only run the build after deps layer is ready
FROM deps AS build
# copy the rest of the source
COPY . .
RUN npm run build:prod
FROM base AS prod
# Production stage: keep the final image small and only include what's necessary
FROM node:22.12-alpine AS prod
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/node_modules ./node_modules
# copy production node_modules from the deps stage (they include prod + dev if needed for build)
COPY --from=deps /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
COPY --from=build /app/server.js ./
COPY --from=build /app/set_env.js ./
COPY --from=build /app/package*.json ./
EXPOSE 5000-7000
# expose single well-known port instead of a wide range
EXPOSE 5000
CMD ["npm", "run", "start:prod"]