Files
backend_v2/Dockerfile
toom1996 5db90af73a update
2026-10-19 00:33:25 +08:00

45 lines
1.5 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.

# ---- 构建阶段 ----
FROM golang:1.26-alpine AS build
WORKDIR /src
# 国内构建默认走 goproxy.cn:服务器上没有本地模块缓存,直连 proxy.golang.org 常常超时。
# 换环境时用 --build-arg GOPROXY=https://proxy.golang.org,direct 覆盖。
ARG GOPROXY=https://goproxy.cn,direct
ENV GOPROXY=$GOPROXY
# 先拉依赖,利用层缓存(改业务代码时不会重复下载模块)
COPY go.mod go.sum ./
RUN go mod download
COPY . .
# -trimpath 去掉构建机的绝对路径;-s -w 去掉符号表与调试信息(约 50MB -> 32MB)
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags "-s -w" -o /out/server ./cmd/server
# ---- 运行阶段 ----
FROM alpine:3.20
RUN apk add --no-cache ca-certificates tzdata
WORKDIR /app
COPY --from=build /out/server /app/server
# 模板与后台静态资源已用 go:embed 打进二进制,这里只需要配置
COPY configs ./configs
# 本地存储兜底目录:启用 S4 时图片在对象存储,此目录只承接临时/兜底文件
RUN mkdir -p /app/uploads
ENV CONFIG_PATH=/app/configs/config.yml \
GIN_MODE=release \
UPLOAD_DIR=/app/uploads
# 三个端口的暴露面(详见 README):
# 8090 公开 API(生产由前端 nginx 同源反代)
# 8091 SSG 构建期内部接口(只绑回环,绝不对外)
# 8092 管理后台(只绑回环)
EXPOSE 8090 8091 8092
# busybox 自带 wget,无需额外安装
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
CMD wget -qO- http://127.0.0.1:8090/api/health || exit 1
CMD ["/app/server"]