Files
zt-closet-backend/scripts/dockerfile/Dockerfile.migrate
T
2026-09-12 10:00:53 +08:00

44 lines
1.8 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 迁移镜像:只携带 Go 工具链与源码,构建期不产出二进制。
# 真正的编译发生在容器启动时(go run),因此镜像内保留完整源码与模块缓存。
ARG GO_VERSION=1.27
FROM golang:${GO_VERSION}-alpine
# Go 模块代理:默认官方代理;国内或受限网络可覆盖:
# --build-arg GOPROXY=https://goproxy.cn,direct
ARG GOPROXY=https://proxy.golang.org,direct
ENV GOPROXY=${GOPROXY}
# alpine 为 musl libc,关闭 CGO 避免编译期依赖 gcc;二进制为纯静态。
ENV CGO_ENABLED=0
ENV TZ=Asia/Shanghai
# ca-certificates:MySQL 启用 TLS 时需要根证书;tzdata:配合 TZ 输出本地时区日志。
RUN apk add --no-cache ca-certificates tzdata
# 工作目录必须是 /app:setting 通过 viper 的 AddConfigPath("configs/") 定位配置,
# 该路径相对工作目录解析,配置须挂载到 /app/configs/config.yaml。
WORKDIR /app
# 先只复制依赖清单,下载模块;源码未变动时复用这一层缓存。
COPY go.mod go.sum ./
RUN go mod download
# 源码进入镜像。迁移脚本已由 pkg/migration 通过 go:embed 打包,无需单独 COPY sql 文件。
COPY . .
# configs/:配置挂载点(config.yaml 含密钥,由 .dockerignore 排除,不进镜像层)。
RUN mkdir -p /app/configs
# 预热 Go 构建缓存,缩短容器启动后 go run 的等待时间。
# 这一步不产出任何二进制;失败也不影响镜像构建(|| true)。
# 缓存目录为默认的 /root/.cache/go-build,可挂载卷在多次运行间复用。
RUN go build -o /dev/null ./cmd/migrate || true
# 用法:
# docker run --rm --network host \
# -v <宿主目录>/configs:/app/configs \
# -v <缓存卷>:/root/.cache/go-build \
# <image> up|down|steps N|version|force V
ENTRYPOINT ["go", "run", "./cmd/migrate"]