# =================================================================== # TopFans - Multi-stage Build for Go Services # Stage 1: Build all services in parallel # Stage 2: Create minimal runtime image for each service # =================================================================== # ---- Build Stage ---- FROM --platform=linux/amd64 golang:1.25-alpine AS builder # Install build dependencies RUN apk add --no-cache git make # Set working directory WORKDIR /build # Copy go mod files first for better caching COPY backend/go.mod backend/go.sum ./ # 配置 Go 模块代理,解决网络问题 RUN go env -w GOPROXY=https://goproxy.cn,https://mirrors.aliyun.com/go-proxy/,direct && \ go env -w GOSUMDB=off && \ go mod download # Copy source code COPY backend/ ./ # Build all services with optimization flags RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" \ -o /tmp/gateway gateway/main.go && \ echo "Built gateway" && \ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" \ -o /tmp/userservice services/userService/main.go && \ echo "Built userservice" && \ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" \ -o /tmp/socialservice services/socialService/main.go && \ echo "Built socialservice" && \ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" \ -o /tmp/assetservice services/assetService/main.go && \ echo "Built assetservice" && \ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" \ -o /tmp/galleryservice services/galleryService/main.go && \ echo "Built galleryservice" && \ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" \ -o /tmp/activityservice services/activityService/main.go && \ echo "Built activityservice" && \ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" \ -o /tmp/taskservice services/taskService/main.go && \ echo "Built taskservice" && \ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags="-s -w" \ -o /tmp/starbookservice services/starbookService/main.go && \ echo "Built starbookservice" # ---- Runtime Stage: Gateway ---- FROM --platform=linux/amd64 alpine:3.19 AS gateway RUN apk add --no-cache ca-certificates tzdata WORKDIR /app COPY --from=builder /tmp/gateway /app/gateway ENV GIN_MODE=release EXPOSE 8080 HEALTHCHECK --interval=10s --timeout=5s --start-period=15s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1 ENTRYPOINT ["/app/gateway"] CMD ["-port=8080"] # ---- Runtime Stage: UserService ---- FROM --platform=linux/amd64 alpine:3.19 AS userservice RUN apk add --no-cache ca-certificates tzdata WORKDIR /app COPY --from=builder /tmp/userservice /app/userservice EXPOSE 20000 HEALTHCHECK --interval=10s --timeout=5s --start-period=10s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:20000 || exit 1 ENTRYPOINT ["/app/userservice"] # ---- Runtime Stage: SocialService ---- FROM --platform=linux/amd64 alpine:3.19 AS socialservice RUN apk add --no-cache ca-certificates tzdata WORKDIR /app COPY --from=builder /tmp/socialservice /app/socialservice EXPOSE 20002 HEALTHCHECK --interval=10s --timeout=5s --start-period=10s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:20002 || exit 1 ENTRYPOINT ["/app/socialservice"] # ---- Runtime Stage: AssetService ---- FROM --platform=linux/amd64 alpine:3.19 AS assetservice RUN apk add --no-cache ca-certificates tzdata WORKDIR /app COPY --from=builder /tmp/assetservice /app/assetservice EXPOSE 20003 HEALTHCHECK --interval=10s --timeout=5s --start-period=10s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:20003 || exit 1 ENTRYPOINT ["/app/assetservice"] # ---- Runtime Stage: GalleryService ---- FROM --platform=linux/amd64 alpine:3.19 AS galleryservice RUN apk add --no-cache ca-certificates tzdata WORKDIR /app COPY --from=builder /tmp/galleryservice /app/galleryservice EXPOSE 20004 HEALTHCHECK --interval=10s --timeout=5s --start-period=10s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:20004 || exit 1 ENTRYPOINT ["/app/galleryservice"] # ---- Runtime Stage: ActivityService ---- FROM --platform=linux/amd64 alpine:3.19 AS activityservice RUN apk add --no-cache ca-certificates tzdata WORKDIR /app COPY --from=builder /tmp/activityservice /app/activityservice EXPOSE 20005 HEALTHCHECK --interval=10s --timeout=5s --start-period=10s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:20005 || exit 1 ENTRYPOINT ["/app/activityservice"] # ---- Runtime Stage: TaskService ---- FROM --platform=linux/amd64 alpine:3.19 AS taskservice RUN apk add --no-cache ca-certificates tzdata WORKDIR /app COPY --from=builder /tmp/taskservice /app/taskservice EXPOSE 20006 HEALTHCHECK --interval=10s --timeout=5s --start-period=10s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:21006 || exit 1 ENTRYPOINT ["/app/taskservice"] # ---- Runtime Stage: StarbookService ---- FROM --platform=linux/amd64 alpine:3.19 AS starbookservice RUN apk add --no-cache ca-certificates tzdata WORKDIR /app COPY --from=builder /tmp/starbookservice /app/starbookservice EXPOSE 20007 HEALTHCHECK --interval=10s --timeout=5s --start-period=10s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:21007 || exit 1 ENTRYPOINT ["/app/starbookservice"]