Files
TaskPool/Dockerfile
T

136 lines
4.0 KiB
Docker
Raw Normal View History

2025-12-23 10:59:38 +08:00
# ================================
2025-12-20 09:30:16 +08:00
# Stage 1: Build frontend
2025-12-23 10:59:38 +08:00
# ================================
2025-12-20 09:30:16 +08:00
FROM node:20-alpine AS frontend-builder
WORKDIR /app/web
# Copy package files
COPY web/package*.json ./
# Install dependencies
RUN npm ci
# Copy frontend source
COPY web/ ./
# Build frontend
RUN npm run build
2025-12-29 20:11:48 +08:00
# ================================
# Stage 0: Generate build info
# ================================
FROM alpine:3.19 AS build-info
ARG VERSION
ARG BUILD_TIME
# 生成版本信息文件,确保主服务和 Agent 使用相同的值
RUN VERSION_VAL="${VERSION:-dev-$(date '+%Y%m%d%H%M%S')}" && \
BUILD_TIME_VAL="${BUILD_TIME:-$(date '+%Y-%m-%d %H:%M:%S')}" && \
mkdir -p /build-info && \
echo "${VERSION_VAL}" > /build-info/version.txt && \
echo "${BUILD_TIME_VAL}" > /build-info/build_time.txt
2025-12-23 10:59:38 +08:00
# ================================
2025-12-20 09:30:16 +08:00
# Stage 2: Build backend
2025-12-23 10:59:38 +08:00
# ================================
2025-12-20 12:00:08 +08:00
FROM --platform=$BUILDPLATFORM golang:1.24-alpine AS backend-builder
ARG TARGETOS
ARG TARGETARCH
2025-12-20 09:30:16 +08:00
WORKDIR /app
2025-12-29 20:11:48 +08:00
# Copy build info
COPY --from=build-info /build-info /build-info
2025-12-23 11:07:59 +08:00
# Go mod files
2025-12-20 09:30:16 +08:00
COPY go.mod go.sum ./
RUN go env -w GOPROXY=https://goproxy.cn,direct && go mod download
2025-12-23 11:07:59 +08:00
# Copy backend source
2025-12-20 09:30:16 +08:00
COPY . .
2025-12-23 10:59:38 +08:00
# Copy frontend dist
2025-12-20 09:30:16 +08:00
COPY --from=frontend-builder /app/web/dist ./internal/static/dist
2025-12-29 20:11:48 +08:00
# Build Go binary
RUN VERSION_VAL=$(cat /build-info/version.txt) && \
BUILD_TIME_VAL=$(cat /build-info/build_time.txt) && \
2025-12-29 19:57:23 +08:00
CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
go build -ldflags="-s -w -X baihu/internal/constant.Version=${VERSION_VAL} -X 'baihu/internal/constant.BuildTime=${BUILD_TIME_VAL}'" \
2025-12-23 10:59:38 +08:00
-o baihu .
2025-12-20 09:30:16 +08:00
2025-12-23 10:59:38 +08:00
# ================================
2025-12-27 20:00:32 +08:00
# Stage 3: Build Agent (all platforms)
# ================================
FROM --platform=$BUILDPLATFORM golang:1.24-alpine AS agent-builder
WORKDIR /app
2025-12-29 20:11:48 +08:00
# Copy build info
COPY --from=build-info /build-info /build-info
2025-12-27 20:43:48 +08:00
# Copy agent source and config example
2025-12-27 20:00:32 +08:00
COPY agent/ ./agent/
# Download dependencies
WORKDIR /app/agent
RUN go env -w GOPROXY=https://goproxy.cn,direct && go mod download
2025-12-29 20:11:48 +08:00
# Build agent for all platforms and package as tar.gz
RUN VERSION_VAL=$(cat /build-info/version.txt) && \
BUILD_TIME_VAL=$(cat /build-info/build_time.txt) && \
2025-12-29 19:57:23 +08:00
mkdir -p /opt/agent && \
echo "${VERSION_VAL}" > /opt/agent/version.txt && \
2025-12-27 20:43:48 +08:00
# Linux amd64
2025-12-29 19:57:23 +08:00
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w -X 'main.Version=${VERSION_VAL}' -X 'main.BuildTime=${BUILD_TIME_VAL}'" -o baihu-agent . && \
2025-12-27 20:43:48 +08:00
tar -czvf /opt/agent/baihu-agent-linux-amd64.tar.gz baihu-agent config.example.ini && rm baihu-agent && \
2025-12-29 19:57:23 +08:00
echo "Agent build completed"
2025-12-27 20:00:32 +08:00
# ================================
# Stage 4: Final image
2025-12-23 10:59:38 +08:00
# ================================
2025-12-20 09:30:16 +08:00
FROM debian:bookworm-slim
ENV TZ=Asia/Shanghai
2025-12-23 22:12:27 +08:00
ENV PATH="/app/envs/node/bin:/app/envs/python/bin:$PATH"
ENV NODE_PATH="/app/envs/node/lib/node_modules"
2025-12-20 09:30:16 +08:00
2025-12-23 10:59:38 +08:00
# 安装必要系统工具 + Node + Python
2025-12-23 11:10:05 +08:00
RUN sed -i 's@deb.debian.org@mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list.d/debian.sources \
&& sed -i 's@security.debian.org@mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list.d/debian.sources \
2025-12-20 09:30:16 +08:00
&& echo "${TZ}" > /etc/timezone \
&& ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
2025-12-23 10:59:38 +08:00
&& apt-get update \
&& apt-get install -y --no-install-recommends \
2025-12-23 18:33:54 +08:00
tzdata git gcc curl wget vim nodejs htop npm python3 python3-venv python3-pip \
2025-12-23 10:59:38 +08:00
&& rm -rf /var/lib/apt/lists/*
2025-12-20 09:30:16 +08:00
WORKDIR /app
2025-12-23 10:59:38 +08:00
# Copy Go binary
2025-12-20 09:30:16 +08:00
COPY --from=backend-builder /app/baihu .
2025-12-23 10:59:38 +08:00
# Copy frontend dist folder
COPY --from=frontend-builder /app/web/dist ./internal/static/dist
2025-12-20 09:30:16 +08:00
2025-12-23 10:59:38 +08:00
# Copy configs and entrypoint
COPY --from=backend-builder /app/configs ./configs
2025-12-21 10:23:25 +08:00
COPY docker-entrypoint.sh .
2025-12-25 17:55:54 +08:00
# Copy sync.py to /opt
2025-12-27 16:43:42 +08:00
COPY custom/sync.py /opt/sync.py
2025-12-27 20:00:32 +08:00
# Copy agent binaries to /opt/agent
COPY --from=agent-builder /opt/agent /opt/agent
2025-12-25 17:55:54 +08:00
RUN chmod +x /opt/sync.py \
&& chmod +x docker-entrypoint.sh \
2025-12-25 18:13:07 +08:00
&& touch "dont-not-delete-anythings" \
&& echo "set encoding=utf-8" >> /etc/vim/vimrc
2025-12-20 09:30:16 +08:00
EXPOSE 8052
2025-12-23 11:07:59 +08:00
ENTRYPOINT ["./docker-entrypoint.sh"]