Rollback dockerfile

This commit is contained in:
Arthur Belleville 2025-11-15 09:02:36 +01:00
parent d5137719b5
commit 70a560724d
No known key found for this signature in database

View file

@ -3,76 +3,76 @@
# ============================================================================== # ==============================================================================
# NOTE: This Dockerfile should be built from the monorepo root, not from apps/api # NOTE: This Dockerfile should be built from the monorepo root, not from apps/api
# Build command: docker build -f apps/api/Dockerfile -t xtablo-api . # Build command: docker build -f apps/api/Dockerfile -t xtablo-api .
# Following pnpm Example 3 for CI/CD without BuildKit cache mounts FROM node:20-alpine AS base
FROM node:20-slim AS base
# Configure pnpm # Build argument for NODE_ENV
ENV PNPM_HOME="/pnpm" ARG NODE_ENV=production
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
# ============================================================================== # Install security updates and pnpm
# Build stage - Install all dependencies and build RUN apk --no-cache upgrade && \
# ============================================================================== corepack enable && \
FROM base AS build corepack prepare pnpm@latest --activate
# Copy only lockfile first for better layer caching # Create app directory and set up non-root user
COPY pnpm-lock.yaml /app/pnpm-lock.yaml
WORKDIR /app WORKDIR /app
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nodejs -u 1001
# Fetch all dependencies (including devDependencies for build) # Copy workspace configuration and root package files
RUN pnpm fetch COPY pnpm-workspace.yaml package.json pnpm-lock.yaml ./
# Copy source files # Copy packages directory (shared packages)
COPY . /app COPY packages ./packages
# Install all dependencies from fetched cache # Copy api app package.json
RUN pnpm install --frozen-lockfile --offline COPY apps/api/package.json ./apps/api/package.json
# Build the API # ==============================================================================
# Dependencies stage - Install all dependencies
# ==============================================================================
FROM base AS deps
# Install all dependencies (including devDependencies for build)
# This installs dependencies for the entire workspace
RUN pnpm install --frozen-lockfile
# ==============================================================================
# Build stage - Compile TypeScript
# ==============================================================================
FROM deps AS build
# Copy api source code
COPY apps/api ./apps/api
# Build the api application
WORKDIR /app/apps/api WORKDIR /app/apps/api
RUN pnpm run build RUN pnpm run build
# ============================================================================== # ==============================================================================
# Production dependencies stage # Production dependencies stage - Install only production dependencies
# ============================================================================== # ==============================================================================
FROM base AS prod-deps FROM base AS prod-deps
# Copy only lockfile first # Install only production dependencies
COPY pnpm-lock.yaml /app/pnpm-lock.yaml RUN pnpm install --frozen-lockfile --prod && pnpm store prune
WORKDIR /app
# Fetch only production dependencies
RUN pnpm fetch --prod
# Copy workspace files needed for install
COPY pnpm-workspace.yaml package.json /app/
COPY packages /app/packages
COPY apps/api/package.json /app/apps/api/package.json
# Install production dependencies from fetched cache
RUN pnpm install --prod --frozen-lockfile --offline
# ============================================================================== # ==============================================================================
# Final stage - Production image # Final stage - Production image
# ============================================================================== # ==============================================================================
FROM base FROM base AS final
# Create non-root user # Copy built api application
RUN groupadd --gid 1001 nodejs && \ COPY --from=build /app/apps/api/dist ./apps/api/dist
useradd -l --uid 1001 --gid nodejs --shell /bin/bash --create-home nodejs COPY --from=build /app/apps/api/package.json ./apps/api/package.json
# Copy production dependencies from prod-deps stage # Copy shared packages (needed for workspace resolution)
COPY --from=prod-deps /app/node_modules /app/node_modules COPY --from=prod-deps /app/packages ./packages
COPY --from=prod-deps /app/packages /app/packages
# Copy built application from build stage # Install production dependencies
COPY --from=build /app/apps/api/dist /app/apps/api/dist # This ensures proper symlink structure for pnpm
COPY --from=build /app/apps/api/package.json /app/apps/api/package.json RUN pnpm install --frozen-lockfile --prod --filter @xtablo/api...
COPY --from=build /app/pnpm-workspace.yaml /app/pnpm-workspace.yaml
COPY --from=build /app/package.json /app/package.json
# Set working directory # Set working directory to api app
WORKDIR /app/apps/api WORKDIR /app/apps/api
# Change ownership to nodejs user # Change ownership to nodejs user
@ -85,4 +85,5 @@ USER nodejs
EXPOSE 8080 EXPOSE 8080
# NODE_ENV will be set via Cloud Run environment variables # NODE_ENV will be set via Cloud Run environment variables
# Start the application
CMD ["pnpm", "start"] CMD ["pnpm", "start"]