xtablo-source/apps/api/Dockerfile

130 lines
4 KiB
Text
Raw 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-07-13 16:42:15 +00:00
FROM node:18-alpine AS base
# 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
# ==============================================================================
# Staging stage - For staging environment
# ==============================================================================
FROM base AS staging
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
# Note: Environment variables should be injected at runtime via docker run -e or docker-compose
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-07-13 20:44:00 +00:00
# Set staging environment
ENV NODE_ENV=staging
2025-07-13 16:42:15 +00:00
# Start the application
2025-11-13 20:59:53 +00:00
CMD ["pnpm", "start"]
2025-07-13 16:42:15 +00:00
# ==============================================================================
# Production stage - For production environment (default)
# ==============================================================================
FROM base AS production
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-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
2025-11-13 20:59:53 +00:00
# Note: Environment variables should be injected at runtime via docker run -e or docker-compose
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
# Set production environment
ENV NODE_ENV=production
# Start the application
2025-11-13 20:59:53 +00:00
# no-dd-sa:docker-best-practices/multiple-cmd
CMD ["pnpm", "start"]