添加dockerfile
This commit is contained in:
22
Dockerfile
Normal file
22
Dockerfile
Normal 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"]
|
||||
Reference in New Issue
Block a user