11# Multi-stage build for optimized containerized testing using Chainguard Wolfi OS
22FROM cgr.dev/chainguard/node:latest-dev AS base
33
4- # Install build dependencies using Wolfi OS package manager
4+ # Install build dependencies and setup environment in single root block
55USER root
66RUN apk update && apk add --no-cache \
77 python3 \
@@ -10,39 +10,31 @@ RUN apk update && apk add --no-cache \
1010 glibc-dev \
1111 linux-headers \
1212 curl \
13- bash
13+ bash && \
14+ mkdir -p /app/node_modules /app/.npm && \
15+ chown -R 65532:65532 /app
1416
15- # Enable pnpm using corepack (no root permissions needed)
16- USER root
17- RUN corepack enable pnpm
18- USER 65532:65532
19-
20- # Create app directory with proper permissions (Chainguard uses /app by default)
21- USER root
17+ # Set working directory and switch to non-root user
2218WORKDIR /app
23- RUN mkdir -p /app/node_modules /app/.npm && chown -R 65532:65532 /app
2419USER 65532:65532
2520
2621# Dependencies stage - optimized for caching
2722FROM base AS dependencies
2823
29- # Copy only package files for optimal layer caching
24+ # Copy only package files first for optimal layer caching
3025COPY --chown=65532:65532 package.json pnpm-lock.yaml ./
26+
27+ # Install dependencies before copying source code
28+ RUN pnpm install --frozen-lockfile --ignore-scripts --prefer-frozen-lockfile \
29+ && pnpm store prune
30+
31+ # Copy source files after dependencies are installed
3132COPY --chown=65532:65532 scripts/ ./scripts/
3233COPY --chown=65532:65532 binding.gyp ./
3334COPY --chown=65532:65532 src/ ./src/
3435COPY --chown=65532:65532 test/ ./test/
3536COPY --chown=65532:65532 tsconfig.json vitest.config.ts ./
3637
37- # Configure npm for performance (keep for compatibility)
38- USER root
39- RUN npm config set cache /app/.npm --global
40- USER 65532:65532
41-
42- # Install dependencies with optimizations
43- RUN pnpm install --frozen-lockfile --ignore-scripts --prefer-frozen-lockfile \
44- && pnpm store prune
45-
4638# Skip native build in container - it's built in CI
4739RUN echo "⏭️ Skipping native build in container - built in CI"
4840
@@ -51,10 +43,6 @@ FROM cgr.dev/chainguard/node:latest AS runtime
5143
5244# Copy installed dependencies from dependencies stage
5345COPY --from=dependencies --chown=65532:65532 /app/node_modules ./node_modules
54- COPY --from=dependencies --chown=65532:65532 /app/.npm ./.npm
55-
56- # Use npm instead of pnpm in distroless runtime (npm is available)
57- # pnpm dependencies are already installed in node_modules
5846
5947# Note: Native modules are built separately in CI and not included in container
6048RUN echo "📝 Container ready for native module compilation"
@@ -64,9 +52,7 @@ USER 65532:65532
6452
6553# Set environment variables for optimized testing
6654ENV NODE_ENV=test \
67- CI=true \
68- npm_config_cache=/app/.npm \
69- PNPM_HOME=/app/.npm
55+ CI=true
7056
7157# Expose debugging port
7258EXPOSE 9229
@@ -78,5 +64,5 @@ HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
7864# Use dumb-init for proper signal handling in distroless environment
7965ENTRYPOINT ["/usr/bin/dumb-init" , "--" ]
8066
81- # Default command optimized for speed (use npm for test execution in distroless)
82- CMD ["npm" , "run " , "test" ]
67+ # Default command optimized for speed
68+ CMD ["pnpm " , "test" ]
0 commit comments