Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

13.3 KB
Raw Blame
# syntax=docker/dockerfile:1.6
ARG PYTHON_VERSION=3.11
ARG NODE_VERSION=22
ARG UV_VERSION=0.11.10
ARG PYPI_INDEX_URL=""
ARG APT_MIRROR=""

############################
# Backend base
############################
FROM ghcr.io/astral-sh/uv:${UV_VERSION} AS uv

FROM python:${PYTHON_VERSION}-bookworm AS backend-base

ARG PYPI_INDEX_URL=""
ARG APT_MIRROR=""

WORKDIR /opt/xagent

COPY --from=uv /uv /uvx /usr/local/bin/

ENV UV_COMPILE_BYTECODE=1
ENV UV_LINK_MODE=copy
ENV UV_PYTHON_DOWNLOADS=0

RUN if [ -n "$APT_MIRROR" ]; then \
        sed -i "s|http://deb.debian.org/debian|$APT_MIRROR|g" /etc/apt/sources.list.d/debian.sources \
        || sed -i "s|http://deb.debian.org/debian|$APT_MIRROR|g" /etc/apt/sources.list; \
    fi

RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    ca-certificates \
    gcc \
    git \
    pkg-config \
    curl \
    openssl \
    libssl-dev \
    libffi-dev \
    libgl1 \
    libglib2.0-0 \
    libsm6 \
    libxrender1 \
    libxext6 \
    && git config --global http.version HTTP/1.1 \
    && git config --global http.postBuffer 524288000 \
    && rm -rf /var/lib/apt/lists/*

RUN python -m venv /opt/venv \
    && /opt/venv/bin/python -c "import ssl; print(ssl.OPENSSL_VERSION)" \
    && /opt/venv/bin/pip install --no-cache-dir ${PYPI_INDEX_URL:+--index-url $PYPI_INDEX_URL} --upgrade pip

############################
# Backend dependencies
############################
FROM backend-base AS backend-deps

ARG PYPI_INDEX_URL=""
ARG XAGENT_PACKAGE_VERSION="0.0.0+docker"

COPY pyproject.toml uv.lock README.md ./

RUN --mount=type=cache,target=/root/.cache/uv \
    SETUPTOOLS_SCM_PRETEND_VERSION="${XAGENT_PACKAGE_VERSION}" \
    VIRTUAL_ENV=/opt/venv uv sync --active --locked --no-dev --no-install-project --no-editable \
    --group backend-image \
    ${PYPI_INDEX_URL:+--default-index $PYPI_INDEX_URL}

############################
# DeepDoc model assets
############################
FROM backend-deps AS deepdoc-models

RUN --mount=type=cache,id=xagent-deepdoc-model-cache,target=/tmp/deepdoc-cache,sharing=locked \
    DEEPDOC_MODEL_HOME=/tmp/deepdoc-cache \
    DEEPDOC_NLTK_DATA_DIR=/tmp/deepdoc-cache/nltk_data \
    DEEPDOC_TIKTOKEN_CACHE_DIR=/tmp/deepdoc-cache/tiktoken_cache \
    /opt/venv/bin/deepdoc-download-models \
    && mkdir -p /root/.cache/deepdoc \
    && cp -a /tmp/deepdoc-cache/. /root/.cache/deepdoc/

############################
# Backend build
############################
FROM backend-deps AS backend-build

ARG PYPI_INDEX_URL=""
ARG XAGENT_PACKAGE_VERSION="0.0.0+docker"

COPY src ./src
COPY alembic.ini .

RUN --mount=type=cache,target=/root/.cache/uv \
    SETUPTOOLS_SCM_PRETEND_VERSION="${XAGENT_PACKAGE_VERSION}" \
    VIRTUAL_ENV=/opt/venv uv sync --active --locked --no-dev --no-editable \
    --group backend-image \
    ${PYPI_INDEX_URL:+--default-index $PYPI_INDEX_URL}

# A raw backend image must reach its local combined-mode startup admission
# without deployment-only Redis or encryption configuration. A configured
# worker pool or split host still selects shared execution and strict validation.
RUN env -u XAGENT_SHARED_TASK_EXECUTION_ENABLED \
        -u XAGENT_TASK_EXECUTION_ROLE \
        -u XAGENT_WORKER_COUNT \
        -u XAGENT_REDIS_URL \
        -u ENCRYPTION_KEY \
        /opt/venv/bin/python -c \
        'from xagent.config import get_shared_task_execution_enabled, validate_task_execution_host_config; assert not get_shared_task_execution_enabled(); validate_task_execution_host_config()'

############################
# Runtime
############################
FROM python:${PYTHON_VERSION}-bookworm AS runtime-base

ARG NODE_VERSION=22
ARG APT_MIRROR=""
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
ENV VIRTUAL_ENV=/opt/venv
ENV PATH="/opt/venv/bin:$PATH"
ENV NODE_PATH="/opt/xagent/frontend/node_modules"
# Chromium sandboxing stays enabled by default. The published Compose
# deployment runs this image as root for its existing mounted-volume
# integrations, so docker-compose.yml opts into the compatibility flag
# explicitly. Direct deployments should run as a non-root user; only set
# XAGENT_BROWSER_ALLOW_UNSANDBOXED=true when that is not possible.
ENV XAGENT_BROWSER_ALLOW_UNSANDBOXED=false

WORKDIR /opt/xagent

RUN if [ -n "$APT_MIRROR" ]; then \
        sed -i "s|http://deb.debian.org/debian|$APT_MIRROR|g" /etc/apt/sources.list.d/debian.sources \
        || sed -i "s|http://deb.debian.org/debian|$APT_MIRROR|g" /etc/apt/sources.list; \
    fi

# Noto Sans/Serif CJK families referenced by pptx_tool default fontFace values
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    curl \
    gnupg \
    openssl \
    libreoffice \
    ffmpeg \
    fonts-noto-cjk \
    libglib2.0-0 \
    libsm6 \
    libxrender1 \
    libxext6 \
    libgl1 \
    libatk1.0-0 \
    libatk-bridge2.0-0 \
    libcups2 \
    libdbus-1-3 \
    libdrm2 \
    libxkbcommon0 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxi6 \
    libxtst6 \
    libpango-1.0-0 \
    libcairo2 \
    libgbm1 \
    libnss3 \
    libnspr4 \
    libxrandr2 \
    libasound2 \
    libpangocairo-1.0-0 \
    xvfb \
    xauth \
    && rm -rf /var/lib/apt/lists/*

RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /usr/share/keyrings/nodesource.gpg \
    && echo "deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_${NODE_VERSION}.x nodistro main" \
        > /etc/apt/sources.list.d/nodesource.list \
    && apt-get update \
    && apt-get install -y --no-install-recommends nodejs \
    && rm -rf /var/lib/apt/lists/*

# Docker CLI only: no daemon runs here, so docker.io's engine + containerd +
# runc are dead weight. Swapped rather than dropped because command_policy.py
# gates the agent shell by path -- /usr/bin/docker must stay. arch must come
# from `dpkg --print-architecture` or the arm64 image breaks.
# --no-install-recommends drops buildx/compose (+109 MiB): plain `docker build`
# still works via the legacy builder, DOCKER_BUILDKIT=1 hard-errors. Supported
# paths are the Python SDK and the sandbox overlay, not ad-hoc CLI builds.
# See https://github.com/xorbitsai/xagent/pull/1807
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker.gpg \
    && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker.gpg] https://download.docker.com/linux/debian $(. /etc/os-release && echo "$VERSION_CODENAME") stable" \
        > /etc/apt/sources.list.d/docker.list \
    && apt-get update \
    && apt-get install -y --no-install-recommends docker-ce-cli \
    && test -x /usr/bin/docker \
    && docker --version \
    && ! command -v dockerd \
    && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/docker.list /usr/share/keyrings/docker.gpg

# Browser for the built-in "chrome" MCP connector (chrome-devtools-mcp).
# Gated behind INSTALL_CHROME (default true: the default build invocation is
# unchanged, but the resulting image gains a Chrome install and a warmed npx
# cache it did not have before) rather than tying this to
# is_visible_in_connector: that flag is a mutable DB boolean an admin can
# flip at runtime with no image rebuild, so it cannot gate what ships in the
# image. A build ARG is the actual release gate for the image; deployments
# that never intend to enable this connector can pass
# --build-arg INSTALL_CHROME=false to drop the image size and the
# /opt/google/chrome/chrome binary + npx cache. Playwright Chromium is
# installed unconditionally in a separate stage below. The published backend
# image still runs as root. The Compose deployment supplies the explicit
# compatibility opt-in; direct non-root deployments keep Chromium sandboxed.
ARG INSTALL_CHROME=true
# Distinct from the Playwright Chromium below, which chrome-devtools-mcp does
# not detect. Its default "stable" channel resolution checks exactly
# /opt/google/chrome/chrome on Linux, so guarantee that path exists on every
# platform this image builds for (docker-publish.yml: amd64 + arm64):
#   - amd64: Google's own chrome deb currently ships the real binary at
#     exactly .../chrome, so no symlink is needed -- but the resolver path is
#     load-bearing, so fail the build loudly if a future package layout
#     changes it, instead of silently shipping a broken image. (A symlink
#     fallback to .../google-chrome would be wrong here regardless:
#     google-chrome is a wrapper script that execs "$HERE/chrome", so
#     pointing chrome -> google-chrome would create a self-referential exec
#     loop, not a working fallback.)
#   - arm64: Google publishes no arm64 Chrome at all; Debian's Chromium is
#     CDP-compatible with chrome-devtools-mcp, symlinked into the same path
# The apt keyring/source list added for the amd64 branch are removed in the
# same layer once the package is installed -- keeping them would leave
# dl.google.com resolvable via `apt-get update` inside a running container
# for no benefit, since the install target is unpinned "stable" anyway.
# Both arch branches guarantee the same resolver path, so the existence
# check is collapsed to one, after the arch selection, instead of a
# near-duplicate per branch. arm64 is now an explicit elif (not the bare
# else) so a genuinely unexpected architecture fails loudly here instead of
# silently attempting the arm64 branch's Debian-chromium install.
RUN if [ "$INSTALL_CHROME" != "true" ]; then \
        echo "INSTALL_CHROME=$INSTALL_CHROME -- skipping Chrome/Chromium install"; \
    else \
        if [ "$(dpkg --print-architecture)" = "amd64" ]; then \
            curl -fsSL https://dl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/google-chrome.gpg \
            && echo "deb [arch=amd64 signed-by=/usr/share/keyrings/google-chrome.gpg] https://dl.google.com/linux/chrome/deb/ stable main" \
                > /etc/apt/sources.list.d/google-chrome.list \
            && apt-get update \
            && apt-get install -y --no-install-recommends google-chrome-stable \
            && rm -rf /var/lib/apt/lists/* /etc/apt/sources.list.d/google-chrome.list /usr/share/keyrings/google-chrome.gpg; \
        elif [ "$(dpkg --print-architecture)" = "arm64" ]; then \
            apt-get update \
            && apt-get install -y --no-install-recommends chromium \
            && rm -rf /var/lib/apt/lists/* \
            && mkdir -p /opt/google/chrome \
            && ln -s /usr/bin/chromium /opt/google/chrome/chrome; \
        else \
            echo "ERROR: unsupported architecture $(dpkg --print-architecture) for the chrome connector -- add an install branch, or build with --build-arg INSTALL_CHROME=false" >&2; \
            exit 1; \
        fi; \
        [ -e /opt/google/chrome/chrome ] || { echo "ERROR: the Chrome/Chromium install did not produce /opt/google/chrome/chrome -- update the chrome-devtools-mcp resolver path handling" >&2; exit 1; }; \
    fi

# Warm the npx cache for the exact version pinned in
# builtin_mcp_registry.py's launch_config. npx resolves against its own
# cache (~/.npm/_npx), NOT global npm installs, so `npm install -g` would
# not prevent per-launch registry hits — an exact-version spec with a warm
# cache resolves fully offline (verified with npm_config_offline=true).
# The backend runs as root, so build-time root's cache is the one the MCP
# launch path reads. `--help` exercises resolution without starting Chrome.
# Skipped along with the browser install above: warming the cache without a
# browser to run against buys nothing (the connector 404s on connect either
# way while hidden, and every call would fail at launch without a browser).
RUN if [ "$INSTALL_CHROME" = "true" ]; then \
        npx -y --prefer-offline [email protected] --help > /dev/null; \
    fi

COPY docker/entrypoint.sh /opt/xagent/deploy/entrypoint.sh
RUN chmod +x /opt/xagent/deploy/entrypoint.sh

FROM runtime-base AS runtime-frontend-tools

COPY frontend/package.json frontend/package-lock.json /opt/xagent/frontend/
RUN --mount=type=cache,target=/root/.npm \
    cd /opt/xagent/frontend \
    && npm config set fetch-retries 5 \
    && npm config set fetch-retry-mintimeout 30000 \
    && npm ci

FROM runtime-base AS runtime-playwright

COPY --from=backend-deps /opt/venv /opt/venv
RUN python -m playwright install chromium

FROM runtime-base AS runtime

COPY --from=uv /uv /uvx /usr/local/bin/
COPY --from=runtime-frontend-tools /opt/xagent/frontend /opt/xagent/frontend
COPY --from=runtime-playwright /ms-playwright /ms-playwright

COPY --from=backend-build /opt/venv /opt/venv
COPY --from=backend-build /opt/xagent/src /opt/xagent/src
COPY --from=backend-build /opt/xagent/pyproject.toml /opt/xagent/
COPY --from=backend-build /opt/xagent/alembic.ini /opt/xagent/
COPY --from=deepdoc-models /root/.cache/deepdoc /root/.cache/deepdoc

ARG XAGENT_VERSION=""
ARG XAGENT_GIT_COMMIT=""
ARG XAGENT_BUILD_TIME=""
ENV XAGENT_VERSION=${XAGENT_VERSION}
ENV XAGENT_GIT_COMMIT=${XAGENT_GIT_COMMIT}
ENV XAGENT_BUILD_TIME=${XAGENT_BUILD_TIME}

# The published backend image is the team/self-hosting artifact: run one web
# process plus two Agent workers by default.  The entrypoint creates or reuses a
# private key at this path; Compose mounts it on the persistent secrets volume.
ENV XAGENT_WORKER_COUNT=2
ENV XAGENT_ENCRYPTION_KEY_FILE=/run/xagent-secrets/encryption.key
ENV XAGENT_JWT_SECRET_FILE=/run/xagent-secrets/jwt.secret
ENV XAGENT_SECRET_ENCRYPTION_KEY_FILE=/run/xagent-secrets/tool-credentials.secret

EXPOSE 8000
ENTRYPOINT ["/opt/xagent/deploy/entrypoint.sh"]