feat: adjust docker image build
This commit is contained in:
+35
-22
@@ -1,4 +1,6 @@
|
|||||||
|
# ================================
|
||||||
# Stage 1: Build frontend
|
# Stage 1: Build frontend
|
||||||
|
# ================================
|
||||||
FROM node:20-alpine AS frontend-builder
|
FROM node:20-alpine AS frontend-builder
|
||||||
|
|
||||||
WORKDIR /app/web
|
WORKDIR /app/web
|
||||||
@@ -15,59 +17,70 @@ COPY web/ ./
|
|||||||
# Build frontend
|
# Build frontend
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
|
# ================================
|
||||||
# Stage 2: Build backend
|
# Stage 2: Build backend
|
||||||
|
# ================================
|
||||||
FROM --platform=$BUILDPLATFORM golang:1.24-alpine AS backend-builder
|
FROM --platform=$BUILDPLATFORM golang:1.24-alpine AS backend-builder
|
||||||
|
|
||||||
ARG TARGETOS
|
ARG TARGETOS
|
||||||
ARG TARGETARCH
|
ARG TARGETARCH
|
||||||
|
ARG VERSION=dev
|
||||||
|
ARG BUILD_TIME
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Copy go mod files
|
|
||||||
COPY go.mod go.sum ./
|
COPY go.mod go.sum ./
|
||||||
RUN go env -w GOPROXY=https://goproxy.cn,direct && go mod download
|
RUN go env -w GOPROXY=https://goproxy.cn,direct && go mod download
|
||||||
|
|
||||||
# Copy source code
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
# Copy built frontend to embed location
|
# Copy frontend dist
|
||||||
COPY --from=frontend-builder /app/web/dist ./internal/static/dist
|
COPY --from=frontend-builder /app/web/dist ./internal/static/dist
|
||||||
|
|
||||||
# Build Go binary with cross-compilation (native speed, no QEMU)
|
# Build Go binary
|
||||||
ARG VERSION=dev
|
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
|
||||||
ARG BUILD_TIME
|
go build -ldflags="-s -w -X baihu/internal/constant.Version=${VERSION} -X 'baihu/internal/constant.BuildTime=${BUILD_TIME}'" \
|
||||||
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 .
|
-o baihu .
|
||||||
|
|
||||||
# Stage 3: Final image based on Dockerfile.debian
|
# ================================
|
||||||
|
# Stage 3: Final image
|
||||||
|
# ================================
|
||||||
FROM debian:bookworm-slim
|
FROM debian:bookworm-slim
|
||||||
|
|
||||||
ENV TZ=Asia/Shanghai
|
ENV TZ=Asia/Shanghai
|
||||||
|
ENV PATH="/opt/venv/bin:$PATH"
|
||||||
|
|
||||||
RUN sed -i 's@deb.debian.org@mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list.d/debian.sources \
|
# 安装必要系统工具 + Node + Python
|
||||||
&& sed -i 's@security.debian.org@mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list.d/debian.sources \
|
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 \
|
&& echo "${TZ}" > /etc/timezone \
|
||||||
&& ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
|
&& ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
|
||||||
&& apt update \
|
&& apt-get update \
|
||||||
&& apt install -y tzdata git gcc curl wget vim nodejs npm python3 python3-venv python3-pip \
|
&& apt-get install -y --no-install-recommends \
|
||||||
&& python3 -m venv /opt/basepy3 \
|
tzdata git gcc curl wget vim nodejs npm python3 python3-venv python3-pip \
|
||||||
&& source /opt/basepy3/bin/activate \
|
&& python3 -m venv /opt/venv \
|
||||||
&& rm -rf /var/lib/apt/lists/* \
|
&& /opt/venv/bin/pip install --upgrade pip \
|
||||||
&& python3 -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
|
&& /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
|
WORKDIR /app
|
||||||
|
|
||||||
# Copy binary from builder
|
# Copy Go binary
|
||||||
COPY --from=backend-builder /app/baihu .
|
COPY --from=backend-builder /app/baihu .
|
||||||
|
|
||||||
# Copy config files
|
# Copy frontend dist folder
|
||||||
COPY --from=backend-builder /app/configs ./configs
|
COPY --from=frontend-builder /app/web/dist ./internal/static/dist
|
||||||
|
|
||||||
# Copy entrypoint script
|
# Copy configs and entrypoint
|
||||||
|
COPY --from=backend-builder /app/configs ./configs
|
||||||
COPY docker-entrypoint.sh .
|
COPY docker-entrypoint.sh .
|
||||||
RUN chmod +x docker-entrypoint.sh
|
RUN chmod +x docker-entrypoint.sh
|
||||||
|
|
||||||
# Expose port
|
|
||||||
EXPOSE 8052
|
EXPOSE 8052
|
||||||
|
|
||||||
# Run with entrypoint
|
|
||||||
CMD ["./docker-entrypoint.sh"]
|
CMD ["./docker-entrypoint.sh"]
|
||||||
|
|||||||
Reference in New Issue
Block a user