feat(docker): Multi stage docker now fetch deps once

Split dockerfile into three different dockerfiles such that dependencies
are only downloaded once

This allows the build to be a bit faster, since all deps are downloaded
once at the start.

This also makes it so the frontend container no longer needs to be ran,
as its files are directly embedded into the nginx container

This also remove the extra files, since bind mounts do work
it also remove the entrypoint.sh file, as you should prefer to not use
it
This commit is contained in:
Maieul BOYER 2025-12-30 17:28:06 +01:00 committed by Maix0
parent af13395f2f
commit 2ed524872b
17 changed files with 93 additions and 128 deletions

View file

@ -1,33 +1,25 @@
FROM node:22-alpine AS pnpm_base
RUN npm install --global pnpm@10 --no-fund -q;
RUN apk add make python3 gcc clang build-base musl-dev;
RUN apk add --no-cache curl
# lets include the pnpm_deps as an named image (raw_deps)
FROM pnpm_deps AS raw_deps
FROM pnpm_base AS deps
WORKDIR /build
ARG SERVICE
COPY package.json pnpm-workspace.yaml pnpm-lock.yaml /build/
COPY @shared/package.json /build/@shared/
COPY ${SERVICE}/package.json /build/${SERVICE}/
RUN pnpm install --frozen-lockfile;
FROM pnpm_base AS builder
ARG SERVICE
# lets make a `raw_builder` as an image -> this only include the deps and the metadata files
FROM pnpm_base AS raw_builder
WORKDIR /build
COPY --from=raw_deps /build/node_modules /build/node_modules
ARG SERVICE
COPY package.json /build/
COPY @shared/package.json /build/@shared/
COPY ${SERVICE}/ /build/${SERVICE}
COPY ${SERVICE}/package.json /build/${SERVICE}/
COPY tsconfig.base.json pnpm-workspace.yaml pnpm-lock.yaml /build/
COPY ${SERVICE}/entrypoint.sh /build/
COPY --from=deps /build/node_modules /build/node_modules
COPY @shared/ /build/@shared/
COPY ${SERVICE}/ /build/${SERVICE}/
# lets actually build our stuff
FROM raw_builder AS builder
WORKDIR /build
COPY @shared/ /build/@shared/
COPY ${SERVICE}/ /build/${SERVICE}/
RUN cd /build/${SERVICE} && \
pnpm run build:prod && \
@ -35,24 +27,16 @@ RUN cd /build/${SERVICE} && \
cp /build/pnpm-workspace.yaml /dist/pnpm-workspace.yaml && \
cp /build/pnpm-lock.yaml /dist/pnpm-lock.yaml && \
cp /build/@shared/package.json /dist/@shared/ && \
cp /build/${SERVICE}/package.json /dist/${SERVICE}/ && \
cp /build/entrypoint.sh /dist/ && \
chmod +x /dist/entrypoint.sh;
cp /build/${SERVICE}/package.json /dist/${SERVICE}/;
# this is our actual running container :D
FROM pnpm_base
WORKDIR /src
ARG EXTRA_FILES=empty
COPY --from=builder /dist /src
COPY --from=deps /build/node_modules /src/node_modules
COPY ${EXTRA_FILES} /extra
ENTRYPOINT [ "/src/entrypoint.sh" ]
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f -s http://localhost/monitoring?docker || exit 1
COPY --from=builder /dist /src
COPY --from=raw_builder /build/node_modules /src/node_modules
CMD ["node", "/src/run.cjs"]
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f -s http://localhost/monitoring?docker || exit 1