|
| 1 | +# Stage 1: Build dependencies (Rust + Foundry) |
| 2 | +FROM rust:1.83-bookworm AS rust-builder |
| 3 | + |
| 4 | +# Install build dependencies |
| 5 | +RUN apt-get update && apt-get install -y \ |
| 6 | + curl \ |
| 7 | + git \ |
| 8 | + build-essential \ |
| 9 | + pkg-config \ |
| 10 | + libssl-dev \ |
| 11 | + && rm -rf /var/lib/apt/lists/* |
| 12 | + |
| 13 | +# Add wasm32 target |
| 14 | +RUN rustup target add wasm32-unknown-unknown |
| 15 | + |
| 16 | +# Install wasm-pack |
| 17 | +RUN curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh |
| 18 | + |
| 19 | +# Install Foundry (forge, cast, anvil) |
| 20 | +RUN curl -L https://foundry.paradigm.xyz | bash |
| 21 | +ENV PATH="/root/.foundry/bin:${PATH}" |
| 22 | +RUN foundryup |
| 23 | + |
| 24 | +# Stage 2: Build Node.js dependencies and contracts |
| 25 | +FROM node:22-bookworm-slim AS builder |
| 26 | + |
| 27 | +# Copy Foundry and Rust tools from previous stage |
| 28 | +COPY --from=rust-builder /usr/local/cargo /usr/local/cargo |
| 29 | +COPY --from=rust-builder /usr/local/rustup /usr/local/rustup |
| 30 | +COPY --from=rust-builder /root/.foundry /root/.foundry |
| 31 | +ENV PATH="/root/.foundry/bin:/usr/local/cargo/bin:${PATH}" |
| 32 | +ENV RUSTUP_HOME=/usr/local/rustup |
| 33 | +ENV CARGO_HOME=/usr/local/cargo |
| 34 | + |
| 35 | +# Enable pnpm |
| 36 | +ENV PNPM_HOME="/pnpm" |
| 37 | +ENV PATH="$PNPM_HOME:$PATH" |
| 38 | +RUN corepack enable pnpm |
| 39 | + |
| 40 | +# Install build dependencies for native modules |
| 41 | +RUN apt-get update && apt-get install -y \ |
| 42 | + git \ |
| 43 | + python3 \ |
| 44 | + make \ |
| 45 | + g++ \ |
| 46 | + && rm -rf /var/lib/apt/lists/* |
| 47 | + |
| 48 | +# Set working directory |
| 49 | +WORKDIR /usr/src/app |
| 50 | + |
| 51 | +# Copy the entire monorepo |
| 52 | +COPY . . |
| 53 | + |
| 54 | +# Install all dependencies |
| 55 | +RUN pnpm install -r --frozen-lockfile |
| 56 | + |
| 57 | +# Build ERC-4337 contracts with Foundry |
| 58 | +WORKDIR /usr/src/app/packages/erc4337-contracts |
| 59 | +RUN forge soldeer install |
| 60 | +RUN forge build |
| 61 | + |
| 62 | +# Build ERC-4337 related packages |
| 63 | +WORKDIR /usr/src/app |
| 64 | +RUN pnpm nx build web-sdk |
| 65 | +RUN pnpm nx build sdk-4337 |
| 66 | + |
| 67 | +# Build auth-server-api |
| 68 | +RUN pnpm nx build auth-server-api |
| 69 | + |
| 70 | +# Deploy only production dependencies for auth-server-api |
| 71 | +RUN pnpm deploy --filter=auth-server-api --prod /prod/auth-server-api |
| 72 | + |
| 73 | +# Stage 3: Production runtime |
| 74 | +# Using distroless for minimal attack surface |
| 75 | +FROM gcr.io/distroless/nodejs22-debian12:nonroot AS production |
| 76 | + |
| 77 | +# Copy the deployed auth-server-api with its dependencies |
| 78 | +COPY --from=builder --chown=nonroot:nonroot /prod/auth-server-api /prod/auth-server-api |
| 79 | + |
| 80 | +# Copy the built dist folder |
| 81 | +COPY --from=builder --chown=nonroot:nonroot /usr/src/app/packages/auth-server-api/dist /prod/auth-server-api/dist |
| 82 | + |
| 83 | +WORKDIR /prod/auth-server-api |
| 84 | + |
| 85 | +# Environment variables |
| 86 | +ENV PORT=3004 |
| 87 | +ENV NODE_ENV=production |
| 88 | + |
| 89 | +# Healthcheck |
| 90 | +HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ |
| 91 | + CMD ["node", "-e", "require('http').get('http://localhost:3004/api/health', (r) => process.exit(r.statusCode === 200 ? 0 : 1))"] |
| 92 | + |
| 93 | +# Expose port |
| 94 | +EXPOSE 3004 |
| 95 | + |
| 96 | +# Start the server with WASM support |
| 97 | +ENTRYPOINT ["node", "--experimental-wasm-modules", "dist/index.js"] |
0 commit comments