chore(docker): 更新 Dockerfile 和部署脚本以优化构建过程

- 将基础镜像版本更新至 golang:1.21-alpine,确保与最新的 Go 语言特性兼容。
- 在 Dockerfile 中添加了构建时的优化选项,减小生成的二进制文件大小。
- 更新部署脚本,自动生成缺失的 proto 文件,并确保每次都重新构建 Docker 镜像。
- 扩展 .dockerignore 文件,增加对构建产物和开发工具的排除规则,优化项目结构。
This commit is contained in:
zhouyonggao 2025-12-20 15:09:18 +08:00
parent c402cc4ec5
commit aea29262a6
3 changed files with 66 additions and 24 deletions

View File

@ -1,12 +1,47 @@
# Git 相关
.git
.gitignore
.gitattributes
# 日志和存储
log/
storage/
server/bin/
*.log
server/log/
server/server.log
# 配置文件(运行时挂载)
server/config.yaml
server/config.test.yaml
server/config.prod.yaml
server/config_example.yaml
web/config.js
# 构建产物
server/bin/
server/server
*.tar
scripts/*.tar
# 注意:不要忽略以下文件,它们对构建很重要
# !go.work
# !go.work.sum
# !grpc/user/userv1/
marketing-system-data-tool.tar
# 开发工具
.idea/
.vscode/
*.swp
*.swo
*~
# 文档
README.md
docs/
*.md
# Docker 相关
Dockerfile
.dockerignore
docker-compose*.yml
# 其他
.env.local
.env
# 注意go.work.sum 需要保留Dockerfile 中会使用

View File

@ -1,8 +1,9 @@
FROM golang:1.25-alpine AS builder
FROM golang:1.21-alpine AS builder
ENV CGO_ENABLED=0
ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.cn,direct
RUN apk add --no-cache git tzdata
# 只安装构建所需的工具
RUN apk add --no-cache git
WORKDIR /app
# 复制 go.work 文件(用于工作区配置,必须在项目根目录)
@ -26,12 +27,21 @@ COPY server/ ./server/
COPY grpc/user/userv1/ ./grpc/user/userv1/
# 构建应用在项目根目录go.work 会自动生效)
# 使用 -ldflags 减小二进制文件大小,移除调试信息
WORKDIR /app/server
RUN go build -o /out/server ./cmd/server/main.go
RUN go build -ldflags="-w -s" -trimpath -o /out/server ./cmd/server/main.go
# 使用最小化的 alpine 镜像(包含时区支持,应用需要 loc=Local
FROM alpine:3.19
RUN apk add --no-cache tzdata ca-certificates curl bash
# 只安装运行时必需的包:时区和 CA 证书(用于可能的 HTTPS 连接)
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/*
WORKDIR /app
COPY --from=builder /out/server /app/server
EXPOSE 8077
USER appuser:appuser
ENTRYPOINT ["/app/server"]

View File

@ -20,25 +20,22 @@ fi
# 检查 proto 文件是否已生成(至少检查一个关键文件)
if [ ! -f "$ROOT_DIR/grpc/user/userv1/user.pb.go" ]; then
echo "警告: proto 文件未生成,构建可能会失败" >&2
echo "提示: 运行 'cd grpc && make generate' 生成 proto 文件" >&2
read -p "是否继续构建? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "proto 文件未生成,正在自动生成..." >&2
cd "$ROOT_DIR/grpc" && make generate
cd "$ROOT_DIR"
if [ ! -f "$ROOT_DIR/grpc/user/userv1/user.pb.go" ]; then
echo "错误: proto 文件生成失败" >&2
exit 1
fi
echo "proto 文件生成成功" >&2
fi
# 如果镜像存在,则直接使用;否则构建
if docker image inspect "$IMAGE:$TAG" >/dev/null 2>&1; then
echo "镜像 $IMAGE:$TAG 已存在,跳过构建。"
else
echo "开始构建 Docker 镜像..."
DOCKER_BUILDKIT=1 docker build \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--build-arg GOPROXY="${GOPROXY:-https://goproxy.cn,direct}" \
-t "$IMAGE:$TAG" -f Dockerfile .
fi
# 每次重新构建镜像
echo "开始构建 Docker 镜像..."
DOCKER_BUILDKIT=1 docker build \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--build-arg GOPROXY="${GOPROXY:-https://goproxy.cn,direct}" \
-t "$IMAGE:$TAG" -f Dockerfile .
USE_IMAGE="$IMAGE:$TAG"
mkdir -p log storage/export