Files
kefu_cloud/deploy/docker/Dockerfile.api
T

71 lines
2.1 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.
# 客服云 API — 生产镜像(多阶段构建)
# build context: 仓库根目录
# docker build -f deploy/docker/Dockerfile.api -t kefu-api:latest .
#
# 国内加速(默认开启):Alpine 腾讯云源 + goproxy.cn
# 注意:chai2010/webp 需要 CGO,必须装 gcc/musl-dev 且 CGO_ENABLED=1
# 海外构建可覆盖:
# docker build --build-arg ALPINE_MIRROR=dl-cdn.alpinelinux.org \
# --build-arg GOPROXY=https://proxy.golang.org,direct ...
FROM golang:1.26-alpine AS builder
ARG ALPINE_MIRROR=mirrors.cloud.tencent.com
ARG GOPROXY=https://goproxy.cn,direct
# go.mod 若要求更高版本,自动下载 toolchain
ENV GOTOOLCHAIN=auto
# webp(chai2010)含 C 代码,关闭 CGO 会 undefined: webpEncode*
ENV CGO_ENABLED=1
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 gcc musl-dev
WORKDIR /src
# 先下依赖,利用缓存
COPY server/go.mod server/go.sum ./
RUN go mod download
COPY server/ ./
# 静态链接,最终 alpine 镜像无需再装 libwebp / gcc
RUN GOOS=linux GOARCH=amd64 \
go build -trimpath \
-ldflags="-s -w -linkmode external -extldflags '-static'" \
-o /out/server ./cmd/main.go
RUN GOOS=linux GOARCH=amd64 \
go build -trimpath \
-ldflags="-s -w -linkmode external -extldflags '-static'" \
-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"]