Files
OSIT-AE-App-Svelte/Dockerfile
Scott Idem 5e0cc73084 perf: add BuildKit npm cache mounts, add compose:down script
- RUN --mount=type=cache,target=/root/.npm for both npm install steps
  Persists npm cache across builds so unchanged deps aren't re-downloaded
- deploy:staging now targets ae_app only (skip api/nginx rebuild)
- compose:down includes --profile database to remove mariadb/pma containers
  and cleanly release ae_dev_net (fixes 'Resource is still in use' warning)
2026-03-10 16:56:41 -04:00

55 lines
1.9 KiB
Docker

# Stage 1: Build the application
# Using node:21-alpine for a lightweight, secure build environment.
FROM node:21-alpine AS builder
WORKDIR /app
# Install dependencies first for better Docker layer caching.
# This step only reruns if package.json or package-lock.json changes.
COPY package*.json ./
RUN --mount=type=cache,target=/root/.npm npm install
# Copy the rest of the source code.
COPY . .
# Build Argument to determine build environment (staging, prod, or production).
# Defaults to "staging".
ARG BUILD_MODE=staging
ENV NODE_ENV=production
# Sync the SvelteKit project to generate ./.svelte-kit/tsconfig.json
# and other required files, which avoids the build warning.
RUN npx svelte-kit sync
# Perform the build based on the BUILD_MODE argument.
# This runs the existing scripts in package.json, which already
# handle copying the correct .env file to .env.production for Vite.
RUN if [ "$BUILD_MODE" = "prod" ] || [ "$BUILD_MODE" = "production" ]; then \
npm run build:prod; \
else \
npm run build:staging; \
fi
# Stage 2: Final runtime image
FROM node:21-alpine AS deploy-node
WORKDIR /app
# Copy only the built files and necessary scripts from the builder stage.
COPY --from=builder /app/build .
COPY --from=builder /app/package.json .
# Install only production dependencies for a smaller, cleaner image.
RUN --mount=type=cache,target=/root/.npm npm install --omit=dev
# Copy the resulting .env.production file to .env.
# adapter-node reads from .env at runtime for non-PUBLIC_ variables.
COPY --from=builder /app/.env.production .env
# SvelteKit (via adapter-node) defaults to port 3000.
EXPOSE 3000
# Healthcheck to verify the app is running
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD node -e "fetch('http://localhost:3000/health').then(r => r.ok ? process.exit(0) : process.exit(1)).catch(() => process.exit(1))"
CMD ["node", "index.js"]