Files
tyapi-server/Dockerfile

84 lines
2.2 KiB
Docker
Raw Normal View History

2025-07-02 16:17:59 +08:00
# 第一阶段:构建阶段
2025-07-28 01:46:39 +08:00
FROM golang:1.23.4-alpine AS builder
2025-07-02 16:17:59 +08:00
2025-07-28 02:07:53 +08:00
# 设置Go代理和Alpine镜像源
ENV GOPROXY https://goproxy.cn,direct
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
2025-07-02 16:17:59 +08:00
# 设置工作目录
WORKDIR /app
# 安装必要的包
2025-07-28 02:07:53 +08:00
RUN apk add --no-cache git tzdata
2025-07-02 16:17:59 +08:00
# 复制模块文件
COPY go.mod go.sum ./
# 下载依赖
RUN go mod download
# 复制源代码
COPY . .
# 构建应用程序
ARG VERSION=1.0.0
ARG COMMIT=dev
ARG BUILD_TIME
RUN BUILD_TIME=${BUILD_TIME:-$(date -u +"%Y-%m-%dT%H:%M:%SZ")} && \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags "-X main.version=${VERSION} -X main.commit=${COMMIT} -X main.date=${BUILD_TIME} -w -s" \
-a -installsuffix cgo \
-o tyapi-server \
cmd/api/main.go
2025-12-04 10:41:07 +08:00
# 创建字体文件 tar 包(如果字体文件存在)
RUN if [ -d internal/shared/pdf/fonts ] && [ "$(ls -A internal/shared/pdf/fonts 2>/dev/null)" ]; then \
tar czf /tmp/fonts.tar.gz -C internal/shared/pdf/fonts .; \
else \
touch /tmp/fonts.tar.gz; \
fi
2025-07-02 16:17:59 +08:00
# 第二阶段:运行阶段
FROM alpine:3.19
2025-07-28 02:07:53 +08:00
# 设置Alpine镜像源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
2025-07-02 16:17:59 +08:00
# 安装必要的包
2025-07-28 02:07:53 +08:00
RUN apk --no-cache add tzdata curl
2025-07-02 16:17:59 +08:00
# 设置时区
ENV TZ=Asia/Shanghai
# 设置工作目录
WORKDIR /app
# 从构建阶段复制二进制文件
COPY --from=builder /app/tyapi-server .
# 复制配置文件
2025-12-03 18:25:04 +08:00
COPY config.yaml .
COPY configs/ ./configs/
2025-12-04 10:41:07 +08:00
# 复制字体文件PDF生成需要
2025-12-03 18:25:04 +08:00
RUN mkdir -p ./internal/shared/pdf/fonts
2025-12-04 10:41:07 +08:00
# 从 builder 阶段复制字体 tar 包并解压(如果存在字体文件)
COPY --from=builder /tmp/fonts.tar.gz /tmp/fonts.tar.gz
RUN if [ -s /tmp/fonts.tar.gz ]; then \
tar xzf /tmp/fonts.tar.gz -C ./internal/shared/pdf/fonts/ && \
rm /tmp/fonts.tar.gz; \
else \
echo "字体文件不存在,将在运行时通过 volume 挂载或手动复制"; \
rm /tmp/fonts.tar.gz; \
fi
2025-07-02 16:17:59 +08:00
# 暴露端口
EXPOSE 8080
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1
# 启动命令
CMD ["./tyapi-server", "-env=production"]