diff --git a/Dockerfile b/Dockerfile index 297d841..02cd640 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,24 +1,44 @@ -# 最小化Docker镜像 -FROM busybox:uclibc +# 多阶段构建 - 第一阶段:编译(使用已安装的镜像) +FROM golang:1.26-alpine3.23 AS builder + +RUN apk add --no-cache git ca-certificates tzdata + +ENV GO111MODULE=on +ENV GOPROXY=https://goproxy.cn,direct +ENV CGO_ENABLED=0 +ENV GOTOOLCHAIN=auto +ENV GOPRIVATE=gitea.com/red-future/common + +# 配置git使用私有Gitea仓库(带Token认证) +RUN git config --global url."http://x-token-auth:9b31146aa8c10a7cb4f2e49dcee0934a223be1076289810e1ad98b968066c2bc@116.204.74.41:3000/red-future/common.git".insteadOf "https://gitea.com/red-future/common.git" && \ + git config --global credential.helper store + +WORKDIR /build + +# 复制父目录的 common 模块(因为 go.mod 中使用了本地 replace) +COPY ../common /build/common +COPY . . + +RUN go mod download && go mod tidy + +RUN go build -ldflags="-s -w" -o main ./main.go + +# 第二阶段:运行 +FROM alpine:3.23 + +ENV TIME_ZONE=Asia/Shanghai +RUN apk add --no-cache ca-certificates tzdata && \ + ln -sf /usr/share/zoneinfo/$TIME_ZONE /etc/localtime WORKDIR /app -# 复制时区数据 -COPY timezone/localtime /etc/localtime -COPY timezone/timezone /etc/timezone -COPY timezone/Shanghai /usr/share/zoneinfo/Asia/Shanghai - -# 复制预构建的二进制文件和配置文件 -COPY app ./main -COPY config.yml ./ +# 复制编译好的二进制文件 +COPY --from=builder /build/main . +COPY --from=builder /build/config.yml ./ # 创建日志目录 RUN mkdir -p /logs /app/resource/log/run /app/resource/log/server -# 添加执行权限 -RUN chmod +x /app/main - EXPOSE 9000 -# 使用root用户运行 CMD ["./main"] diff --git a/app b/app deleted file mode 100755 index 464e733..0000000 Binary files a/app and /dev/null differ diff --git a/timezone/Shanghai b/timezone/Shanghai deleted file mode 100644 index 91f6f8b..0000000 Binary files a/timezone/Shanghai and /dev/null differ diff --git a/timezone/localtime b/timezone/localtime deleted file mode 100644 index 91f6f8b..0000000 Binary files a/timezone/localtime and /dev/null differ diff --git a/timezone/timezone b/timezone/timezone deleted file mode 100644 index 421b7a4..0000000 --- a/timezone/timezone +++ /dev/null @@ -1 +0,0 @@ -Asia/Shanghai