> For the complete documentation index, see [llms.txt](https://www.openclawbook.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.openclawbook.xyz/en/ch37-deployment/37.2-docker-deployment.md). Agents can also search and read this documentation through its [MCP server](https://www.openclawbook.xyz/~gitbook/mcp).

# 37.2 Docker Deployment

> **Generated by**: Claude Opus 4.6 (initial draft) / Claude Opus 5 (`v2026.3.9` revision) **Token usage**: Input \~200k tokens, Output \~6k tokens (this section) **Source baseline**: `v2026.3.9` (`v2026.3.8-170-g665f67726`)

***

Docker is the recommended way to deploy OpenClaw to servers, particularly suitable for VPS and NAS scenarios.

## 37.2.1 Dockerfile Analysis: Multi-stage Build and Non-root User

OpenClaw's main Dockerfile is based on `node:22-bookworm`:

```dockerfile
# Dockerfile
FROM node:22-bookworm

# Install Bun (required by build scripts)
RUN curl -fsSL https://bun.sh/install | bash
ENV PATH="/root/.bun/bin:${PATH}"

RUN corepack enable

WORKDIR /app

# Optional additional apt packages (controlled via build argument)
ARG OPENCLAW_DOCKER_APT_PACKAGES=""
RUN if [ -n "$OPENCLAW_DOCKER_APT_PACKAGES" ]; then \
      apt-get update && \
      DEBIAN_FRONTEND=noninteractive apt-get install -y \
        --no-install-recommends $OPENCLAW_DOCKER_APT_PACKAGES && \
      apt-get clean && rm -rf /var/lib/apt/lists/*; \
    fi

# Dependency installation (leveraging Docker layer cache)
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml .npmrc ./
COPY ui/package.json ./ui/package.json
COPY patches ./patches
COPY scripts ./scripts
RUN pnpm install --frozen-lockfile

# Build
COPY . .
RUN OPENCLAW_A2UI_SKIP_MISSING=1 pnpm build
ENV OPENCLAW_PREFER_PNPM=1
RUN pnpm ui:build

ENV NODE_ENV=production

# Security hardening: run as non-root user
RUN chown -R node:node /app
USER node

# Default command: start Gateway
CMD ["node", "openclaw.mjs", "gateway", "--allow-unconfigured"]
```

Key design decisions:

| Decision                                 | Rationale                                                                                |
| ---------------------------------------- | ---------------------------------------------------------------------------------------- |
| `node:22-bookworm` instead of `alpine`   | Requires complete glibc and compiler toolchain (native modules like Sharp image library) |
| Install Bun                              | Some build scripts use Bun to run                                                        |
| `OPENCLAW_PREFER_PNPM=1`                 | Bun may fail on ARM/Synology architectures, force pnpm to build UI                       |
| COPY dependency files before source code | Leverage Docker layer cache, skip `pnpm install` when dependencies unchanged             |
| `USER node`                              | Run as non-root user (uid 1000), reduce container escape risk                            |
| `--allow-unconfigured`                   | Allow startup without configuration, convenient for first run                            |

> **Sidebar — Docker Layer Cache**
>
> Docker images consist of multiple read-only layers. Each `RUN`/`COPY` instruction creates a layer. If a layer's input hasn't changed, Docker reuses the cache instead of re-executing. By separately COPY and install `package.json` and `pnpm-lock.yaml` first, it means that as long as dependencies don't change, even if source code changes, dependency reinstallation is skipped — this saves significant build time during development iterations.

## 37.2.2 `docker-compose.yml` Parsing

`docker-compose.yml` defines two services:

```yaml
services:
  openclaw-gateway:
    image: ${OPENCLAW_IMAGE:-openclaw:local}
    environment:
      HOME: /home/node
      TERM: xterm-256color
      OPENCLAW_GATEWAY_TOKEN: ${OPENCLAW_GATEWAY_TOKEN}
    volumes:
      - ${OPENCLAW_CONFIG_DIR}:/home/node/.openclaw
      - ${OPENCLAW_WORKSPACE_DIR}:/home/node/.openclaw/workspace
    ports:
      - "${OPENCLAW_GATEWAY_PORT:-18789}:18789"
      - "${OPENCLAW_BRIDGE_PORT:-18790}:18790"
    init: true
    restart: unless-stopped
    command: ["node", "dist/index.js", "gateway",
              "--bind", "${OPENCLAW_GATEWAY_BIND:-lan}",
              "--port", "18789"]

  openclaw-cli:
    image: ${OPENCLAW_IMAGE:-openclaw:local}
    environment:
      HOME: /home/node
      BROWSER: echo              # Prevent automatic browser opening
    volumes:
      - ${OPENCLAW_CONFIG_DIR}:/home/node/.openclaw
      - ${OPENCLAW_WORKSPACE_DIR}:/home/node/.openclaw/workspace
    stdin_open: true             # Support interactive input
    tty: true                    # Allocate terminal
    init: true
    entrypoint: ["node", "dist/index.js"]
```

The **Gateway service** is the core, running long-term. The **CLI service** is an interactive container used on-demand, started via `docker compose run openclaw-cli chat`.

Key configuration parsing:

| Configuration             | Description                                                                        |
| ------------------------- | ---------------------------------------------------------------------------------- |
| `init: true`              | Use `tini` as PID 1 process, correctly forward signals and reap zombie processes   |
| `restart: unless-stopped` | Auto-restart on crash, unless manually stopped                                     |
| `--bind lan`              | Bind to LAN address (`0.0.0.0`), necessary in container to be accessible from host |
| Volume mounts             | Persist configuration and workspace directories to host                            |
| `BROWSER: echo`           | Prevent browser opening in CLI container (no GUI in container)                     |

> **Sidebar — init: true and PID 1 Problem**
>
> In Linux containers, the process specified by `CMD` runs directly as PID 1. PID 1 has special responsibilities: reap orphan processes and correctly respond to SIGTERM signals. Node.js processes aren't designed for this — they don't reap child process exit statuses and may not gracefully respond to signals. `init: true` injects `tini` (a minimal init process) into the container as PID 1, responsible for signal forwarding and zombie process reaping.

## 37.2.3 Docker Sandbox (`Dockerfile.sandbox`)

OpenClaw's code execution sandbox can run in a separate Docker container:

```dockerfile
# Dockerfile.sandbox
FROM debian:bookworm-slim

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update \
  && apt-get install -y --no-install-recommends \
    bash ca-certificates curl git jq python3 ripgrep \
  && rm -rf /var/lib/apt/lists/*

CMD ["sleep", "infinity"]
```

This sandbox container is extremely minimal — only basic CLI tools, no Node.js or other runtimes. The AI agent's `exec` tool executes commands in this container, completely isolated from the host system.

Additionally, there's `Dockerfile.sandbox-browser`, which adds browser automation tools (Playwright/Puppeteer) on top of the sandbox base, for agent tasks requiring browser capabilities.

## 37.2.4 Environment Variable Configuration

The most important environment variables in Docker deployment:

| Environment Variable        | Description                     | Default          |
| --------------------------- | ------------------------------- | ---------------- |
| `OPENCLAW_GATEWAY_TOKEN`    | Gateway authentication token    | (none, must set) |
| `OPENCLAW_GATEWAY_PASSWORD` | Gateway password authentication | (none)           |
| `OPENCLAW_GATEWAY_BIND`     | Bind address                    | `lan`            |
| `OPENCLAW_GATEWAY_PORT`     | Gateway port                    | `18789`          |
| `OPENCLAW_BRIDGE_PORT`      | Bridge port                     | `18790`          |
| `OPENCLAW_CONFIG_DIR`       | Configuration directory mapping | (must set)       |
| `OPENCLAW_WORKSPACE_DIR`    | Workspace directory mapping     | (must set)       |

API keys can be passed via environment variables or written in mounted configuration files.

***

## Section Summary

1. **Dockerfile uses node:22-bookworm**, installs Bun + pnpm, optimizes builds with layer cache, runs as non-root user.
2. **docker-compose defines two services**: long-running Gateway and on-demand CLI, both use tini as init process.
3. **Sandbox container** based on debian:bookworm-slim, contains only basic CLI tools, provides secure isolated environment for agent's exec tool.
4. **Environment variables** control authentication, network binding, and directory mapping, the primary configuration method for containerized deployment.
