dashboard-nanobot/bot-images/Dashboard.Dockerfile

88 lines
3.9 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

FROM python:3.12-slim AS builder
ENV PYTHONUNBUFFERED=1
ENV LANG=C.UTF-8
ENV LC_ALL=C.UTF-8
ENV PYTHONIOENCODING=utf-8
ENV PATH=/opt/venv/bin:$PATH
# 1. 替换 Debian 源为国内镜像
RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources && \
sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources
# 2. 仅在构建阶段安装编译依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
&& rm -rf /var/lib/apt/lists/*
RUN python -m venv /opt/venv
WORKDIR /app
COPY pyproject.toml README.md LICENSE THIRD_PARTY_NOTICES.md ./
# 3. 先安装第三方依赖。该层只依赖 pyproject.toml源码改动不会触发整套依赖重装。
RUN --mount=type=cache,target=/root/.cache/pip \
python -m pip install -i https://mirrors.aliyun.com/pypi/simple/ --upgrade \
--no-compile pip setuptools wheel aiohttp hatchling && \
python -c 'import tomllib; data=tomllib.load(open("pyproject.toml","rb")); deps=list(data["project"].get("dependencies", [])); deps.extend(data["project"].get("optional-dependencies", {}).get("wecom", [])); print("\n".join(deps))' > /tmp/requirements.txt && \
pip install --no-compile -i https://mirrors.aliyun.com/pypi/simple/ -r /tmp/requirements.txt && \
rm -f /tmp/requirements.txt
COPY nanobot/ nanobot/
COPY bridge/ bridge/
# 4. 源码层只安装 nanobot 本体,不重复解析/下载第三方依赖。
RUN --mount=type=cache,target=/root/.cache/pip \
pip install --no-compile --no-deps --no-build-isolation . && \
find /opt/venv -type d -name __pycache__ -prune -exec rm -rf {} + && \
find /opt/venv -name '*.pyc' -delete
FROM python:3.12-slim
ENV PYTHONUNBUFFERED=1
ENV LANG=C.UTF-8
ENV LC_ALL=C.UTF-8
ENV PYTHONIOENCODING=utf-8
ENV PATH=/opt/venv/bin:$PATH
ARG INSTALL_EXTRA_CLI=false
# 1. 替换 Debian 源为国内镜像
RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources && \
sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources
# 2. 安装基础运行时依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
bubblewrap \
ca-certificates \
curl \
git \
openssh-client \
tmux \
&& git config --global --add url."https://github.com/".insteadOf ssh://git@github.com/ \
&& git config --global --add url."https://github.com/".insteadOf git@github.com: \
&& rm -rf /var/lib/apt/lists/*
# 3. Node.js 与 GitHub CLI 只在需要相关工具时安装,默认跳过以避免访问外部 apt 源。
RUN if [ "$INSTALL_EXTRA_CLI" = "true" ]; then \
apt-get update && apt-get install -y --no-install-recommends gnupg \
&& mkdir -p /etc/apt/keyrings /etc/apt/sources.list.d \
&& curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
&& echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_22.x nodistro main" > /etc/apt/sources.list.d/nodesource.list \
&& curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg > /etc/apt/keyrings/githubcli-archive-keyring.gpg \
&& chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" > /etc/apt/sources.list.d/github-cli.list \
&& apt-get update && apt-get install -y --no-install-recommends \
gh \
nodejs \
&& apt-get purge -y --auto-remove gnupg \
&& rm -rf /var/lib/apt/lists/*; \
else \
echo "Skipping optional Node.js and GitHub CLI"; \
fi
# 4. 仅复制已安装好的运行环境,避免把源码目录打进最终镜像
COPY --from=builder /opt/venv /opt/venv
WORKDIR /root
# 官方 gateway 模式,现在它会自动加载您的 DashboardChannel
CMD ["nanobot", "gateway"]