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 \
# && rm -rf /var/lib/apt/lists/*
WORKDIR /usr/src/app
WORKDIR /usr/src/app/build
# Copy only necessary files for build
COPY . .
# Build and cleanup in a single layer
RUN sed -i "/electron/d" package.json && \
cp -R build/src/* src/. && \
cp build/docker_healthcheck.js . && \
rm docker_healthcheck.ts && \
npm install && \
npm run build:webpack && \
npm prune --omit=dev && \
RUN npm ci && \
npm run build:prepare-dist && \
npm cache clean --force && \
cp -r src/public/app/doc_notes src/public/app-dist/. && \
rm -rf src/public/app/* && \
mkdir -p src/public/app/services && \
cp -r build/src/public/app/services/mime_type_definitions.js src/public/app/services/mime_type_definitions.js && \
rm src/services/asset_path.ts && \
rm -r build
mv dist/* \
start-docker.sh \
package-lock.json \
/usr/src/app/ && \
rm -rf /usr/src/app/build
#TODO: move package-lock copying into copy-dist
# Runtime stage
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
# Copy only necessary files from builder
COPY --from=builder /usr/src/app/node_modules ./node_modules
COPY --from=builder /usr/src/app/src ./src
COPY --from=builder /usr/src/app/db ./db
COPY --from=builder /usr/src/app/docker_healthcheck.js .
COPY --from=builder /usr/src/app/start-docker.sh .
COPY --from=builder /usr/src/app/package.json .
COPY --from=builder /usr/src/app/config-sample.ini .
COPY --from=builder /usr/src/app/images ./images
COPY --from=builder /usr/src/app/translations ./translations
COPY --from=builder /usr/src/app/libraries ./libraries
# 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/*
COPY --from=builder /usr/src/app ./
RUN sed -i "/electron/d" package.json && \
npm ci --omit=dev && \
npm cache clean --force
# Configure container
EXPOSE 8080