xpeditis2.0/apps/backend/Dockerfile
David 450f1ffc18
Some checks are pending
CD Preprod / Deploy to Preprod (push) Blocked by required conditions
CD Preprod / Notify Success (push) Blocked by required conditions
CD Preprod / Notify Failure (push) Blocked by required conditions
CD Preprod / Security gate (push) Successful in 29s
CD Preprod / Backend — Lint (push) Successful in 1m3s
CD Preprod / Frontend — Lint & Type-check (push) Successful in 1m9s
CD Preprod / Backend — Unit Tests (push) Successful in 1m5s
CD Preprod / Frontend — Unit Tests (push) Successful in 43s
CD Preprod / Backend — Integration Tests (push) Successful in 46s
CD Preprod / Build Frontend (push) Successful in 35s
CD Preprod / Build Backend (push) Successful in 1m3s
CD Preprod / Build Log Exporter (push) Successful in 34s
CD Preprod / Image security (${{ matrix.service }}, ${{ matrix.arch }}) (amd64, backend) (push) Successful in 25s
CD Preprod / Image security (${{ matrix.service }}, ${{ matrix.arch }}) (amd64, frontend) (push) Successful in 25s
CD Preprod / Image security (${{ matrix.service }}, ${{ matrix.arch }}) (amd64, log-exporter) (push) Successful in 21s
CD Preprod / Image security (${{ matrix.service }}, ${{ matrix.arch }}) (arm64, backend) (push) Successful in 24s
CD Preprod / Image security (${{ matrix.service }}, ${{ matrix.arch }}) (arm64, frontend) (push) Successful in 23s
CD Preprod / Image security (${{ matrix.service }}, ${{ matrix.arch }}) (arm64, log-exporter) (push) Successful in 22s
fix
2026-09-25 16:59:38 +02:00

96 lines
2.7 KiB
Docker

# ===============================================
# Stage 1: Dependencies Installation
# ===============================================
FROM node:22-alpine AS dependencies
# Install build dependencies
RUN apk add --no-cache python3 make g++ libc6-compat
# Set working directory
WORKDIR /app
# Copy package files
COPY package*.json ./
COPY tsconfig*.json ./
# Install all dependencies (including dev for build)
RUN npm ci --legacy-peer-deps
# ===============================================
# Stage 2: Build Application
# ===============================================
FROM node:22-alpine AS builder
WORKDIR /app
# Copy dependencies from previous stage
COPY --from=dependencies /app/node_modules ./node_modules
# Copy source code
COPY . .
# Build the application
RUN npm run build
# Remove dev dependencies to reduce size
RUN npm prune --production --legacy-peer-deps
# ===============================================
# Stage 3: Production Image
# ===============================================
FROM node:22-alpine AS production
# Runtime starts with node; remove the base image package manager and its dependencies.
# npm remains available in the dependency and build stages.
RUN npm uninstall --global npm
# Install dumb-init for proper signal handling
RUN apk add --no-cache dumb-init
# Create non-root user
RUN addgroup -g 1001 -S nodejs && \
adduser -S nestjs -u 1001
# Set working directory
WORKDIR /app
# Copy built application from builder
COPY --from=builder --chown=nestjs:nodejs /app/dist ./dist
COPY --from=builder --chown=nestjs:nodejs /app/node_modules ./node_modules
COPY --from=builder --chown=nestjs:nodejs /app/package*.json ./
# Copy source code needed at runtime (for CSV storage path resolution)
COPY --from=builder --chown=nestjs:nodejs /app/src ./src
# Copy startup script (includes migrations)
COPY --chown=nestjs:nodejs scripts/setup/startup.js ./startup.js
# Recovery command: set an admin password without SMTP
# docker exec -it <backend-container> node admin-password.js <email>
COPY --chown=nestjs:nodejs scripts/setup/admin-password.js ./admin-password.js
# Create logs and uploads directories
RUN mkdir -p /app/logs && \
mkdir -p /app/src/infrastructure/storage/csv-storage/rates && \
chown -R nestjs:nodejs /app/logs /app/src
# Switch to non-root user
USER nestjs
# Expose port
EXPOSE 4000
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
CMD node -e "require('http').get('http://localhost:4000/api/v1/health', (r) => process.exit(r.statusCode === 200 ? 0 : 1))"
# Set environment variables
ENV NODE_ENV=production \
PORT=4000
# Use dumb-init to handle signals properly
ENTRYPOINT ["dumb-init", "--"]
# Start the application with migrations
CMD ["node", "startup.js"]