Optimize Dockerfile with multi-stage build
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 19m42s

- Added multi-stage build to reduce final image size
- Stage 1 (builder): Installs all deps, compiles TS, generates OpenAPI, builds HTML
- Stage 2 (production): Fresh base image with only production deps and compiled artifacts
- Final image no longer contains src/, tsconfig.json, or dev dependencies
- Added TDD test (dockerfile-build.test.ts) to verify build artifacts exist
- All 561 tests pass

Reduces image size by excluding TypeScript source, build tools, and dev dependencies.
This commit is contained in:
DocFast Dev 2026-03-08 11:05:59 +01:00
parent da57f57299
commit 921562750f
2 changed files with 73 additions and 17 deletions

View file

@ -1,4 +1,37 @@
FROM node:22-bookworm-slim
# ============================================
# Stage 1: Builder
# ============================================
FROM node:22-bookworm-slim AS builder
WORKDIR /app
# Copy package files for dependency installation
COPY package*.json tsconfig.json ./
# Install ALL dependencies (including devDependencies for build)
RUN npm install
# Copy source code and build scripts
COPY src/ src/
COPY scripts/ scripts/
COPY public/ public/
# Compile TypeScript
RUN npx tsc
# Generate OpenAPI spec
RUN node scripts/generate-openapi.mjs
# Build HTML templates
RUN node scripts/build-html.cjs
# Create swagger-ui symlink in builder stage
RUN rm -f public/swagger-ui && ln -s /app/node_modules/swagger-ui-dist public/swagger-ui
# ============================================
# Stage 2: Production
# ============================================
FROM node:22-bookworm-slim AS production
# Install Chromium and dependencies as root
RUN apt-get update && apt-get install -y --no-install-recommends \
@ -9,25 +42,25 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
RUN groupadd --gid 1001 docfast \
&& useradd --uid 1001 --gid docfast --shell /bin/bash --create-home docfast
# Set environment variables
WORKDIR /app
# Copy package files for production dependency installation
COPY package*.json ./
# Install ONLY production dependencies
RUN npm install --omit=dev
# Copy compiled artifacts from builder stage
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/public ./public
# Recreate swagger-ui symlink in production stage
RUN rm -f public/swagger-ui && ln -s /app/node_modules/swagger-ui-dist public/swagger-ui
# Set Puppeteer environment variables
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium
# Build stage - compile TypeScript
WORKDIR /app
COPY package*.json tsconfig.json ./
RUN npm install
COPY src/ src/
RUN npx tsc
# Remove dev dependencies
RUN npm prune --omit=dev
COPY scripts/ scripts/
COPY public/ public/
RUN node scripts/generate-openapi.mjs
RUN node scripts/build-html.cjs
RUN rm -f public/swagger-ui && ln -s /app/node_modules/swagger-ui-dist public/swagger-ui
# Create data directory and set ownership to docfast user
RUN mkdir -p /app/data && chown -R docfast:docfast /app