# ============================================================================= # Codex Pool Multi-Stage Dockerfile # ============================================================================= # Stage 1: Build frontend # Stage 2: Build Go backend with embedded frontend # Stage 3: Final minimal image with Chromium # ============================================================================= ARG NODE_IMAGE=node:20-alpine ARG GOLANG_IMAGE=golang:1.23-alpine ARG ALPINE_IMAGE=alpine:3.20 ARG GOPROXY=https://goproxy.cn,direct # ----------------------------------------------------------------------------- # Stage 1: Frontend Builder # ----------------------------------------------------------------------------- FROM ${NODE_IMAGE} AS frontend-builder WORKDIR /app/frontend # Install dependencies first (better caching) COPY frontend/package*.json ./ RUN npm ci # Copy frontend source and build COPY frontend/ ./ RUN npm run build # ----------------------------------------------------------------------------- # Stage 2: Backend Builder # ----------------------------------------------------------------------------- FROM ${GOLANG_IMAGE} AS backend-builder ARG GOPROXY ENV GOPROXY=${GOPROXY} ENV GOTOOLCHAIN=auto # Install build dependencies RUN apk add --no-cache git gcc musl-dev WORKDIR /app/backend # Copy go mod files first (better caching) COPY backend/go.mod backend/go.sum ./ RUN go mod download # Copy backend source COPY backend/ ./ # Copy frontend dist from previous stage COPY --from=frontend-builder /app/frontend/dist ./internal/web/dist # Build the binary with embed tag (使用缓存加速) RUN --mount=type=cache,target=/root/.cache/go-build \ --mount=type=cache,target=/go/pkg/mod \ GOTOOLCHAIN=auto CGO_ENABLED=1 GOOS=linux go build \ -tags embed \ -ldflags="-s -w" \ -o /app/codex-pool \ ./cmd/main.go # ----------------------------------------------------------------------------- # Stage 3: Final Runtime Image with Chromium # ----------------------------------------------------------------------------- FROM ${ALPINE_IMAGE} # Labels LABEL maintainer="Codex Pool" LABEL description="Codex Pool - Team Account Management" # Install runtime dependencies including Chromium RUN apk add --no-cache \ chromium \ chromium-chromedriver \ ca-certificates \ tzdata \ curl \ && rm -rf /var/cache/apk/* # Chromedp configuration ENV CHROME_BIN=/usr/bin/chromium-browser ENV CHROME_PATH=/usr/bin/chromium-browser # Set working directory WORKDIR /app # Copy binary from builder COPY --from=backend-builder /app/codex-pool /app/codex-pool # Copy team-reg executable (if exists) - for auto-registration feature # 注意: 需要提供 Linux 版本的 team-reg 可执行文件 COPY backend/team-reg* /app/ # Create data directory RUN mkdir -p /app/data && \ chmod +x /app/team-reg* 2>/dev/null || true # Expose port EXPOSE 8848 # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \ CMD curl -f http://localhost:8848/api/health || exit 1 # Run the application ENTRYPOINT ["/app/codex-pool"]