Claude Code Docker 沙箱

从 Dagu Docker 沙箱运行 Claude Code。

在 runner 镜像中安装 Claude Code,验证 claude 二进制,并只向需要的 harness 步骤传递凭据。Dagu 用 YAML 保存调度、日志、重试策略和容器边界。

Docker 中的 Claude Code 步骤
steps:
  - id: claude_review
    action: harness.run
    container:
      image: dagu-claude-runner:local
      pull_policy: never
      working_dir: /workspace
      volumes:
        - .:/workspace:ro
      env:
        - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
    with:
      provider: claude
      model: sonnet
      prompt: |
        Review this repository and summarize the highest-risk issues.

使用官方 @anthropic-ai/claude-code npm 包。

模型调用前通过 Dagu 验证 claude --version。

在 container 步骤边界传递 ANTHROPIC_API_KEY。

可根据凭据作用域选择根级或步骤级 container。

01

Runner 镜像

Claude Code 可在 Node.js 18 或更高版本上作为全局 npm 包安装。示例使用 Node 22,并在镜像构建期间验证二进制。

Dockerfile
FROM node:22-bookworm-slim

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

RUN npm install -g @anthropic-ai/claude-code
RUN claude --version

WORKDIR /workspace
ENTRYPOINT []
CMD ["bash"]
02

冒烟检查

先运行 shell provider,在不向 Claude 发送 prompt 的情况下检查镜像和二进制路径。

版本检查 DAG
type: graph

harnesses:
  shell:
    binary: sh
    prefix_args:
      - -c
    prompt_mode: arg

steps:
  - id: claude_version
    action: harness.run
    container:
      image: dagu-claude-runner:local
      pull_policy: never
    with:
      provider: shell
      prompt: |
        set -eu
        claude --version
        command -v claude
03

凭据

简单的非交互运行可以把 ANTHROPIC_API_KEY 传入容器。其他 Claude Code 认证方式也可以使用,前提是对应文件或环境变量在同一容器内可用。

04

容器作用域

单个 Claude Code 任务优先使用步骤级 container。只有当前置 build 或 test 也必须在同一镜像中运行后再让 Claude Code 读取 workspace 时,才使用根级 container。