Dockerfile 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # =============================================================================
  2. # Sub2API Multi-Stage Dockerfile
  3. # =============================================================================
  4. # Stage 1: Build frontend
  5. # Stage 2: Build Go backend with embedded frontend
  6. # Stage 3: Final minimal image
  7. # =============================================================================
  8. ARG NODE_IMAGE=node:24-alpine
  9. ARG GOLANG_IMAGE=golang:1.27.0-alpine
  10. ARG ALPINE_IMAGE=alpine:3.20
  11. ARG GOPROXY=https://goproxy.cn,direct
  12. ARG GOSUMDB=sum.golang.google.cn
  13. # -----------------------------------------------------------------------------
  14. # Stage 1: Frontend Builder
  15. # -----------------------------------------------------------------------------
  16. FROM ${NODE_IMAGE} AS frontend-builder
  17. WORKDIR /app/frontend
  18. ENV NODE_OPTIONS=--max-old-space-size=1536
  19. # Install pnpm. Keep this pinned to the lockfile-compatible major version so
  20. # Docker builds remain reproducible when pnpm changes config validation rules.
  21. RUN corepack enable && corepack prepare pnpm@9.15.9 --activate
  22. # Install dependencies first (better caching)
  23. COPY frontend/package.json frontend/pnpm-lock.yaml ./
  24. RUN pnpm install --frozen-lockfile
  25. # Copy frontend source and build.
  26. # LegalDocumentView.vue (admin-compliance gate) build-time imports
  27. # ../../../../docs/legal/*.md?raw, so docs/legal/ must sit beside frontend/
  28. # in the image (WORKDIR /app/frontend -> resolves to /app/docs/legal/*.md).
  29. # Copy only that subtree to keep the build dependency minimal.
  30. COPY frontend/ ./
  31. COPY docs/legal/ /app/docs/legal/
  32. RUN pnpm run build
  33. # -----------------------------------------------------------------------------
  34. # Stage 2: Backend Builder
  35. # -----------------------------------------------------------------------------
  36. FROM ${GOLANG_IMAGE} AS backend-builder
  37. # Build arguments for version info (set by CI)
  38. ARG VERSION=
  39. ARG COMMIT=docker
  40. ARG DATE
  41. ARG GOPROXY
  42. ARG GOSUMDB
  43. ENV GOPROXY=${GOPROXY}
  44. ENV GOSUMDB=${GOSUMDB}
  45. # Install build dependencies
  46. RUN apk add --no-cache git ca-certificates tzdata
  47. WORKDIR /app/backend
  48. # Copy go mod files first (better caching)
  49. COPY backend/go.mod backend/go.sum ./
  50. RUN go mod download
  51. # Copy backend source first
  52. COPY backend/ ./
  53. # Copy frontend dist from previous stage (must be after backend copy to avoid being overwritten)
  54. COPY --from=frontend-builder /app/backend/internal/web/dist ./internal/web/dist
  55. # Build the binary (BuildType=release for CI builds, embed frontend)
  56. # Version precedence: build arg VERSION > exact git tag > cmd/server/VERSION
  57. RUN VERSION_VALUE="${VERSION}" && \
  58. if [ -z "${VERSION_VALUE}" ]; then VERSION_VALUE="$(./scripts/resolve-version.sh)"; fi && \
  59. DATE_VALUE="${DATE:-$(date -u +%Y-%m-%dT%H:%M:%SZ)}" && \
  60. CGO_ENABLED=0 GOOS=linux go build \
  61. -tags embed \
  62. -ldflags="-s -w -X main.Version=${VERSION_VALUE} -X main.Commit=${COMMIT} -X main.Date=${DATE_VALUE} -X main.BuildType=release" \
  63. -o /app/sub2api \
  64. ./cmd/server
  65. # -----------------------------------------------------------------------------
  66. # Stage 3: Final Runtime Image
  67. # -----------------------------------------------------------------------------
  68. FROM ${ALPINE_IMAGE}
  69. # Labels
  70. LABEL maintainer="Wei-Shaw <github.com/Wei-Shaw>"
  71. LABEL description="Sub2API - AI API Gateway Platform"
  72. LABEL org.opencontainers.image.source="https://github.com/Wei-Shaw/sub2api"
  73. # Install runtime dependencies
  74. RUN apk add --no-cache \
  75. ca-certificates \
  76. tzdata \
  77. curl \
  78. su-exec \
  79. && rm -rf /var/cache/apk/*
  80. # Create non-root user
  81. RUN addgroup -g 1000 sub2api && \
  82. adduser -u 1000 -G sub2api -s /bin/sh -D sub2api
  83. # Set working directory
  84. WORKDIR /app
  85. # Copy binary and runtime fallback resources from builder
  86. COPY --from=backend-builder /app/sub2api /app/sub2api
  87. COPY --from=backend-builder --chown=sub2api:sub2api /app/backend/resources /app/resources
  88. # Create data directory
  89. RUN mkdir -p /app/data && chown -R sub2api:sub2api /app
  90. # Copy entrypoint script (fixes volume permissions then drops to sub2api)
  91. COPY deploy/docker-entrypoint.sh /app/docker-entrypoint.sh
  92. RUN chmod +x /app/docker-entrypoint.sh
  93. # Expose port (can be overridden by SERVER_PORT env var)
  94. EXPOSE 8080
  95. # Health check
  96. HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
  97. CMD wget -q -T 5 -O /dev/null http://localhost:${SERVER_PORT:-8080}/health || exit 1
  98. # Run the application (entrypoint fixes /app/data ownership then execs as sub2api)
  99. ENTRYPOINT ["/app/docker-entrypoint.sh"]
  100. CMD ["/app/sub2api"]