Assist_Design/apps/bff/Dockerfile
barsa bc5c7c9bd4 Update package dependencies, enhance Dockerfiles, and improve workspace configuration
- Added glob dependency to package.json and pnpm-lock.yaml for better file handling.
- Updated pnpm-workspace.yaml to include additional built dependencies for improved management.
- Refactored Dockerfiles for BFF and Portal to enhance security and optimize build processes.
- Improved entrypoint scripts to include better logging and readiness checks for services.
- Cleaned up TypeScript configuration files for consistency and alignment with project standards.
2025-12-10 16:31:18 +09:00

122 lines
4.5 KiB
Docker

# syntax=docker/dockerfile:1
# =============================================================================
# BFF (NestJS) Dockerfile
# =============================================================================
# Multi-stage build with BuildKit cache mounts for fast rebuilds
# Optimized for minimal image size, security, and fast startup
# =============================================================================
ARG NODE_VERSION=22
ARG PNPM_VERSION=10.25.0
ARG PRISMA_VERSION=7.1.0
# =============================================================================
# Stage 1: Dependencies (cached layer)
# =============================================================================
FROM node:${NODE_VERSION}-alpine AS deps
ARG PNPM_VERSION
# Install build dependencies in single layer
RUN apk add --no-cache python3 make g++ openssl libc6-compat \
&& corepack enable \
&& corepack prepare pnpm@${PNPM_VERSION} --activate
WORKDIR /app
# Copy manifests first for dependency caching
COPY .npmrc pnpm-workspace.yaml package.json pnpm-lock.yaml ./
COPY packages/domain/package.json ./packages/domain/
COPY apps/bff/package.json ./apps/bff/
# Install all dependencies with cache mount (separate layer for better caching)
ENV HUSKY=0
RUN --mount=type=cache,id=pnpm-bff,target=/root/.local/share/pnpm/store \
pnpm install --frozen-lockfile
# =============================================================================
# Stage 2: Builder
# =============================================================================
FROM deps AS builder
ARG PRISMA_VERSION
# Copy source files
COPY tsconfig.json tsconfig.base.json ./
COPY packages/domain/ ./packages/domain/
COPY apps/bff/ ./apps/bff/
# Build: domain → Prisma generate → BFF (single RUN for better layer efficiency)
RUN pnpm --filter @customer-portal/domain build \
&& pnpm --filter @customer-portal/bff exec prisma generate \
&& pnpm --filter @customer-portal/bff build
# Create production deployment bundle with pnpm deploy
RUN pnpm deploy --filter @customer-portal/bff --prod /app/deploy \
&& cp -r apps/bff/dist deploy/dist \
&& cp -r apps/bff/prisma deploy/prisma \
&& cp -r packages/domain/dist deploy/node_modules/@customer-portal/domain/dist
# =============================================================================
# Stage 3: Production
# =============================================================================
FROM node:${NODE_VERSION}-alpine AS production
ARG PRISMA_VERSION
LABEL org.opencontainers.image.title="Customer Portal BFF" \
org.opencontainers.image.description="NestJS Backend-for-Frontend API" \
org.opencontainers.image.vendor="Customer Portal"
# Install runtime dependencies only + security hardening
RUN apk add --no-cache dumb-init libc6-compat netcat-openbsd \
&& addgroup --system --gid 1001 nodejs \
&& adduser --system --uid 1001 nestjs \
# Remove apk cache and unnecessary files
&& rm -rf /var/cache/apk/* /tmp/* /root/.npm
WORKDIR /app
# Set Prisma schema path before copying files
ENV PRISMA_SCHEMA_PATH=/app/prisma/schema.prisma
# Copy deploy bundle with correct ownership in single layer
COPY --from=builder --chown=nestjs:nodejs /app/deploy ./
# Regenerate Prisma client for production paths and cleanup
RUN rm -rf node_modules/.prisma \
&& npx --yes prisma@${PRISMA_VERSION} generate --schema=${PRISMA_SCHEMA_PATH} \
# Create symlink for backward compatibility with any hardcoded paths
&& mkdir -p /app/apps/bff/prisma \
&& ln -sf /app/prisma/schema.prisma /app/apps/bff/prisma/schema.prisma \
# Fix ownership
&& chown -R nestjs:nodejs /app/node_modules/.prisma /app/apps/bff/prisma \
# Cleanup npm cache and temp files
&& rm -rf /root/.npm /tmp/* /root/.cache
# Copy entrypoint and setup directories
COPY --chown=nestjs:nodejs apps/bff/scripts/docker-entrypoint.sh ./docker-entrypoint.sh
RUN chmod +x docker-entrypoint.sh \
&& mkdir -p secrets logs \
&& chown nestjs:nodejs secrets logs
# Security: Run as non-root user
USER nestjs
# Expose BFF port
EXPOSE 4000
# Environment configuration
ENV NODE_ENV=production \
PORT=4000 \
PRISMA_VERSION=${PRISMA_VERSION} \
# Node.js production optimizations
NODE_OPTIONS="--max-old-space-size=512"
# Health check for container orchestration
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD node -e "fetch('http://localhost:4000/health').then(r=>r.ok||process.exit(1)).catch(()=>process.exit(1))"
ENTRYPOINT ["dumb-init", "--", "./docker-entrypoint.sh"]
CMD ["node", "dist/main.js"]