File size: 8,436 Bytes
d664931 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | # Running Claude Code in Docker
The `claude.sh` script runs [Claude Code](https://docs.anthropic.com/en/docs/claude-code)
inside a Docker container with `--dangerously-skip-permissions`. The container
provides a sandboxed environment where Claude Code has full shell access but
cannot affect the host system beyond the mounted repository.
The container supports:
- Building and testing OpenROAD with **CMake** and **Bazel**
- Running **ORFS flows** (Makefile-based RTL-GDSII)
- All file changes reflected on the host via volume mount
## Prerequisites
- [Docker](https://docs.docker.com/engine/install) installed on your machine
- Claude Code credentials (run `claude` on the host once to authenticate,
or set `ANTHROPIC_API_KEY` in your environment)
## Quick start
```shell
./claude.sh
```
On first run the Docker image is built automatically. Credentials from
`~/.claude/` are mounted into the container. The ORFS environment (`env.sh`)
is sourced automatically so tools are on `PATH` without any manual setup.
## Usage
```
./claude.sh [OPTIONS] [-- CLAUDE_ARGS...]
```
### Options
| Option | Description |
|--------|-------------|
| `--build` | Force rebuild the Docker image |
| `--shell` | Start a bash shell instead of Claude Code |
| `--image NAME` | Override Docker image (default: `openroad/flow-claude:latest`) |
| `--name NAME` | Override container name (default: `claude-orfs`) |
| `-h`, `--help` | Show help |
Arguments after `--` are passed directly to `claude`.
### Examples
```shell
# Interactive Claude Code session
./claude.sh
# Pass a prompt to Claude Code
./claude.sh -- -p "fix the failing test in openroad/drt"
# Interactive bash shell (for manual builds)
./claude.sh --shell
# Force rebuild the image after updates
./claude.sh --build
# Run a parallel session
CONTAINER_NAME=claude-2 ./claude.sh
```
## Building and testing inside the container
When using `--shell`, tools are already on `PATH`:
```shell
# CMake build
./build_openroad.sh --local --no_init -t $(nproc)
# Bazel build
cd tools/OpenROAD && bazel build //...
# Run an ORFS flow
cd flow && make DESIGN_CONFIG=./designs/nangate45/gcd/config.mk
```
Claude Code can run these same commands autonomously during an interactive
session.
## What persists across runs
The container is removed on exit (`--rm`), but the following host directories
are mounted so their contents survive:
| Host path | Container path | Content |
|-----------|---------------|---------|
| Repository root | `/workspace` | All source and build artifacts |
| `~/.claude/` | `~/.claude/` | Credentials, settings, conversation history |
| `~/.cache/bazel-claude/` | `~/.cache/bazel/` | Bazel external dependencies |
| `~/.gitconfig` | `~/.gitconfig` | Git identity (read-only) |
| Worktree and main `.git` (worktree only) | Host paths, or paths under `/tmp/claude-worktree` if they overlap a container mount | Git refs, objects, hooks, config |
| `$SSH_AUTH_SOCK` | forwarded | SSH agent for git over SSH |
## Environment variables
All environment variables are optional. They are passed into the container
when set on the host.
| Variable | Purpose |
|----------|---------|
| `ANTHROPIC_API_KEY` | API key (alternative to stored credentials) |
| `ANTHROPIC_MODEL` | Model override |
| `CLAUDE_IMAGE` | Docker image override (same as `--image`) |
| `CONTAINER_NAME` | Container name override (same as `--name`) |
| `BAZEL_CACHE_DIR` | Host path for Bazel cache (default: `~/.cache/bazel-claude`) |
| `CLAUDE_CODE_USE_BEDROCK` | Use AWS Bedrock |
| `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_REGION` | AWS credentials for Bedrock |
## How it works
The setup consists of three files:
### `docker/Dockerfile.claude`
Extends the `openroad/flow-ubuntu26.04-dev` base image (which has all
OpenROAD build dependencies) with:
- Java 21 and Bazelisk (for Bazel 8.x builds)
- Node.js 22 LTS and Claude Code CLI
- VS Code CLI, sudo, and other convenience tools
No source code is copied into the image. Everything comes from the
volume mount.
### `docker/claude-entrypoint.sh`
Handles UID/GID mapping so files created inside the container have the
correct ownership on the host. The entrypoint:
1. Reuses the account with the host UID if that UID already exists in the container.
Otherwise, it creates an account with the host UID/GID.
2. Sources `env.sh` to set up tool paths, from the start directory if it has one, else from `/workspace`
3. Drops privileges via `setpriv` before running the command
The mapped user's account record and `HOME` both use `/home/claude-user`.
This keeps the settings and cache mounts visible when the base image already has an account with the host UID.
Run the entrypoint regression check in disposable containers:
```shell
bash docker/test-claude-entrypoint.sh
```
With Podman:
```shell
CONTAINER_ENGINE=podman bash docker/test-claude-entrypoint.sh
```
The check covers an existing UID and a new UID.
It verifies that the launched command can read and write fixture settings through `HOME`.
It also checks that the account's recorded home matches `HOME`.
It also checks that the user can create `~/.cache/bazelisk` beside the mounted Bazel cache.
### `claude.sh`
Wrapper script that assembles the `docker run` command with the correct
volume mounts, environment variables, and entrypoint arguments.
When you run `claude.sh` from the top of a git worktree, it also mounts the worktree and the main checkout's `.git` directory at their host paths.
The shell starts in the worktree's host path.
The worktree's `.git` file and the `.git` files of its submodules point to these paths, so git works in the container.
The entrypoint sources `env.sh` from there, so `OPENROAD`, `FLOW_HOME` and `PATH` use the host path too.
Rebuild the image with `./claude.sh --build` to get this entrypoint.
The worktree is also at `/workspace`.
Use the start path for Git commands in submodules when `/workspace` is an alias for the worktree.
If a host path would overlap `/workspace` or `/home/claude-user`, the wrapper adds `/tmp/claude-worktree` before both host paths.
It starts Claude Code or the shell at the resulting worktree path and uses a temporary Git pointer there.
Submodules in a relocated worktree use the relative Git links that Git creates during normal initialization.
A worktree at exactly `/workspace` can use the existing checkout mount if its Git data is outside the reserved paths.
The paths of the other worktrees do not exist in the container, so git sees them as stale.
The wrapper sets `gc.worktreePruneExpire=never` in the container, so `git gc` keeps their records.
`git gc` still removes the record of a worktree that has no index file.
Do not run `git worktree prune` or `git worktree repair` in the container: they change the records of the other worktrees.
The start directory is part of some keys and caches.
Claude Code stores the history of a worktree session under its start path, so changing that path changes which sessions can resume.
Bazel uses a new output base, and a CMake build directory that you configured under `/workspace` must be configured again.
Check the mounts with a stub `docker` command, without a container engine:
```shell
bash docker/test-claude-sh.sh
```
The Claude Docker CI workflow also runs `docker/test-claude-worktree-paths.sh` on a disposable Linux runner.
It checks Git in the launched shell, including the initialized submodule and writes to the original checkout.
## Security model
Claude Code runs with `--dangerously-skip-permissions` **inside** the
container, meaning it can execute any shell command without confirmation.
The Docker container provides the isolation:
- File access is limited to the mounted repository.
From a git worktree, this includes the main checkout's `.git` directory, with the refs and objects of all its worktrees.
It also holds git hooks and config, which run on the host at the next git command, so code in the container can run code on the host.
- No access to host system packages, services, or other projects
- No access to host network services bound to localhost
- `docker kill claude-orfs` stops everything instantly
:::{Warning}
The container has network access (required for the Anthropic API and
Bazel dependency fetching). Claude Code could in principle make outbound
network requests. If this is a concern, use Docker network policies to
restrict egress to only the Anthropic API endpoints.
:::
|