diff --git a/Dockerfile b/Dockerfile index 0359e97..f95513c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,7 +20,7 @@ COPY grpc/user/userv1/go.mod grpc/user/userv1/go.sum* ./grpc/user/userv1/ WORKDIR /app RUN go work sync -# 安装 protoc 插件(需要在有 go.mod 的环境中安装) +# 安装 protoc 插件(使用单个 RUN 命令减少层数) # 使用临时工作目录安装插件,避免影响项目目录 WORKDIR /tmp RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@latest && \ @@ -60,20 +60,11 @@ RUN go build -ldflags="-w -s" -trimpath -o /out/server ./cmd/server/main.go && \ # 使用最小化的 alpine 镜像(包含时区支持,应用需要 loc=Local) FROM alpine:3.19 -# 只安装运行时必需的包:时区和 CA 证书(用于可能的 HTTPS 连接) +# 安装运行时必需的包:时区和 CA 证书 RUN apk add --no-cache ca-certificates tzdata && \ - # 创建非 root 用户 - addgroup -g 1000 appuser && \ - adduser -D -u 1000 -G appuser appuser && \ - # 清理缓存 - rm -rf /var/cache/apk/* + mkdir -p /app/storage/export /app/log WORKDIR /app COPY --from=builder /out/server /app/server -# 设置文件所有者和执行权限(在切换用户之前) -RUN chown appuser:appuser /app/server && chmod +x /app/server -# 创建必要的目录 -RUN mkdir -p /app/storage/export /app/log && \ - chown -R appuser:appuser /app/storage /app/log EXPOSE 8077 -USER appuser:appuser +# 使用 root 用户运行 ENTRYPOINT ["/app/server"] diff --git a/scripts/deploy_docker.sh b/scripts/deploy_docker.sh index c9b7409..3cd797d 100755 --- a/scripts/deploy_docker.sh +++ b/scripts/deploy_docker.sh @@ -32,11 +32,16 @@ fi # proto 文件将在容器内自动生成,无需在构建前生成 # 每次重新构建镜像 -echo "开始构建 Docker 镜像..." +echo "开始构建 Docker 镜像(使用构建缓存加速)..." +echo "提示: 首次构建可能需要 3-5 分钟,后续构建会更快(使用缓存)" +START_TIME=$(date +%s) DOCKER_BUILDKIT=1 docker build \ + --progress=plain \ --build-arg BUILDKIT_INLINE_CACHE=1 \ --build-arg GOPROXY="${GOPROXY:-https://goproxy.cn,direct}" \ -t "$IMAGE:$TAG" -f Dockerfile . +BUILD_TIME=$(($(date +%s) - START_TIME)) +echo "构建完成,耗时: ${BUILD_TIME} 秒" USE_IMAGE="$IMAGE:$TAG" mkdir -p log storage/export