xtablo-source/apps/api/Dockerfile

89 lines
2.9 KiB
Text
Raw Permalink Normal View History

2025-07-13 16:42:15 +00:00
# ==============================================================================
# Base stage - Common dependencies and setup
# ==============================================================================
2025-11-13 20:59:53 +00:00
# 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 .
2025-11-14 08:04:08 +00:00
FROM node:20-alpine AS base
2025-07-13 16:42:15 +00:00
# Build argument for NODE_ENV
ARG NODE_ENV=production
2025-11-13 20:59:53 +00:00
# Install security updates and pnpm
RUN apk --no-cache upgrade && \
corepack enable && \
corepack prepare pnpm@latest --activate
2025-07-13 16:42:15 +00:00
# Create app directory and set up non-root user
WORKDIR /app
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nodejs -u 1001
2025-11-13 20:59:53 +00:00
# Copy workspace configuration and root package files
COPY pnpm-workspace.yaml package.json pnpm-lock.yaml ./
# Copy packages directory (shared packages)
COPY packages ./packages
# Copy api app package.json
COPY apps/api/package.json ./apps/api/package.json
2025-07-13 16:42:15 +00:00
# ==============================================================================
# Dependencies stage - Install all dependencies
# ==============================================================================
FROM base AS deps
# Install all dependencies (including devDependencies for build)
2025-11-13 20:59:53 +00:00
# This installs dependencies for the entire workspace
RUN pnpm install --frozen-lockfile
2025-07-13 16:42:15 +00:00
# ==============================================================================
# Build stage - Compile TypeScript
# ==============================================================================
FROM deps AS build
2025-11-13 20:59:53 +00:00
# Copy api source code
COPY apps/api ./apps/api
2025-07-13 16:42:15 +00:00
2025-11-13 20:59:53 +00:00
# Build the api application
WORKDIR /app/apps/api
RUN pnpm run build
2025-07-13 16:42:15 +00:00
# ==============================================================================
# Production dependencies stage - Install only production dependencies
# ==============================================================================
FROM base AS prod-deps
# Install only production dependencies
2025-11-13 20:59:53 +00:00
RUN pnpm install --frozen-lockfile --prod && pnpm store prune
2025-07-13 16:42:15 +00:00
# ==============================================================================
2025-11-14 08:15:40 +00:00
# Final stage - Production image
2025-07-13 16:42:15 +00:00
# ==============================================================================
2025-11-14 08:15:40 +00:00
FROM base AS final
2025-07-13 16:42:15 +00:00
2025-11-13 20:59:53 +00:00
# Copy built api application
COPY --from=build /app/apps/api/dist ./apps/api/dist
COPY --from=build /app/apps/api/package.json ./apps/api/package.json
2025-07-13 16:42:15 +00:00
2025-11-13 21:29:47 +00:00
# Copy shared packages (needed for workspace resolution)
COPY --from=prod-deps /app/packages ./packages
# Install production dependencies
# This ensures proper symlink structure for pnpm
RUN pnpm install --frozen-lockfile --prod --filter @xtablo/api...
2025-11-13 20:59:53 +00:00
# Set working directory to api app
WORKDIR /app/apps/api
2025-07-13 16:42:15 +00:00
# Change ownership to nodejs user
RUN chown -R nodejs:nodejs /app
# Switch to non-root user
USER nodejs
# Expose port
EXPOSE 8080
2025-11-14 08:15:40 +00:00
# NODE_ENV will be set via Cloud Run environment variables
2025-07-13 16:42:15 +00:00
# Start the application
2025-11-13 20:59:53 +00:00
CMD ["pnpm", "start"]