# Go 服务 Dockerfile # 多阶段构建:编译阶段 + 运行阶段 # ===== 编译阶段 ===== FROM golang:1.21-alpine AS builder WORKDIR /build # 配置 Go 代理(国内镜像) ENV GOPROXY=https://goproxy.cn,direct # 先复制依赖文件,利用 Docker 缓存 COPY go.mod go.sum ./ RUN go mod download # 复制源代码并编译 COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o billai-server . # ===== 运行阶段 ===== FROM alpine:latest WORKDIR /app # 配置 Alpine 镜像源(国内) RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories # 安装必要工具 RUN apk --no-cache add ca-certificates tzdata curl # 设置时区 ENV TZ=Asia/Shanghai # 从编译阶段复制二进制文件 COPY --from=builder /build/billai-server . COPY --from=builder /build/config.yaml . # 创建必要目录 RUN mkdir -p uploads outputs # 暴露端口 EXPOSE 8080 # 健康检查 HEALTHCHECK --interval=10s --timeout=5s --retries=5 \ CMD curl -f http://localhost:8080/health || exit 1 # 启动服务 CMD ["./billai-server"]