# ======================================== # Base Stage: Alpine Linux with Bun # ======================================== FROM oven/bun:1.3.3-alpine AS base # ======================================== # Dependencies Stage: Install Dependencies # ======================================== FROM base AS deps RUN apk add --no-cache libc6-compat WORKDIR /app COPY package.json bun.lock turbo.json ./ RUN mkdir -p apps packages/db COPY apps/sim/package.json ./apps/sim/package.json COPY packages/db/package.json ./packages/db/package.json # Install turbo globally and dependencies with cache mount for faster builds RUN --mount=type=cache,id=bun-cache,target=/root/.bun/install/cache \ bun install -g turbo && \ bun install --frozen-lockfile --omit dev --ignore-scripts # ======================================== # Builder Stage: Build the Application # ======================================== FROM base AS builder WORKDIR /app # Install turbo globally (cached for fast reinstall) RUN --mount=type=cache,id=bun-cache,target=/root/.bun/install/cache \ bun install -g turbo # Copy node_modules from deps stage (cached if dependencies don't change) COPY --from=deps /app/node_modules ./node_modules # Copy package configuration files (needed for build) COPY package.json bun.lock turbo.json ./ COPY apps/sim/package.json ./apps/sim/package.json COPY packages/db/package.json ./packages/db/package.json # Copy workspace configuration files (needed for turbo) COPY apps/sim/next.config.ts ./apps/sim/next.config.ts COPY apps/sim/tsconfig.json ./apps/sim/tsconfig.json COPY apps/sim/tailwind.config.ts ./apps/sim/tailwind.config.ts COPY apps/sim/postcss.config.mjs ./apps/sim/postcss.config.mjs # Copy source code (changes most frequently - placed last to maximize cache hits) COPY apps/sim ./apps/sim COPY packages ./packages # Required for standalone nextjs build WORKDIR /app/apps/sim RUN --mount=type=cache,id=bun-cache,target=/root/.bun/install/cache \ bun install sharp ENV NEXT_TELEMETRY_DISABLED=1 \ VERCEL_TELEMETRY_DISABLED=1 \ DOCKER_BUILD=1 WORKDIR /app # Provide dummy database URLs during image build so server code that imports @sim/db # can be evaluated without crashing. Runtime environments should override these. ARG DATABASE_URL="postgresql://user:pass@localhost:5432/dummy" ENV DATABASE_URL=${DATABASE_URL} # Provide dummy NEXT_PUBLIC_APP_URL for build-time evaluation # Runtime environments should override this with the actual URL ARG NEXT_PUBLIC_APP_URL="http://localhost:3000" ENV NEXT_PUBLIC_APP_URL=${NEXT_PUBLIC_APP_URL} RUN bun run build # ======================================== # Runner Stage: Run the actual app # ======================================== FROM base AS runner WORKDIR /app # Install Python and dependencies for guardrails PII detection (cached separately) # Also install ffmpeg for audio/video processing in STT RUN apk add --no-cache python3 py3-pip bash ffmpeg ENV NODE_ENV=production # Create non-root user and group (cached separately) RUN addgroup -g 1001 -S nodejs && \ adduser -S nextjs -u 1001 # Copy application artifacts from builder (these change on every build) COPY --from=builder --chown=nextjs:nodejs /app/apps/sim/public ./apps/sim/public COPY --from=builder --chown=nextjs:nodejs /app/apps/sim/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/apps/sim/.next/static ./apps/sim/.next/static # Guardrails setup (files need to be owned by nextjs for runtime) COPY --from=builder --chown=nextjs:nodejs /app/apps/sim/lib/guardrails/setup.sh ./apps/sim/lib/guardrails/setup.sh COPY --from=builder --chown=nextjs:nodejs /app/apps/sim/lib/guardrails/requirements.txt ./apps/sim/lib/guardrails/requirements.txt COPY --from=builder --chown=nextjs:nodejs /app/apps/sim/lib/guardrails/validate_pii.py ./apps/sim/lib/guardrails/validate_pii.py # Run guardrails setup as root, then fix ownership of generated venv files RUN chmod +x ./apps/sim/lib/guardrails/setup.sh && \ cd ./apps/sim/lib/guardrails && \ ./setup.sh && \ chown -R nextjs:nodejs /app/apps/sim/lib/guardrails # Create .next/cache directory with correct ownership RUN mkdir -p apps/sim/.next/cache && \ chown -R nextjs:nodejs /app # Switch to non-root user USER nextjs EXPOSE 3000 ENV PORT=3000 \ HOSTNAME="0.0.0.0" CMD ["bun", "apps/sim/server.js"]