22# Multi-stage build for optimal image size and security
33
44# ============================================================================
5- # BUILD STAGE
5+ # DEPENDENCIES STAGE - Install all dependencies
66# ============================================================================
7- FROM node:22-alpine AS builder
8-
9- # Install build dependencies
10- RUN apk add --no-cache \
11- python3 \
12- make \
13- g++ \
14- git
7+ FROM node:22-alpine AS dependencies
158
169# Enable Corepack for Yarn 4
1710RUN corepack enable
1811
1912# Set working directory
2013WORKDIR /app
2114
22- # Copy package management files
15+ # Copy only package files for better caching
2316COPY package.json yarn.lock .yarnrc.yml ./
24- COPY .yarn ./.yarn
17+ # Copy .yarn directory if it exists (Yarn 4 cache)
18+ COPY .yarn/ ./.yarn/
2519
26- # Install dependencies with cache mount
27- RUN --mount=type=cache,target=/app/.yarn/cache \
20+ # Install ALL dependencies (including dev) for building
21+ RUN --mount=type=cache,target=/root/.yarn/berry/cache \
22+ --mount=type=cache,target=/app/.yarn/cache \
2823 yarn install --immutable
2924
30- # Copy source code
31- COPY . .
25+ # ============================================================================
26+ # BUILD STAGE - Build the application
27+ # ============================================================================
28+ FROM node:22-alpine AS builder
29+
30+ # Enable Corepack for Yarn 4
31+ RUN corepack enable
32+
33+ # Set working directory
34+ WORKDIR /app
35+
36+ # Copy dependencies from previous stage
37+ COPY --from=dependencies /app/node_modules ./node_modules
38+ COPY --from=dependencies /app/.yarn ./.yarn
39+ COPY --from=dependencies /app/package.json ./package.json
40+ COPY --from=dependencies /app/yarn.lock ./yarn.lock
41+ COPY --from=dependencies /app/.yarnrc.yml ./.yarnrc.yml
42+
43+ # Copy source code and config files
44+ COPY tsconfig*.json ./
45+ COPY src ./src
3246
3347# Build the application
3448RUN yarn build
3549
36- # Remove dev dependencies and rebuild for production
37- RUN --mount=type=cache,target=/app/.yarn/cache \
50+ # ============================================================================
51+ # PRODUCTION DEPENDENCIES STAGE - Install only production dependencies
52+ # ============================================================================
53+ FROM node:22-alpine AS production-deps
54+
55+ # Enable Corepack for Yarn 4
56+ RUN corepack enable
57+
58+ # Set working directory
59+ WORKDIR /app
60+
61+ # Copy package files
62+ COPY package.json yarn.lock .yarnrc.yml ./
63+ COPY .yarn/ ./.yarn/
64+
65+ # Install only production dependencies
66+ ENV NODE_ENV=production
67+ RUN --mount=type=cache,target=/root/.yarn/berry/cache \
68+ --mount=type=cache,target=/app/.yarn/cache \
3869 yarn workspaces focus --production
3970
4071# ============================================================================
@@ -55,9 +86,13 @@ RUN addgroup -g 1001 -S gitlab-mcp && \
5586# Set working directory
5687WORKDIR /app
5788
58- # Copy built application and dependencies from builder
89+ # Copy built application from builder
5990COPY --from=builder --chown=gitlab-mcp:gitlab-mcp /app/dist ./dist
60- COPY --from=builder --chown=gitlab-mcp:gitlab-mcp /app/node_modules ./node_modules
91+
92+ # Copy production dependencies from production-deps stage
93+ COPY --from=production-deps --chown=gitlab-mcp:gitlab-mcp /app/node_modules ./node_modules
94+
95+ # Copy package.json for reference
6196COPY --from=builder --chown=gitlab-mcp:gitlab-mcp /app/package.json ./package.json
6297
6398# ============================================================================
@@ -96,8 +131,8 @@ ENV HTTP_PROXY=""
96131ENV HTTPS_PROXY=""
97132ENV NO_PROXY=""
98133
99- # Performance and resource limits
100- ENV NODE_OPTIONS="--max-old-space-size=512"
134+ # Performance and resource limits with ESM support
135+ ENV NODE_OPTIONS="--max-old-space-size=512 --experimental-specifier-resolution=node --no-warnings "
101136
102137# Health check endpoint
103138ENV HEALTH_CHECK_ENABLED=true
0 commit comments