build(Docker): simplify Docker build and runtime stage

this Dockerfile is aimed at production builds, i.e. trying to keep size as small as possible at the cost of "rebuild speed", due to missed docker cache opportunities.

Build Stage:
* do the complete build inside docker as oposed to the previous "hybrid", where tsc was run locally and the output got copied into the Docker build stage → you can now build this with Docker, without having to install the whole node/TS env locally

* build into a "build" subfolder, for easier clean up during build stage

* get rid of now unnecessary extra file/asset handling, as this is now handled by `npm run build:prepare-dist`

* no `npm prune` needed here, as we delete the whole build folder anyways in the last build step

Runtime stage:
* move the "electron" dep removal from the builder stage to the runtime stage, before installing the dependencies

* move to `npm ci` for reproducible installations – but only installing runtime deps here

* get rid of now unnecessary copying commands from the builder stage, as everything is now neatly available in "/usr/src/app"
This commit is contained in:
Panagiotis Papadopoulos 2025-03-05 07:07:47 +01:00 committed by Panagiotis Papadopoulos
parent 2973d38db0
commit c68b0b02e4

View File

@ -15,49 +15,40 @@ FROM node:22.14.0-bullseye-slim AS builder
# python3 \ # python3 \
# && rm -rf /var/lib/apt/lists/* # && rm -rf /var/lib/apt/lists/*
WORKDIR /usr/src/app WORKDIR /usr/src/app/build
# Copy only necessary files for build # Copy only necessary files for build
COPY . . COPY . .
# Build and cleanup in a single layer # Build and cleanup in a single layer
RUN sed -i "/electron/d" package.json && \ RUN npm ci && \
cp -R build/src/* src/. && \ npm run build:prepare-dist && \
cp build/docker_healthcheck.js . && \
rm docker_healthcheck.ts && \
npm install && \
npm run build:webpack && \
npm prune --omit=dev && \
npm cache clean --force && \ npm cache clean --force && \
cp -r src/public/app/doc_notes src/public/app-dist/. && \ mv dist/* \
rm -rf src/public/app/* && \ start-docker.sh \
mkdir -p src/public/app/services && \ package-lock.json \
cp -r build/src/public/app/services/mime_type_definitions.js src/public/app/services/mime_type_definitions.js && \ /usr/src/app/ && \
rm src/services/asset_path.ts && \ rm -rf /usr/src/app/build
rm -r build
#TODO: move package-lock copying into copy-dist
# Runtime stage # Runtime stage
FROM node:22.14.0-bullseye-slim FROM node:22.14.0-bullseye-slim
# Install only runtime dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
gosu \
&& rm -rf /var/lib/apt/lists/* && \
rm -rf /var/cache/apt/*
WORKDIR /usr/src/app WORKDIR /usr/src/app
# Copy only necessary files from builder # Install only runtime dependencies
COPY --from=builder /usr/src/app/node_modules ./node_modules RUN apt-get update && \
COPY --from=builder /usr/src/app/src ./src apt-get install -y --no-install-recommends \
COPY --from=builder /usr/src/app/db ./db gosu && \
COPY --from=builder /usr/src/app/docker_healthcheck.js . rm -rf /var/lib/apt/lists/* && \
COPY --from=builder /usr/src/app/start-docker.sh . rm -rf /var/cache/apt/*
COPY --from=builder /usr/src/app/package.json .
COPY --from=builder /usr/src/app/config-sample.ini . COPY --from=builder /usr/src/app ./
COPY --from=builder /usr/src/app/images ./images
COPY --from=builder /usr/src/app/translations ./translations RUN sed -i "/electron/d" package.json && \
COPY --from=builder /usr/src/app/libraries ./libraries npm ci --omit=dev && \
npm cache clean --force
# Configure container # Configure container
EXPOSE 8080 EXPOSE 8080