docfast/Dockerfile
DocFast Dev 921562750f
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 19m42s
Optimize Dockerfile with multi-stage build
- 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.
2026-03-08 11:05:59 +01:00

72 lines
1.9 KiB
Docker

# ============================================
# 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 \
chromium fonts-liberation \
&& rm -rf /var/lib/apt/lists/*
# Create non-root user
RUN groupadd --gid 1001 docfast \
&& useradd --uid 1001 --gid docfast --shell /bin/bash --create-home docfast
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
# Create data directory and set ownership to docfast user
RUN mkdir -p /app/data && chown -R docfast:docfast /app
# Switch to non-root user
USER docfast
ENV PORT=3100
EXPOSE 3100
CMD ["node", "dist/index.js"]