Files
junhong_cmp_fiber/Dockerfile.worker
huang 919d4350d0
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Failing after 4s
在所有 Go 命令中设置 GOTOOLCHAIN=auto
2026-01-19 17:14:12 +08:00

59 lines
1.2 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ================================
# 阶段 1: 构建阶段
# ================================
FROM golang:1.23.4-alpine AS builder
# 设置工作目录
WORKDIR /build
# 安装必要的构建工具
RUN apk add --no-cache git ca-certificates tzdata
# 复制 go.mod 和 go.sum利用 Docker 缓存)
COPY go.mod go.sum ./
# 使用国内 Go 代理加速依赖下载
ENV GOPROXY=https://goproxy.cn,direct
ENV GOTOOLCHAIN=auto
RUN go mod download
# 复制源代码
COPY . .
# 编译 Worker 服务(静态链接)
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOTOOLCHAIN=auto go build \
-ldflags="-w -s" \
-o /build/worker \
cmd/worker/main.go
# ================================
# 阶段 2: 运行阶段
# ================================
FROM alpine:latest
# 安装运行时依赖
RUN apk add --no-cache ca-certificates tzdata bash
# 设置时区
ENV TZ=Asia/Shanghai
# 创建非 root 用户
RUN addgroup -g 1000 appuser && \
adduser -D -u 1000 -G appuser appuser
# 设置工作目录
WORKDIR /app
# 从构建阶段复制二进制文件
COPY --from=builder /build/worker /app/worker
# 复制配置文件
COPY configs /app/configs
# 切换到非 root 用户
USER appuser
# 启动命令
CMD ["/app/worker"]