Dockerfile 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # syntax=docker/dockerfile:1.7
  2. # =============================================================================
  3. # Sub2API Multi-Stage Dockerfile
  4. # =============================================================================
  5. # Stage 1: Build frontend
  6. # Stage 2: Build Go backend with embedded frontend
  7. # Stage 3: Final minimal image
  8. # =============================================================================
  9. ARG NODE_IMAGE=node:24-alpine
  10. ARG GOLANG_IMAGE=golang:1.27.0-alpine
  11. ARG ALPINE_IMAGE=alpine:3.21
  12. ARG POSTGRES_IMAGE=postgres:18-alpine
  13. ARG GOPROXY=https://goproxy.cn,direct
  14. ARG GOSUMDB=sum.golang.google.cn
  15. ARG NPM_CONFIG_REGISTRY=
  16. # -----------------------------------------------------------------------------
  17. # Stage 1: Frontend Builder
  18. # -----------------------------------------------------------------------------
  19. # --platform=$BUILDPLATFORM: the frontend output is JS (arch-neutral), so build
  20. # it on the native host arch instead of under QEMU emulation for the target.
  21. FROM --platform=${BUILDPLATFORM} ${NODE_IMAGE} AS frontend-builder
  22. ARG NPM_CONFIG_REGISTRY
  23. WORKDIR /app/frontend
  24. # Install pnpm (pinned to v9 to match CI and keep builds reproducible)
  25. RUN corepack enable && corepack prepare pnpm@9 --activate
  26. # Install dependencies first (better caching)
  27. COPY frontend/package.json frontend/pnpm-lock.yaml ./
  28. RUN --mount=type=cache,id=sub2api-pnpm-store,target=/root/.local/share/pnpm/store \
  29. if [ -n "${NPM_CONFIG_REGISTRY}" ]; then pnpm config set registry "${NPM_CONFIG_REGISTRY}"; fi && \
  30. pnpm install --frozen-lockfile --prefer-offline
  31. # Copy frontend source and build.
  32. # LegalDocumentView.vue (admin-compliance gate) build-time imports
  33. # ../../../../docs/legal/*.md?raw, so docs/legal/ must sit beside frontend/
  34. # in the image (WORKDIR /app/frontend -> resolves to /app/docs/legal/*.md).
  35. # Copy only that subtree to keep the build dependency minimal.
  36. COPY frontend/ ./
  37. COPY docs/legal/ /app/docs/legal/
  38. ENV NODE_OPTIONS=--max-old-space-size=3072
  39. RUN pnpm run build
  40. # -----------------------------------------------------------------------------
  41. # Stage 2: Backend Builder
  42. # -----------------------------------------------------------------------------
  43. # --platform=$BUILDPLATFORM: run the Go toolchain on the native host arch and
  44. # cross-compile to the target arch below. The binary is CGO_ENABLED=0, so this
  45. # is a clean pure-Go cross-compile — no QEMU emulation of go mod download / go
  46. # build (emulated networking here was dropping module fetches with EOF).
  47. FROM --platform=${BUILDPLATFORM} ${GOLANG_IMAGE} AS backend-builder
  48. # Build arguments for version info (set by CI)
  49. ARG VERSION=
  50. ARG COMMIT=docker
  51. ARG DATE
  52. ARG GOPROXY
  53. ARG GOSUMDB
  54. # Populated by buildx from the --platform target (e.g. linux/amd64).
  55. ARG TARGETOS
  56. ARG TARGETARCH
  57. ENV GOPROXY=${GOPROXY}
  58. ENV GOSUMDB=${GOSUMDB}
  59. # Install build dependencies
  60. RUN apk add --no-cache git ca-certificates tzdata
  61. WORKDIR /app/backend
  62. # Copy go mod files first (better caching)
  63. COPY backend/go.mod backend/go.sum ./
  64. # Cache mount keeps the module cache across builds so a transient CDN blip on
  65. # retry resumes instead of re-fetching every zip from scratch.
  66. RUN --mount=type=cache,id=sub2api-gomod,target=/go/pkg/mod \
  67. go mod download
  68. # Copy backend source first
  69. COPY backend/ ./
  70. # Copy frontend dist from previous stage (must be after backend copy to avoid being overwritten)
  71. COPY --from=frontend-builder /app/backend/internal/web/dist ./internal/web/dist
  72. # Build the binary (BuildType=release for CI builds, embed frontend)
  73. # Version precedence: build arg VERSION > exact git tag > cmd/server/VERSION
  74. RUN --mount=type=cache,id=sub2api-gomod,target=/go/pkg/mod \
  75. --mount=type=cache,id=sub2api-gobuild,target=/root/.cache/go-build \
  76. VERSION_VALUE="${VERSION}" && \
  77. if [ -z "${VERSION_VALUE}" ]; then VERSION_VALUE="$(./scripts/resolve-version.sh)"; fi && \
  78. DATE_VALUE="${DATE:-$(date -u +%Y-%m-%dT%H:%M:%SZ)}" && \
  79. CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build \
  80. -tags embed \
  81. -ldflags="-s -w -X main.Version=${VERSION_VALUE} -X main.Commit=${COMMIT} -X main.Date=${DATE_VALUE} -X main.BuildType=release" \
  82. -trimpath \
  83. -o /app/sub2api \
  84. ./cmd/server
  85. # -----------------------------------------------------------------------------
  86. # Stage 3: PostgreSQL Client (version-matched with docker-compose)
  87. # -----------------------------------------------------------------------------
  88. FROM ${POSTGRES_IMAGE} AS pg-client
  89. # -----------------------------------------------------------------------------
  90. # Stage 4: Final Runtime Image
  91. # -----------------------------------------------------------------------------
  92. FROM ${ALPINE_IMAGE}
  93. # Labels
  94. LABEL maintainer="Wei-Shaw <github.com/Wei-Shaw>"
  95. LABEL description="Sub2API - AI API Gateway Platform"
  96. LABEL org.opencontainers.image.source="https://github.com/Wei-Shaw/sub2api"
  97. # Install runtime dependencies
  98. RUN apk add --no-cache \
  99. ca-certificates \
  100. tzdata \
  101. su-exec \
  102. libpq \
  103. zstd-libs \
  104. lz4-libs \
  105. krb5-libs \
  106. libldap \
  107. libedit \
  108. && rm -rf /var/cache/apk/*
  109. # Copy pg_dump and psql from the same postgres image used in docker-compose
  110. # This ensures version consistency between backup tools and the database server
  111. COPY --from=pg-client /usr/local/bin/pg_dump /usr/local/bin/pg_dump
  112. COPY --from=pg-client /usr/local/bin/psql /usr/local/bin/psql
  113. COPY --from=pg-client /usr/local/lib/libpq.so.5* /usr/local/lib/
  114. # Create non-root user
  115. RUN addgroup -g 1000 sub2api && \
  116. adduser -u 1000 -G sub2api -s /bin/sh -D sub2api
  117. # Set working directory
  118. WORKDIR /app
  119. # Copy binary/resources with ownership to avoid extra full-layer chown copy
  120. COPY --from=backend-builder --chown=sub2api:sub2api /app/sub2api /app/sub2api
  121. COPY --from=backend-builder --chown=sub2api:sub2api /app/backend/resources /app/resources
  122. # Create data directory
  123. RUN mkdir -p /app/data && chown sub2api:sub2api /app/data
  124. # Copy entrypoint script (fixes volume permissions then drops to sub2api)
  125. COPY deploy/docker-entrypoint.sh /app/docker-entrypoint.sh
  126. RUN chmod +x /app/docker-entrypoint.sh
  127. # Expose port (can be overridden by SERVER_PORT env var)
  128. EXPOSE 8080
  129. # Health check
  130. HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
  131. CMD wget -q -T 5 -O /dev/null http://localhost:${SERVER_PORT:-8080}/health || exit 1
  132. # Run the application (entrypoint fixes /app/data ownership then execs as sub2api)
  133. ENTRYPOINT ["/app/docker-entrypoint.sh"]
  134. CMD ["/app/sub2api"]