xtablo-source/apps/api/Dockerfile
Arthur Belleville 85f5bcbc15
Fix docker
2025-11-14 23:10:04 +01:00

71 lines
No EOL
2.4 KiB
Docker

# ==============================================================================
# Base stage - Common dependencies and setup
# ==============================================================================
# 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 .
FROM node:20-slim AS base
# Configure pnpm
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
# Copy all files to working directory
COPY . /app
WORKDIR /app
# ==============================================================================
# Production dependencies stage - Install only production dependencies
# ==============================================================================
FROM base AS prod-deps
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile
# ==============================================================================
# Build stage - Install all dependencies and build
# ==============================================================================
FROM base AS build
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
# Build only the API and its dependencies
WORKDIR /app/apps/api
RUN pnpm run build
# ==============================================================================
# Final stage - Production image
# ==============================================================================
FROM node:20-slim
# Configure pnpm
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
# Create non-root user
RUN groupadd --gid 1001 nodejs && \
useradd -l --uid 1001 --gid nodejs --shell /bin/bash --create-home nodejs
# Copy production dependencies from prod-deps stage
COPY --from=prod-deps /app/node_modules /app/node_modules
COPY --from=prod-deps /app/packages /app/packages
# Copy built application from build stage
COPY --from=build /app/apps/api/dist /app/apps/api/dist
COPY --from=build /app/apps/api/package.json /app/apps/api/package.json
# Copy workspace files for proper resolution
COPY --from=build /app/pnpm-workspace.yaml /app/pnpm-workspace.yaml
COPY --from=build /app/package.json /app/package.json
# Set working directory
WORKDIR /app/apps/api
# Change ownership to nodejs user
RUN chown -R nodejs:nodejs /app
# Switch to non-root user
USER nodejs
# Expose port
EXPOSE 8080
# NODE_ENV will be set via Cloud Run environment variables
CMD ["pnpm", "start"]