build(Docker): simplify Docker alpine build and runtime stage

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"
This commit is contained in:
Panagiotis Papadopoulos 2025-03-05 07:48:49 +01:00 committed by Panagiotis Papadopoulos
parent 68875683af
commit f544a84f6d

View File

@ -1,37 +1,22 @@
# Build stage # Build stage
FROM node:22.14.0-alpine AS builder FROM node:22.14.0-alpine AS builder
# Configure build dependencies WORKDIR /usr/src/app/build
RUN apk add --no-cache --virtual .build-dependencies \
autoconf \
automake \
g++ \
gcc \
libtool \
make \
nasm \
libpng-dev \
python3
WORKDIR /usr/src/app
# 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 build/docker_healthcheck.js . && \ npm run build:prepare-dist && \
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-alpine FROM node:22.14.0-alpine
@ -41,17 +26,11 @@ RUN apk add --no-cache su-exec shadow
WORKDIR /usr/src/app WORKDIR /usr/src/app
# Copy only necessary files from builder COPY --from=builder /usr/src/app ./
COPY --from=builder /usr/src/app/node_modules ./node_modules
COPY --from=builder /usr/src/app/src ./src RUN sed -i "/electron/d" package.json && \
COPY --from=builder /usr/src/app/db ./db npm ci --omit=dev && \
COPY --from=builder /usr/src/app/docker_healthcheck.js . npm cache clean --force
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
# Add application user # Add application user
RUN adduser -s /bin/false node; exit 0 RUN adduser -s /bin/false node; exit 0