Files
TaskPool/Dockerfile
T
2025-12-23 10:59:38 +08:00

87 lines
2.5 KiB
Docker

# ================================
# Stage 1: Build frontend
# ================================
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
# ================================
# Stage 2: Build backend
# ================================
FROM --platform=$BUILDPLATFORM golang:1.24-alpine AS backend-builder
ARG TARGETOS
ARG TARGETARCH
ARG VERSION=dev
ARG BUILD_TIME
WORKDIR /app
COPY go.mod go.sum ./
RUN go env -w GOPROXY=https://goproxy.cn,direct && go mod download
COPY . .
# Copy frontend dist
COPY --from=frontend-builder /app/web/dist ./internal/static/dist
# Build Go binary
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
go build -ldflags="-s -w -X baihu/internal/constant.Version=${VERSION} -X 'baihu/internal/constant.BuildTime=${BUILD_TIME}'" \
-o baihu .
# ================================
# Stage 3: Final image
# ================================
FROM debian:bookworm-slim
ENV TZ=Asia/Shanghai
ENV PATH="/opt/venv/bin:$PATH"
# 安装必要系统工具 + Node + Python
RUN sed -i 's@deb.debian.org@mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list \
&& sed -i 's@security.debian.org@mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list \
&& 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 npm python3 python3-venv python3-pip \
&& python3 -m venv /opt/venv \
&& /opt/venv/bin/pip install --upgrade pip \
&& /opt/venv/bin/pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple \
# 设置虚拟环境 Python 为系统默认
&& update-alternatives --install /usr/bin/python python /opt/venv/bin/python 1 \
&& update-alternatives --install /usr/bin/python3 python3 /opt/venv/bin/python 1 \
&& update-alternatives --install /usr/bin/pip pip /opt/venv/bin/pip 1 \
&& update-alternatives --install /usr/bin/pip3 pip3 /opt/venv/bin/pip 1 \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
# Copy Go binary
COPY --from=backend-builder /app/baihu .
# Copy frontend dist folder
COPY --from=frontend-builder /app/web/dist ./internal/static/dist
# Copy configs and entrypoint
COPY --from=backend-builder /app/configs ./configs
COPY docker-entrypoint.sh .
RUN chmod +x docker-entrypoint.sh
EXPOSE 8052
CMD ["./docker-entrypoint.sh"]