2025-12-18 13:50:39 +08:00
|
|
|
# =============================================================================
|
|
|
|
|
# Sub2API Multi-Stage Dockerfile
|
|
|
|
|
# =============================================================================
|
|
|
|
|
# Stage 1: Build frontend
|
|
|
|
|
# Stage 2: Build Go backend with embedded frontend
|
|
|
|
|
# Stage 3: Final minimal image
|
|
|
|
|
# =============================================================================
|
|
|
|
|
|
2025-12-29 12:00:33 +08:00
|
|
|
ARG NODE_IMAGE=node:24-alpine
|
2026-03-07 08:45:55 +08:00
|
|
|
ARG GOLANG_IMAGE=golang:1.26.1-alpine
|
2026-02-28 15:01:20 +08:00
|
|
|
ARG ALPINE_IMAGE=alpine:3.21
|
2026-03-13 10:38:19 +08:00
|
|
|
ARG POSTGRES_IMAGE=postgres:18-alpine
|
2025-12-29 16:59:07 +08:00
|
|
|
ARG GOPROXY=https://goproxy.cn,direct
|
|
|
|
|
ARG GOSUMDB=sum.golang.google.cn
|
2025-12-29 12:00:33 +08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
# Stage 1: Frontend Builder
|
|
|
|
|
# -----------------------------------------------------------------------------
|
2025-12-29 12:00:33 +08:00
|
|
|
FROM ${NODE_IMAGE} AS frontend-builder
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
WORKDIR /app/frontend
|
|
|
|
|
|
2026-01-05 11:10:31 +08:00
|
|
|
# Install pnpm
|
|
|
|
|
RUN corepack enable && corepack prepare pnpm@latest --activate
|
|
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
# Install dependencies first (better caching)
|
2026-01-05 11:10:31 +08:00
|
|
|
COPY frontend/package.json frontend/pnpm-lock.yaml ./
|
|
|
|
|
RUN pnpm install --frozen-lockfile
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
# Copy frontend source and build
|
|
|
|
|
COPY frontend/ ./
|
2026-01-05 11:10:31 +08:00
|
|
|
RUN pnpm run build
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
# Stage 2: Backend Builder
|
|
|
|
|
# -----------------------------------------------------------------------------
|
2025-12-29 12:00:33 +08:00
|
|
|
FROM ${GOLANG_IMAGE} AS backend-builder
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
# Build arguments for version info (set by CI)
|
2026-02-14 21:16:21 +08:00
|
|
|
ARG VERSION=
|
2025-12-18 13:50:39 +08:00
|
|
|
ARG COMMIT=docker
|
|
|
|
|
ARG DATE
|
2025-12-29 16:59:07 +08:00
|
|
|
ARG GOPROXY
|
|
|
|
|
ARG GOSUMDB
|
|
|
|
|
|
|
|
|
|
ENV GOPROXY=${GOPROXY}
|
|
|
|
|
ENV GOSUMDB=${GOSUMDB}
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
# Install build dependencies
|
|
|
|
|
RUN apk add --no-cache git ca-certificates tzdata
|
|
|
|
|
|
|
|
|
|
WORKDIR /app/backend
|
|
|
|
|
|
|
|
|
|
# Copy go mod files first (better caching)
|
|
|
|
|
COPY backend/go.mod backend/go.sum ./
|
|
|
|
|
RUN go mod download
|
|
|
|
|
|
2025-12-20 19:13:26 +08:00
|
|
|
# Copy backend source first
|
2025-12-18 13:50:39 +08:00
|
|
|
COPY backend/ ./
|
|
|
|
|
|
2025-12-20 19:13:26 +08:00
|
|
|
# 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)
|
2026-02-14 23:28:33 +08:00
|
|
|
# Version precedence: build arg VERSION > cmd/server/VERSION
|
|
|
|
|
RUN 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 \
|
2025-12-20 19:13:26 +08:00
|
|
|
-tags embed \
|
2026-02-14 23:28:33 +08:00
|
|
|
-ldflags="-s -w -X main.Version=${VERSION_VALUE} -X main.Commit=${COMMIT} -X main.Date=${DATE_VALUE} -X main.BuildType=release" \
|
2026-02-28 15:01:20 +08:00
|
|
|
-trimpath \
|
2025-12-18 13:50:39 +08:00
|
|
|
-o /app/sub2api \
|
|
|
|
|
./cmd/server
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
2026-03-13 10:38:19 +08:00
|
|
|
# Stage 3: PostgreSQL Client (version-matched with docker-compose)
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
FROM ${POSTGRES_IMAGE} AS pg-client
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
# Stage 4: Final Runtime Image
|
2025-12-18 13:50:39 +08:00
|
|
|
# -----------------------------------------------------------------------------
|
2025-12-29 12:00:33 +08:00
|
|
|
FROM ${ALPINE_IMAGE}
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
# Labels
|
|
|
|
|
LABEL 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 dependencies
|
|
|
|
|
RUN apk add --no-cache \
|
|
|
|
|
ca-certificates \
|
|
|
|
|
tzdata \
|
2026-03-16 19:52:14 +08:00
|
|
|
su-exec \
|
2026-03-13 10:38:19 +08:00
|
|
|
libpq \
|
|
|
|
|
zstd-libs \
|
|
|
|
|
lz4-libs \
|
|
|
|
|
krb5-libs \
|
|
|
|
|
libldap \
|
|
|
|
|
libedit \
|
2025-12-18 13:50:39 +08:00
|
|
|
&& rm -rf /var/cache/apk/*
|
|
|
|
|
|
2026-03-13 10:38:19 +08:00
|
|
|
# Copy pg_dump and psql from the same postgres image used in docker-compose
|
|
|
|
|
# This ensures version consistency between backup tools and the database server
|
|
|
|
|
COPY --from=pg-client /usr/local/bin/pg_dump /usr/local/bin/pg_dump
|
|
|
|
|
COPY --from=pg-client /usr/local/bin/psql /usr/local/bin/psql
|
|
|
|
|
COPY --from=pg-client /usr/local/lib/libpq.so.5* /usr/local/lib/
|
|
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
# Create non-root user
|
|
|
|
|
RUN addgroup -g 1000 sub2api && \
|
|
|
|
|
adduser -u 1000 -G sub2api -s /bin/sh -D sub2api
|
|
|
|
|
|
|
|
|
|
# Set working directory
|
|
|
|
|
WORKDIR /app
|
|
|
|
|
|
2026-02-28 15:01:20 +08:00
|
|
|
# Copy binary/resources with ownership to avoid extra full-layer chown copy
|
|
|
|
|
COPY --from=backend-builder --chown=sub2api:sub2api /app/sub2api /app/sub2api
|
|
|
|
|
COPY --from=backend-builder --chown=sub2api:sub2api /app/backend/resources /app/resources
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
# Create data directory
|
2026-02-28 15:01:20 +08:00
|
|
|
RUN mkdir -p /app/data && chown sub2api:sub2api /app/data
|
2025-12-18 13:50:39 +08:00
|
|
|
|
2026-03-16 19:52:14 +08:00
|
|
|
# Copy entrypoint script (fixes volume permissions then drops to sub2api)
|
|
|
|
|
COPY deploy/docker-entrypoint.sh /app/docker-entrypoint.sh
|
|
|
|
|
RUN chmod +x /app/docker-entrypoint.sh
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
# Expose port (can be overridden by SERVER_PORT env var)
|
|
|
|
|
EXPOSE 8080
|
|
|
|
|
|
|
|
|
|
# Health check
|
|
|
|
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
|
2026-02-28 15:01:20 +08:00
|
|
|
CMD wget -q -T 5 -O /dev/null http://localhost:${SERVER_PORT:-8080}/health || exit 1
|
2025-12-18 13:50:39 +08:00
|
|
|
|
2026-03-16 19:52:14 +08:00
|
|
|
# Run the application (entrypoint fixes /app/data ownership then execs as sub2api)
|
|
|
|
|
ENTRYPOINT ["/app/docker-entrypoint.sh"]
|
|
|
|
|
CMD ["/app/sub2api"]
|