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 TARGETARCH
|
||||
|
||||
# Install UPX
|
||||
RUN apt-get update && apt-get install -y upx-ucl && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy build info
|
||||
@@ -53,7 +56,7 @@ RUN go env -w GOPROXY=https://goproxy.cn,direct && go mod download
|
||||
# Copy backend source
|
||||
COPY . .
|
||||
|
||||
# Copy frontend dist
|
||||
# Copy frontend dist (needed for embedding)
|
||||
COPY --from=frontend-builder /app/web/dist ./internal/static/dist
|
||||
|
||||
# Build Go binary
|
||||
@@ -61,13 +64,17 @@ RUN VERSION_VAL=$(cat /build-info/version.txt) && \
|
||||
BUILD_TIME_VAL=$(cat /build-info/build_time.txt) && \
|
||||
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}'" \
|
||||
-o baihu .
|
||||
-o baihu . && \
|
||||
upx --best --lzma baihu
|
||||
|
||||
# ================================
|
||||
# Stage 3: Build Agent (all platforms)
|
||||
# ================================
|
||||
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
|
||||
|
||||
# Copy build info
|
||||
@@ -84,27 +91,23 @@ COPY agent/ ./agent/
|
||||
# Build agent for all platforms and package as tar.gz
|
||||
WORKDIR /app/agent
|
||||
|
||||
# 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) && \
|
||||
LDFLAGS="-s -w -X 'main.Version=${VERSION_VAL}' -X 'main.BuildTime=${BUILD_TIME_VAL}'" && \
|
||||
mkdir -p /opt/agent && \
|
||||
echo "${VERSION_VAL}" > /opt/agent/version.txt && \
|
||||
# Linux amd64
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o baihu-agent . && \
|
||||
tar -czvf /opt/agent/baihu-agent-linux-amd64.tar.gz baihu-agent config.example.ini && rm baihu-agent && \
|
||||
# Linux arm64
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags="${LDFLAGS}" -o baihu-agent . && \
|
||||
tar -czvf /opt/agent/baihu-agent-linux-arm64.tar.gz baihu-agent config.example.ini && rm baihu-agent && \
|
||||
# macOS amd64
|
||||
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 && \
|
||||
# macOS arm64
|
||||
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="${LDFLAGS}" -o baihu-agent . && \
|
||||
tar -czvf /opt/agent/baihu-agent-darwin-arm64.tar.gz baihu-agent config.example.ini && rm baihu-agent && \
|
||||
# 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 && \
|
||||
# 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 . && \
|
||||
upx --best --lzma 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 "" && \
|
||||
echo "Agent build completed for all platforms"
|
||||
# ================================
|
||||
# Stage 4: Final image
|
||||
@@ -132,9 +135,6 @@ 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/docker-entrypoint.sh .
|
||||
@@ -147,7 +147,6 @@ COPY --from=agent-builder /opt/agent /opt/agent
|
||||
|
||||
RUN chmod +x /opt/sync.py \
|
||||
&& chmod +x docker-entrypoint.sh \
|
||||
&& touch "dont-not-delete-anythings" \
|
||||
&& echo "set encoding=utf-8" >> /etc/vim/vimrc
|
||||
|
||||
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 \
|
||||
&& apk add --no-cache \
|
||||
tzdata git bash curl wget vim htop ca-certificates rsync \
|
||||
python3 \
|
||||
# Python build dependencies
|
||||
build-base libffi-dev openssl-dev bzip2-dev zlib-dev readline-dev sqlite-dev ncurses-dev xz-dev \
|
||||
&& BUILD_DEPS="build-base libffi-dev openssl-dev bzip2-dev zlib-dev readline-dev sqlite-dev ncurses-dev xz-dev" \
|
||||
&& apk add --no-cache $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 \
|
||||
&& apk del $BUILD_DEPS \
|
||||
&& ln -sf /usr/bin/python3 /usr/bin/python \
|
||||
&& cp /usr/share/zoneinfo/${TZ} /etc/localtime \
|
||||
&& echo "${TZ}" > /etc/timezone \
|
||||
&& rm -rf /var/cache/apk/*
|
||||
|
||||
# 安装 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
|
||||
&& rm -rf /var/cache/apk/* \
|
||||
&& rm -rf /root/.cache/mise /root/.cache/pip /opt/mise-base/cache/*
|
||||
|
||||
+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 \
|
||||
&& apt-get update \
|
||||
&& apt-get install -y --no-install-recommends \
|
||||
tzdata git gcc curl wget vim htop ca-certificates rsync \
|
||||
build-essential libssl-dev zlib1g-dev libbz2-dev \
|
||||
tzdata git curl wget vim htop ca-certificates rsync \
|
||||
&& BUILD_DEPS="gcc build-essential libssl-dev zlib1g-dev libbz2-dev \
|
||||
libreadline-dev libsqlite3-dev libncursesw5-dev \
|
||||
xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# 安装 mise
|
||||
RUN curl https://mise.run | MISE_INSTALL_PATH=/usr/local/bin/mise sh
|
||||
|
||||
# 使用 mise 安装 Node.js 和 Python
|
||||
ENV MISE_DATA_DIR=/opt/mise-base
|
||||
ENV MISE_CONFIG_DIR=/opt/mise-base
|
||||
ENV MISE_HTTP_TIMEOUT=300
|
||||
# 使用镜像源加速 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
|
||||
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 \
|
||||
&& rm -rf /var/lib/apt/lists/* \
|
||||
&& rm -rf /root/.cache/mise /root/.cache/pip /opt/mise-base/cache/*
|
||||
|
||||
@@ -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 \
|
||||
&& apt-get update \
|
||||
&& apt-get install -y --no-install-recommends \
|
||||
tzdata git gcc curl wget vim htop ca-certificates rsync \
|
||||
python3-full \
|
||||
build-essential libssl-dev zlib1g-dev libbz2-dev \
|
||||
tzdata git curl wget vim htop ca-certificates rsync \
|
||||
&& BUILD_DEPS="gcc build-essential libssl-dev zlib1g-dev libbz2-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 \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# 安装 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
|
||||
&& rm -rf /var/lib/apt/lists/* \
|
||||
&& rm -rf /root/.cache/mise /root/.cache/pip /opt/mise-base/cache/*
|
||||
|
||||
+20
-4
@@ -14,18 +14,34 @@ func GetShell() (shell string, args []string) {
|
||||
|
||||
// 优先使用环境变量中的 SHELL
|
||||
if envShell := os.Getenv("SHELL"); envShell != "" {
|
||||
return envShell, []string{}
|
||||
if _, err := os.Stat(envShell); err == nil {
|
||||
return envShell, []string{}
|
||||
}
|
||||
}
|
||||
|
||||
// 尝试按优先级查找可用的 shell
|
||||
shells := []string{"/bin/bash", "/bin/zsh", "/bin/sh"}
|
||||
// 尝试在 PATH 中查找 bash
|
||||
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 {
|
||||
if _, err := os.Stat(sh); err == nil {
|
||||
return sh, []string{}
|
||||
}
|
||||
}
|
||||
|
||||
// 最后回退到 sh(应该总是存在)
|
||||
return "sh", []string{}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user