Files
TaskPool/docker/Dockerfile
T

175 lines
5.6 KiB
Docker
Raw Normal View History

2026-02-06 22:01:17 +08:00
ARG BASE_TAG=base
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
# ================================
2026-01-26 21:42:06 +08:00
FROM --platform=$BUILDPLATFORM node:20-alpine AS frontend-builder
2025-12-20 09:30:16 +08:00
WORKDIR /app/web
# Copy package files
COPY web/package*.json ./
2026-04-01 11:44:49 +08:00
# Install dependencies using cache mount for npm
RUN --mount=type=cache,target=/root/.npm \
npm ci
2025-12-20 09:30:16 +08:00
# 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
2025-12-30 21:19:39 +08:00
# 生成版本信息文件,确保主服务和 Agent 使用相同的值(使用东八区时间)
RUN VERSION_VAL="${VERSION:-dev-$(TZ=Asia/Shanghai date '+%Y%m%d%H%M%S')}" && \
BUILD_TIME_VAL="${BUILD_TIME:-$(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S')}" && \
2025-12-29 20:11:48 +08:00
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
# ================================
2026-02-10 14:22:58 +08:00
FROM --platform=$BUILDPLATFORM golang:1.25 AS backend-builder
2025-12-20 12:00:08 +08:00
ARG TARGETOS
ARG TARGETARCH
2025-12-20 09:30:16 +08:00
2026-02-12 17:27:23 +08:00
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 ./
2026-04-01 11:44:49 +08:00
RUN go env -w GOPROXY=https://goproxy.cn,direct
# Using Go build cache mount can significantly speed up consecutive builds
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
2025-12-20 09:30:16 +08:00
2025-12-23 11:07:59 +08:00
# Copy backend source
2025-12-20 09:30:16 +08:00
COPY . .
2026-02-12 17:27:23 +08:00
# Copy frontend dist (needed for embedding)
2025-12-20 09:30:16 +08:00
COPY --from=frontend-builder /app/web/dist ./internal/static/dist
2026-04-01 11:44:49 +08:00
# Generate Swagger (Optional: better to commit docs and only copy, but if needed, install once)
# If openapi_docs exists, just use it, otherwise generate.
# Here we avoid go run @latest by copying it if you have it locally.
# If you don't, we install it once to the image.
RUN go install github.com/swaggo/swag/cmd/swag@latest
RUN swag init -g main.go -o ./openapi_docs
2026-03-05 22:22:46 +08:00
2026-04-01 11:44:49 +08:00
# Build Go binary using cache mounts for faster builds
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
VERSION_VAL=$(cat /build-info/version.txt) && \
2025-12-29 20:11:48 +08:00
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 github.com/engigu/baihu-panel/internal/constant.Version=${VERSION_VAL} -X 'github.com/engigu/baihu-panel/internal/constant.BuildTime=${BUILD_TIME_VAL}'" \
2026-02-12 19:09:41 +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)
# ================================
2026-02-10 14:22:58 +08:00
FROM --platform=$BUILDPLATFORM golang:1.25 AS agent-builder
2025-12-27 20:00:32 +08:00
2026-02-12 17:27:23 +08:00
2025-12-27 20:00:32 +08:00
WORKDIR /app
2025-12-29 20:11:48 +08:00
# Copy build info
COPY --from=build-info /build-info /build-info
2026-04-01 11:44:49 +08:00
# Using Go build cache mount
2026-04-01 11:57:51 +08:00
COPY go.mod go.sum ./
2026-04-01 11:44:49 +08:00
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
2026-02-07 21:59:25 +08:00
# Copy source needed for agent
COPY internal/ ./internal/
2025-12-27 20:00:32 +08:00
COPY agent/ ./agent/
2026-02-07 21:59:25 +08:00
# Build agent for all platforms and package as tar.gz
2025-12-27 20:00:32 +08:00
WORKDIR /app/agent
2026-04-01 11:44:49 +08:00
# Build agent with parallel-aware logic or just cache mount
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
VERSION_VAL=$(cat /build-info/version.txt) && \
2025-12-29 20:11:48 +08:00
BUILD_TIME_VAL=$(cat /build-info/build_time.txt) && \
2025-12-30 21:41:00 +08:00
LDFLAGS="-s -w -X 'main.Version=${VERSION_VAL}' -X 'main.BuildTime=${BUILD_TIME_VAL}'" && \
2025-12-29 19:57:23 +08:00
mkdir -p /opt/agent && \
echo "${VERSION_VAL}" > /opt/agent/version.txt && \
2026-02-12 17:27:23 +08:00
# Helper to build and compress
build_agent() { \
local os=$1; local arch=$2; local suffix=$3; \
CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -ldflags="${LDFLAGS}" -o baihu-agent$suffix . && \
tar -czvf /opt/agent/baihu-agent-$os-$arch.tar.gz baihu-agent$suffix config.example.ini && \
rm baihu-agent$suffix; \
} && \
build_agent linux amd64 "" && \
build_agent linux arm64 "" && \
build_agent darwin amd64 "" && \
build_agent darwin arm64 "" && \
2025-12-30 21:41:00 +08:00
echo "Agent build completed for all platforms"
2025-12-27 20:00:32 +08:00
# ================================
# Stage 4: Final image
2025-12-23 10:59:38 +08:00
# ================================
2026-02-06 22:01:17 +08:00
ARG BASE_TAG
2026-02-06 21:52:28 +08:00
FROM ghcr.io/engigu/baihu:${BASE_TAG}
2025-12-20 09:30:16 +08:00
ENV TZ=Asia/Shanghai
2026-02-10 16:50:02 +08:00
ENV LANG=C.UTF-8
ENV LC_ALL=C.UTF-8
2026-02-12 20:16:12 +08:00
ENV MISE_DATA_DIR=/app/envs/mise
ENV MISE_CONFIG_DIR=/app/envs/mise
2026-02-12 12:10:44 +08:00
ENV PATH="/app/envs/mise/shims:/app/envs/mise/bin:$PATH"
2025-12-20 09:30:16 +08:00
2026-01-11 21:44:08 +08:00
# # 安装必要系统工具 + Node + Python
# 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 \
# && echo "${TZ}" > /etc/timezone \
# && ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
# && apt-get update \
# && apt-get install -y --no-install-recommends \
# tzdata git gcc curl wget vim nodejs htop npm python3 python3-venv python3-pip \
# && 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 .
# Copy frontend assets to /www/baihu (No embed in Docker)
COPY --from=frontend-builder /app/web/dist /www/baihu
2025-12-23 10:59:38 +08:00
# Copy configs and entrypoint
COPY --from=backend-builder /app/configs ./configs
2026-03-19 18:08:26 +08:00
COPY --from=backend-builder /app/example ./example
2026-04-16 21:17:09 +08:00
COPY builtin/ /www/builtin
2026-02-06 21:37:35 +08:00
COPY docker/docker-entrypoint.sh .
2025-12-25 17:55:54 +08:00
2026-02-27 11:35:55 +08:00
2025-12-27 20:00:32 +08:00
# Copy agent binaries to /opt/agent
COPY --from=agent-builder /opt/agent /opt/agent
2026-03-23 21:20:35 +08:00
COPY docker/mise-hook.sh /etc/profile.d/mise-hook.sh
2026-02-27 11:35:55 +08:00
RUN chmod +x docker-entrypoint.sh \
2026-03-23 21:20:35 +08:00
&& cat /etc/profile.d/mise-hook.sh >> /etc/bash.bashrc \
2026-02-27 23:27:44 +08:00
&& echo "set encoding=utf-8" >> /etc/vim/vimrc \
&& ln -sf /app/baihu /usr/local/bin/baihu
2025-12-20 09:30:16 +08:00
EXPOSE 8052
2025-12-23 11:07:59 +08:00
ENTRYPOINT ["./docker-entrypoint.sh"]