From f544a84f6dd3b3569c7878af0dc295a91d7d02bf Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Wed, 5 Mar 2025 07:48:49 +0100 Subject: [PATCH] build(Docker): simplify Docker alpine build and runtime stage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit same changes as for the "non-alpine" Dockerfile previously commited, but adapted to Alpine. 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" --- Dockerfile.alpine | 51 ++++++++++++++--------------------------------- 1 file changed, 15 insertions(+), 36 deletions(-) diff --git a/Dockerfile.alpine b/Dockerfile.alpine index 2e134ab5e..43eac8ca7 100644 --- a/Dockerfile.alpine +++ b/Dockerfile.alpine @@ -1,37 +1,22 @@ # Build stage FROM node:22.14.0-alpine AS builder -# Configure build dependencies -RUN apk add --no-cache --virtual .build-dependencies \ - autoconf \ - automake \ - g++ \ - gcc \ - libtool \ - make \ - nasm \ - libpng-dev \ - python3 - -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 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-alpine @@ -41,17 +26,11 @@ RUN apk add --no-cache su-exec shadow 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 +COPY --from=builder /usr/src/app ./ + +RUN sed -i "/electron/d" package.json && \ + npm ci --omit=dev && \ + npm cache clean --force # Add application user RUN adduser -s /bin/false node; exit 0