2024-01-21 22:49:55 +08:00
|
|
|
# 使用官方Go镜像作为构建环境
|
|
|
|
FROM golang:1.21.6 as builder
|
|
|
|
|
|
|
|
# 设置工作目录
|
|
|
|
WORKDIR /app
|
|
|
|
|
|
|
|
# 复制go.mod和go.sum文件
|
|
|
|
COPY go.mod go.sum ./
|
|
|
|
|
2024-01-21 23:59:26 +08:00
|
|
|
RUN go env -w GO111MODULE=on && go env -w GOPROXY=https://goproxy.cn,direct
|
2024-01-21 22:49:55 +08:00
|
|
|
# 下载依赖项
|
|
|
|
RUN go mod tidy
|
|
|
|
|
|
|
|
# 复制源代码
|
|
|
|
COPY . .
|
|
|
|
|
|
|
|
# 构建应用程序
|
2024-01-21 23:59:26 +08:00
|
|
|
RUN CGO_ENABLED=1 GOOS=linux go build -v -o server
|
2024-01-21 22:49:55 +08:00
|
|
|
|
|
|
|
# 使用scratch作为基础镜像
|
|
|
|
FROM scratch
|
|
|
|
|
|
|
|
# 从builder镜像中复制编译好的应用程序
|
|
|
|
COPY --from=builder /app/server /server
|
|
|
|
|
|
|
|
# 暴露端口
|
|
|
|
EXPOSE 8080
|
|
|
|
|
|
|
|
# 运行应用程序
|
2024-01-22 00:02:22 +08:00
|
|
|
ENTRYPOINT ["/app/server"]
|