feat: smaller imaege szie
This commit is contained in:
+21
-22
@@ -41,6 +41,9 @@ FROM --platform=$BUILDPLATFORM golang:1.25 AS backend-builder
|
|||||||
ARG TARGETOS
|
ARG TARGETOS
|
||||||
ARG TARGETARCH
|
ARG TARGETARCH
|
||||||
|
|
||||||
|
# Install UPX
|
||||||
|
RUN apt-get update && apt-get install -y upx-ucl && rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Copy build info
|
# Copy build info
|
||||||
@@ -53,7 +56,7 @@ RUN go env -w GOPROXY=https://goproxy.cn,direct && go mod download
|
|||||||
# Copy backend source
|
# Copy backend source
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
# Copy frontend dist
|
# Copy frontend dist (needed for embedding)
|
||||||
COPY --from=frontend-builder /app/web/dist ./internal/static/dist
|
COPY --from=frontend-builder /app/web/dist ./internal/static/dist
|
||||||
|
|
||||||
# Build Go binary
|
# Build Go binary
|
||||||
@@ -61,13 +64,17 @@ RUN VERSION_VAL=$(cat /build-info/version.txt) && \
|
|||||||
BUILD_TIME_VAL=$(cat /build-info/build_time.txt) && \
|
BUILD_TIME_VAL=$(cat /build-info/build_time.txt) && \
|
||||||
CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
|
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}'" \
|
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}'" \
|
||||||
-o baihu .
|
-o baihu . && \
|
||||||
|
upx --best --lzma baihu
|
||||||
|
|
||||||
# ================================
|
# ================================
|
||||||
# Stage 3: Build Agent (all platforms)
|
# Stage 3: Build Agent (all platforms)
|
||||||
# ================================
|
# ================================
|
||||||
FROM --platform=$BUILDPLATFORM golang:1.25 AS agent-builder
|
FROM --platform=$BUILDPLATFORM golang:1.25 AS agent-builder
|
||||||
|
|
||||||
|
# Install UPX
|
||||||
|
RUN apt-get update && apt-get install -y upx-ucl && rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Copy build info
|
# Copy build info
|
||||||
@@ -84,27 +91,23 @@ COPY agent/ ./agent/
|
|||||||
# Build agent for all platforms and package as tar.gz
|
# Build agent for all platforms and package as tar.gz
|
||||||
WORKDIR /app/agent
|
WORKDIR /app/agent
|
||||||
|
|
||||||
# Build agent for all platforms and package as tar.gz
|
|
||||||
RUN VERSION_VAL=$(cat /build-info/version.txt) && \
|
RUN VERSION_VAL=$(cat /build-info/version.txt) && \
|
||||||
BUILD_TIME_VAL=$(cat /build-info/build_time.txt) && \
|
BUILD_TIME_VAL=$(cat /build-info/build_time.txt) && \
|
||||||
LDFLAGS="-s -w -X 'main.Version=${VERSION_VAL}' -X 'main.BuildTime=${BUILD_TIME_VAL}'" && \
|
LDFLAGS="-s -w -X 'main.Version=${VERSION_VAL}' -X 'main.BuildTime=${BUILD_TIME_VAL}'" && \
|
||||||
mkdir -p /opt/agent && \
|
mkdir -p /opt/agent && \
|
||||||
echo "${VERSION_VAL}" > /opt/agent/version.txt && \
|
echo "${VERSION_VAL}" > /opt/agent/version.txt && \
|
||||||
# Linux amd64
|
# Helper to build and compress
|
||||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o baihu-agent . && \
|
build_agent() { \
|
||||||
tar -czvf /opt/agent/baihu-agent-linux-amd64.tar.gz baihu-agent config.example.ini && rm baihu-agent && \
|
local os=$1; local arch=$2; local suffix=$3; \
|
||||||
# Linux arm64
|
CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -ldflags="${LDFLAGS}" -o baihu-agent$suffix . && \
|
||||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="${LDFLAGS}" -o baihu-agent . && \
|
upx --best --lzma baihu-agent$suffix && \
|
||||||
tar -czvf /opt/agent/baihu-agent-linux-arm64.tar.gz baihu-agent config.example.ini && rm baihu-agent && \
|
tar -czvf /opt/agent/baihu-agent-$os-$arch.tar.gz baihu-agent$suffix config.example.ini && \
|
||||||
# macOS amd64
|
rm baihu-agent$suffix; \
|
||||||
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o baihu-agent . && \
|
} && \
|
||||||
tar -czvf /opt/agent/baihu-agent-darwin-amd64.tar.gz baihu-agent config.example.ini && rm baihu-agent && \
|
build_agent linux amd64 "" && \
|
||||||
# macOS arm64
|
build_agent linux arm64 "" && \
|
||||||
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="${LDFLAGS}" -o baihu-agent . && \
|
build_agent darwin amd64 "" && \
|
||||||
tar -czvf /opt/agent/baihu-agent-darwin-arm64.tar.gz baihu-agent config.example.ini && rm baihu-agent && \
|
build_agent darwin arm64 "" && \
|
||||||
# Windows amd64 (暂时注释)
|
|
||||||
# CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o baihu-agent.exe . && \
|
|
||||||
# tar -czvf /opt/agent/baihu-agent-windows-amd64.tar.gz baihu-agent.exe config.example.ini && rm baihu-agent.exe && \
|
|
||||||
echo "Agent build completed for all platforms"
|
echo "Agent build completed for all platforms"
|
||||||
# ================================
|
# ================================
|
||||||
# Stage 4: Final image
|
# Stage 4: Final image
|
||||||
@@ -132,9 +135,6 @@ WORKDIR /app
|
|||||||
# Copy Go binary
|
# Copy Go binary
|
||||||
COPY --from=backend-builder /app/baihu .
|
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 configs and entrypoint
|
||||||
COPY --from=backend-builder /app/configs ./configs
|
COPY --from=backend-builder /app/configs ./configs
|
||||||
COPY docker/docker-entrypoint.sh .
|
COPY docker/docker-entrypoint.sh .
|
||||||
@@ -147,7 +147,6 @@ COPY --from=agent-builder /opt/agent /opt/agent
|
|||||||
|
|
||||||
RUN chmod +x /opt/sync.py \
|
RUN chmod +x /opt/sync.py \
|
||||||
&& chmod +x docker-entrypoint.sh \
|
&& chmod +x docker-entrypoint.sh \
|
||||||
&& touch "dont-not-delete-anythings" \
|
|
||||||
&& echo "set encoding=utf-8" >> /etc/vim/vimrc
|
&& echo "set encoding=utf-8" >> /etc/vim/vimrc
|
||||||
|
|
||||||
EXPOSE 8052
|
EXPOSE 8052
|
||||||
|
|||||||
@@ -7,21 +7,16 @@ ENV PATH="/app/envs/mise/shims:/app/envs/mise/bin:$PATH"
|
|||||||
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \
|
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \
|
||||||
&& apk add --no-cache \
|
&& apk add --no-cache \
|
||||||
tzdata git bash curl wget vim htop ca-certificates rsync \
|
tzdata git bash curl wget vim htop ca-certificates rsync \
|
||||||
python3 \
|
&& BUILD_DEPS="build-base libffi-dev openssl-dev bzip2-dev zlib-dev readline-dev sqlite-dev ncurses-dev xz-dev" \
|
||||||
# Python build dependencies
|
&& apk add --no-cache $BUILD_DEPS \
|
||||||
build-base libffi-dev openssl-dev bzip2-dev zlib-dev readline-dev sqlite-dev ncurses-dev xz-dev \
|
&& curl https://mise.run | MISE_INSTALL_PATH=/usr/local/bin/mise sh \
|
||||||
|
&& MISE_DATA_DIR=/opt/mise-base MISE_CONFIG_DIR=/opt/mise-base MISE_HTTP_TIMEOUT=300 \
|
||||||
|
PYTHON_BUILD_MIRROR_URL=https://pyenv.pages.dev/api/mirror/binaries \
|
||||||
|
(mise use -g node@22 python@3.12 || mise use -g node@22 python@3.12) \
|
||||||
|
&& mise prune -- -f \
|
||||||
|
&& apk del $BUILD_DEPS \
|
||||||
&& ln -sf /usr/bin/python3 /usr/bin/python \
|
&& ln -sf /usr/bin/python3 /usr/bin/python \
|
||||||
&& cp /usr/share/zoneinfo/${TZ} /etc/localtime \
|
&& cp /usr/share/zoneinfo/${TZ} /etc/localtime \
|
||||||
&& echo "${TZ}" > /etc/timezone \
|
&& echo "${TZ}" > /etc/timezone \
|
||||||
&& rm -rf /var/cache/apk/*
|
&& rm -rf /var/cache/apk/* \
|
||||||
|
&& rm -rf /root/.cache/mise /root/.cache/pip /opt/mise-base/cache/*
|
||||||
# 安装 mise
|
|
||||||
RUN curl https://mise.run | MISE_INSTALL_PATH=/usr/local/bin/mise sh
|
|
||||||
|
|
||||||
# 使用 mise 安装运行时
|
|
||||||
ENV MISE_DATA_DIR=/opt/mise-base
|
|
||||||
ENV MISE_CONFIG_DIR=/opt/mise-base
|
|
||||||
ENV MISE_HTTP_TIMEOUT=300
|
|
||||||
ENV PYTHON_BUILD_MIRROR_URL=https://pyenv.pages.dev/api/mirror/binaries
|
|
||||||
RUN (mise use -g node@22 python@3.12 || mise use -g node@22 python@3.12) \
|
|
||||||
&& mise prune -- -f
|
|
||||||
|
|||||||
+12
-16
@@ -10,20 +10,16 @@ RUN sed -i 's@deb.debian.org@mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.li
|
|||||||
&& ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
|
&& ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
|
||||||
&& apt-get update \
|
&& apt-get update \
|
||||||
&& apt-get install -y --no-install-recommends \
|
&& apt-get install -y --no-install-recommends \
|
||||||
tzdata git gcc curl wget vim htop ca-certificates rsync \
|
tzdata git curl wget vim htop ca-certificates rsync \
|
||||||
build-essential libssl-dev zlib1g-dev libbz2-dev \
|
&& BUILD_DEPS="gcc build-essential libssl-dev zlib1g-dev libbz2-dev \
|
||||||
libreadline-dev libsqlite3-dev libncursesw5-dev \
|
libreadline-dev libsqlite3-dev libncursesw5-dev \
|
||||||
xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev \
|
xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev" \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& apt-get install -y --no-install-recommends $BUILD_DEPS \
|
||||||
|
&& curl https://mise.run | MISE_INSTALL_PATH=/usr/local/bin/mise sh \
|
||||||
# 安装 mise
|
&& MISE_DATA_DIR=/opt/mise-base MISE_CONFIG_DIR=/opt/mise-base MISE_HTTP_TIMEOUT=300 \
|
||||||
RUN curl https://mise.run | MISE_INSTALL_PATH=/usr/local/bin/mise sh
|
PYTHON_BUILD_MIRROR_URL=https://pyenv.pages.dev/api/mirror/binaries \
|
||||||
|
(mise use -g node@22 python@3.12 || mise use -g node@22 python@3.12) \
|
||||||
# 使用 mise 安装 Node.js 和 Python
|
&& mise prune -- -f \
|
||||||
ENV MISE_DATA_DIR=/opt/mise-base
|
&& apt-get purge -y --auto-remove $BUILD_DEPS \
|
||||||
ENV MISE_CONFIG_DIR=/opt/mise-base
|
&& rm -rf /var/lib/apt/lists/* \
|
||||||
ENV MISE_HTTP_TIMEOUT=300
|
&& rm -rf /root/.cache/mise /root/.cache/pip /opt/mise-base/cache/*
|
||||||
# 使用镜像源加速 Python 编译下载
|
|
||||||
ENV PYTHON_BUILD_MIRROR_URL=https://pyenv.pages.dev/api/mirror/binaries
|
|
||||||
RUN (mise use -g node@22 python@3.12 || mise use -g node@22 python@3.12) \
|
|
||||||
&& mise prune -- -f
|
|
||||||
|
|||||||
@@ -10,21 +10,17 @@ RUN sed -i 's@deb.debian.org@mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.li
|
|||||||
&& ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
|
&& ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
|
||||||
&& apt-get update \
|
&& apt-get update \
|
||||||
&& apt-get install -y --no-install-recommends \
|
&& apt-get install -y --no-install-recommends \
|
||||||
tzdata git gcc curl wget vim htop ca-certificates rsync \
|
tzdata git curl wget vim htop ca-certificates rsync \
|
||||||
python3-full \
|
&& BUILD_DEPS="gcc build-essential libssl-dev zlib1g-dev libbz2-dev \
|
||||||
build-essential libssl-dev zlib1g-dev libbz2-dev \
|
|
||||||
libreadline-dev libsqlite3-dev libncursesw5-dev \
|
libreadline-dev libsqlite3-dev libncursesw5-dev \
|
||||||
xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev \
|
xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev" \
|
||||||
|
&& apt-get install -y --no-install-recommends $BUILD_DEPS \
|
||||||
|
&& curl https://mise.run | MISE_INSTALL_PATH=/usr/local/bin/mise sh \
|
||||||
|
&& MISE_DATA_DIR=/opt/mise-base MISE_CONFIG_DIR=/opt/mise-base MISE_HTTP_TIMEOUT=300 \
|
||||||
|
PYTHON_BUILD_MIRROR_URL=https://pyenv.pages.dev/api/mirror/binaries \
|
||||||
|
(mise use -g node@22 python@3.12 || mise use -g node@22 python@3.12) \
|
||||||
|
&& mise prune -- -f \
|
||||||
|
&& apt-get purge -y --auto-remove $BUILD_DEPS \
|
||||||
&& ln -sf /usr/bin/python3 /usr/bin/python \
|
&& ln -sf /usr/bin/python3 /usr/bin/python \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/* \
|
||||||
|
&& rm -rf /root/.cache/mise /root/.cache/pip /opt/mise-base/cache/*
|
||||||
# 安装 mise
|
|
||||||
RUN curl https://mise.run | MISE_INSTALL_PATH=/usr/local/bin/mise sh
|
|
||||||
|
|
||||||
# 使用 mise 安装运行时
|
|
||||||
ENV MISE_DATA_DIR=/opt/mise-base
|
|
||||||
ENV MISE_CONFIG_DIR=/opt/mise-base
|
|
||||||
ENV MISE_HTTP_TIMEOUT=300
|
|
||||||
ENV PYTHON_BUILD_MIRROR_URL=https://pyenv.pages.dev/api/mirror/binaries
|
|
||||||
RUN (mise use -g node@22 python@3.12 || mise use -g node@22 python@3.12) \
|
|
||||||
&& mise prune -- -f
|
|
||||||
|
|||||||
+20
-4
@@ -14,18 +14,34 @@ func GetShell() (shell string, args []string) {
|
|||||||
|
|
||||||
// 优先使用环境变量中的 SHELL
|
// 优先使用环境变量中的 SHELL
|
||||||
if envShell := os.Getenv("SHELL"); envShell != "" {
|
if envShell := os.Getenv("SHELL"); envShell != "" {
|
||||||
return envShell, []string{}
|
if _, err := os.Stat(envShell); err == nil {
|
||||||
|
return envShell, []string{}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 尝试按优先级查找可用的 shell
|
// 尝试在 PATH 中查找 bash
|
||||||
shells := []string{"/bin/bash", "/bin/zsh", "/bin/sh"}
|
if path, err := exec.LookPath("bash"); err == nil {
|
||||||
|
return path, []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试在 PATH 中查找 zsh
|
||||||
|
if path, err := exec.LookPath("zsh"); err == nil {
|
||||||
|
return path, []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试在 PATH 中查找 sh
|
||||||
|
if path, err := exec.LookPath("sh"); err == nil {
|
||||||
|
return path, []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 最后回退到最常见的硬编码路径
|
||||||
|
shells := []string{"/bin/bash", "/usr/bin/bash", "/bin/sh"}
|
||||||
for _, sh := range shells {
|
for _, sh := range shells {
|
||||||
if _, err := os.Stat(sh); err == nil {
|
if _, err := os.Stat(sh); err == nil {
|
||||||
return sh, []string{}
|
return sh, []string{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 最后回退到 sh(应该总是存在)
|
|
||||||
return "sh", []string{}
|
return "sh", []string{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user