添加dockerfile

This commit is contained in:
JACKYMYPERSON
2025-09-16 01:21:02 +08:00
parent 2196168d6d
commit 6dbaa7c001

22
Dockerfile Normal file
View File

@@ -0,0 +1,22 @@
# 阶段1编译 Go 程序(使用 Go 1.24 系列的 alpine 镜像,自动拉取最新小版本)
FROM golang:1.24-alpine AS builder
WORKDIR /app
# 复制依赖文件
COPY go.mod go.sum ./
# 国内代理加速依赖下载(可选但推荐)
ENV GOPROXY=https://goproxy.cn,direct
# 下载依赖
RUN go mod download
# 复制代码并编译(关闭 CGO 确保镜像可移植)
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o main .
# 阶段2构建轻量级运行镜像
FROM alpine:3.18
WORKDIR /app
# 从编译阶段复制二进制文件
COPY --from=builder /app/main .
# 可选:配置时区(避免日志时间异常)
RUN apk add --no-cache tzdata && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 启动程序
CMD ["./main"]