From 6dbaa7c001d8f4ff42883a301bd0e44a0d2ecd85 Mon Sep 17 00:00:00 2001 From: JACKYMYPERSON Date: Tue, 16 Sep 2025 01:21:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0dockerfile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..999fc94 --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file