# =============================================================================# Sub2API Multi-Stage Dockerfile# =============================================================================# Stage 1: Build frontend# Stage 2: Build Go backend with embedded frontend# Stage 3: Final minimal image# =============================================================================ARG NODE_IMAGE=node:24-alpineARG GOLANG_IMAGE=golang:1.26.1-alpineARG ALPINE_IMAGE=alpine:3.21ARG POSTGRES_IMAGE=postgres:18-alpineARG GOPROXY=https://goproxy.cn,directARG GOSUMDB=sum.golang.google.cn# -----------------------------------------------------------------------------# Stage 1: Frontend Builder# -----------------------------------------------------------------------------FROM ${NODE_IMAGE} AS frontend-builderWORKDIR /app/frontend# Install pnpmRUN corepack enable && corepack prepare pnpm@latest --activate# Install dependencies first (better caching)COPY frontend/package.json frontend/pnpm-lock.yaml ./RUN pnpm install --frozen-lockfile# Copy frontend source and buildCOPY frontend/ ./RUN pnpm run build# -----------------------------------------------------------------------------# Stage 2: Backend Builder# -----------------------------------------------------------------------------FROM ${GOLANG_IMAGE} AS backend-builder# Build arguments for version info (set by CI)ARG VERSION=ARG COMMIT=dockerARG DATEARG GOPROXYARG GOSUMDBENV GOPROXY=${GOPROXY}ENV GOSUMDB=${GOSUMDB}# Install build dependenciesRUN apk add --no-cache git ca-certificates tzdataWORKDIR /app/backend# Copy go mod files first (better caching)COPY backend/go.mod backend/go.sum ./RUN go mod download# Copy backend source firstCOPY backend/ ./# Copy frontend dist from previous stage (must be after backend copy to avoid being overwritten)COPY --from=frontend-builder /app/backend/internal/web/dist ./internal/web/dist# Build the binary (BuildType=release for CI builds, embed frontend)# Version precedence: build arg VERSION > cmd/server/VERSIONRUN VERSION_VALUE="${VERSION}" && \if [ -z "${VERSION_VALUE}" ]; then VERSION_VALUE="$(tr -d '\r\n' < ./cmd/server/VERSION)"; fi && \DATE_VALUE="${DATE:-$(date -u +%Y-%m-%dT%H:%M:%SZ)}" && \CGO_ENABLED=0 GOOS=linux go build \-tags embed \-ldflags="-s -w -X main.Version=${VERSION_VALUE} -X main.Commit=${COMMIT} -X main.Date=${DATE_VALUE} -X main.BuildType=release" \-trimpath \-o /app/sub2api \./cmd/server# -----------------------------------------------------------------------------# Stage 3: PostgreSQL Client (version-matched with docker-compose)# -----------------------------------------------------------------------------FROM ${POSTGRES_IMAGE} AS pg-client# -----------------------------------------------------------------------------# Stage 4: Final Runtime Image# -----------------------------------------------------------------------------FROM ${ALPINE_IMAGE}# LabelsLABEL maintainer="Wei-Shaw <github.com/Wei-Shaw>"LABEL description="Sub2API - AI API Gateway Platform"LABEL org.opencontainers.image.source="https://github.com/Wei-Shaw/sub2api"# Install runtime dependenciesRUN apk add --no-cache \ca-certificates \tzdata \su-exec \libpq \zstd-libs \lz4-libs \krb5-libs \libldap \libedit \&& rm -rf /var/cache/apk/*# Copy pg_dump and psql from the same postgres image used in docker-compose# This ensures version consistency between backup tools and the database serverCOPY --from=pg-client /usr/local/bin/pg_dump /usr/local/bin/pg_dumpCOPY --from=pg-client /usr/local/bin/psql /usr/local/bin/psqlCOPY --from=pg-client /usr/local/lib/libpq.so.5* /usr/local/lib/# Create non-root userRUN addgroup -g 1000 sub2api && \adduser -u 1000 -G sub2api -s /bin/sh -D sub2api# Set working directoryWORKDIR /app# Copy binary/resources with ownership to avoid extra full-layer chown copyCOPY --from=backend-builder --chown=sub2api:sub2api /app/sub2api /app/sub2apiCOPY --from=backend-builder --chown=sub2api:sub2api /app/backend/resources /app/resources# Create data directoryRUN mkdir -p /app/data && chown sub2api:sub2api /app/data# Copy entrypoint script (fixes volume permissions then drops to sub2api)COPY deploy/docker-entrypoint.sh /app/docker-entrypoint.shRUN chmod +x /app/docker-entrypoint.sh# Expose port (can be overridden by SERVER_PORT env var)EXPOSE 8080# Health checkHEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \CMD wget -q -T 5 -O /dev/null http://localhost:${SERVER_PORT:-8080}/health || exit 1# Run the application (entrypoint fixes /app/data ownership then execs as sub2api)ENTRYPOINT ["/app/docker-entrypoint.sh"]CMD ["/app/sub2api"]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。