Files
kefu_cloud/deploy/docker/Dockerfile.api
T
2026-07-15 16:09:05 +08:00

65 lines
1.8 KiB
Docker

# 客服云 API — 生产镜像(多阶段构建)
# build context: 仓库根目录
# docker build -f deploy/docker/Dockerfile.api -t kefu-api:latest .
#
# 国内加速(默认开启):Alpine 清华源 + goproxy.cn
# 海外构建可覆盖:
# docker build --build-arg ALPINE_MIRROR=dl-cdn.alpinelinux.org \
# --build-arg GOPROXY=https://proxy.golang.org,direct ...
FROM golang:1.24-alpine AS builder
ARG ALPINE_MIRROR=mirrors.cloud.tencent.com
ARG GOPROXY=https://goproxy.cn,direct
# 若 go.mod 要求更高版本,自动下载 toolchain
ENV GOTOOLCHAIN=auto
ENV CGO_ENABLED=0
ENV GOPROXY=${GOPROXY}
ENV GOSUMDB=sum.golang.google.cn
RUN set -eux; \
if [ -n "${ALPINE_MIRROR}" ]; then \
sed -i "s#dl-cdn.alpinelinux.org#${ALPINE_MIRROR}#g" /etc/apk/repositories; \
fi; \
apk add --no-cache git ca-certificates tzdata
WORKDIR /src
# 先下依赖,利用缓存
COPY server/go.mod server/go.sum ./
RUN go mod download
COPY server/ ./
RUN GOOS=linux GOARCH=amd64 \
go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/main.go
# 种子程序(compose profile seed 使用)
RUN GOOS=linux GOARCH=amd64 \
go build -trimpath -ldflags="-s -w" -o /out/seed ./cmd/seed
FROM alpine:3.21
ARG ALPINE_MIRROR=mirrors.cloud.tencent.com
RUN set -eux; \
if [ -n "${ALPINE_MIRROR}" ]; then \
sed -i "s#dl-cdn.alpinelinux.org#${ALPINE_MIRROR}#g" /etc/apk/repositories; \
fi; \
apk add --no-cache ca-certificates tzdata wget \
&& adduser -D -H -u 10001 kefu
ENV TZ=Asia/Shanghai
WORKDIR /app
COPY --from=builder /out/server /app/server
COPY --from=builder /out/seed /app/seed
USER kefu
EXPOSE 8080
HEALTHCHECK --interval=15s --timeout=5s --start-period=20s --retries=5 \
CMD wget -qO- http://127.0.0.1:8080/health || exit 1
ENTRYPOINT ["/app/server"]