Files
sundynix-pets/pets-be/Dockerfile
T
2026-10-01 13:26:34 +08:00

41 lines
1.7 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.
# 肉垫计划 · 后端 + 内嵌管理后台,单镜像。
# 多阶段:node 构建 admin 控制台 → Go 编译(go:embed 内嵌 admin 产物)→ alpine 运行。
# 一个容器同时 serve /api(小程序接口)和 /admin(管理后台 UI),无需单独的前端容器。
# 构建上下文为 pets-be/:
# docker build -f pets-be/Dockerfile -t sundynix/pets-be:latest pets-be
# ── 0. 构建后台管理前端 ──
FROM node:20-alpine AS admin
WORKDIR /app
COPY web/admin/package*.json ./
RUN npm config set registry https://registry.npmmirror.com && npm ci
COPY web/admin/ ./
# vite.config.ts 里的 outDir 指向仓库相对路径(../../internal/admin/dist),容器里不适用,
# 这里覆盖成 /app/dist,再由下一阶段拷进 embed 目录。
RUN npm run build -- --outDir dist --emptyOutDir
# ── 1. 编译 Go(内嵌 admin 产物)──
FROM golang:1.26-alpine AS build
WORKDIR /src
ENV GOFLAGS=-mod=mod GOPROXY=https://goproxy.cn,direct
# 先拷依赖清单分层缓存,源码变动时不必重下依赖
COPY go.mod go.sum ./
RUN go mod download
COPY . .
# 用真实构建产物覆盖 embed 目录(internal/admin/embed.go 里 //go:embed all:dist)
COPY --from=admin /app/dist ./internal/admin/dist
RUN go test ./...
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/pets-be ./cmd/server
# ── 2. 运行镜像 ──
# 二进制是 CGO_ENABLED=0 全静态;ca-certificates 供 TLS(DeepSeek/微信 API),
# tzdata + TZ 保证「今日任务 / 每日建议」按东八区算日期,非 root 用户运行。
FROM alpine:3.20
RUN apk add --no-cache ca-certificates tzdata \
&& adduser -D -u 10001 app
ENV TZ=Asia/Shanghai
USER app
COPY --from=build /out/pets-be /pets-be
EXPOSE 9090
ENTRYPOINT ["/pets-be"]