From 72a579a74f5b9bfbe1ce026c5b741c9c82877259 Mon Sep 17 00:00:00 2001 From: Matt Liberty Date: Sun, 12 Apr 2026 21:40:03 +0000 Subject: [PATCH 1/2] Add Docker container setup for running Claude Code safely Provides claude.sh wrapper and Docker image that sandboxes claude --dangerously-skip-permissions inside a container with CMake, Bazel, and ORFS support. The repo is volume-mounted so edits persist on the host with correct UID/GID ownership. Signed-off-by: Matt Liberty --- claude.sh | 146 ++++++++++++++++++++++++++++++++ docker/Dockerfile.claude | 57 +++++++++++++ docker/claude-entrypoint.sh | 62 ++++++++++++++ docs/toc.yml | 2 + docs/user/ClaudeCode.md | 162 ++++++++++++++++++++++++++++++++++++ 5 files changed, 429 insertions(+) create mode 100755 claude.sh create mode 100644 docker/Dockerfile.claude create mode 100755 docker/claude-entrypoint.sh create mode 100644 docs/user/ClaudeCode.md diff --git a/claude.sh b/claude.sh new file mode 100755 index 0000000000..c74828daa9 --- /dev/null +++ b/claude.sh @@ -0,0 +1,146 @@ +#!/usr/bin/env bash +# Wrapper to run Claude Code inside a Docker container with +# --dangerously-skip-permissions. The repo is volume-mounted so all +# edits are reflected on the host. +# +# Usage: +# ./claude.sh --build # build the Docker image (one-time) +# ./claude.sh # start Claude Code interactively +# ./claude.sh --shell # get a bash shell instead +# ./claude.sh -- -p "prompt" # pass extra args to claude + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# --- Defaults (overridable via environment) --- +CLAUDE_IMAGE="${CLAUDE_IMAGE:-openroad/flow-claude:latest}" +CONTAINER_NAME="${CONTAINER_NAME:-claude-orfs}" + +# --- Argument parsing --- +SHELL_MODE=0 +BUILD_IMAGE=0 +CLAUDE_ARGS=() + +usage() { + cat < /dev/null 2>&1; then + echo "Image ${CLAUDE_IMAGE} not found. Building automatically..." + _build_image +fi + +# --- Determine the user's home inside the container --- +# The entrypoint creates /home/claude-user for the mapped user. +CONTAINER_HOME="/home/claude-user" + +# --- Assemble docker run arguments --- +DOCKER_RUN_ARGS=( + --rm -it + --name "${CONTAINER_NAME}" + -v "${SCRIPT_DIR}:/workspace" + -e "HOST_UID=$(id -u)" + -e "HOST_GID=$(id -g)" + -w /workspace +) + +# Pass through optional environment variables +for var in ANTHROPIC_API_KEY ANTHROPIC_MODEL CLAUDE_CODE_MAX_TURNS \ + CLAUDE_CODE_USE_BEDROCK \ + AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_REGION \ + GOOGLE_APPLICATION_CREDENTIALS; do + if [[ -v "$var" ]] && [[ -n "${!var}" ]]; then + DOCKER_RUN_ARGS+=(-e "${var}=${!var}") + fi +done + +# SSH agent forwarding (for git push/pull over SSH) +if [[ -n "${SSH_AUTH_SOCK:-}" ]]; then + DOCKER_RUN_ARGS+=( + -v "${SSH_AUTH_SOCK}:/tmp/ssh-agent.sock" + -e "SSH_AUTH_SOCK=/tmp/ssh-agent.sock" + ) +fi + +# Host gitconfig (read-only, for user.name / user.email) +if [[ -f "${HOME}/.gitconfig" ]]; then + DOCKER_RUN_ARGS+=(-v "${HOME}/.gitconfig:${CONTAINER_HOME}/.gitconfig:ro") +fi + +# Persist Claude Code settings / history / memory across runs +CLAUDE_CONFIG_DIR="${HOME}/.claude" +mkdir -p "${CLAUDE_CONFIG_DIR}" +DOCKER_RUN_ARGS+=(-v "${CLAUDE_CONFIG_DIR}:${CONTAINER_HOME}/.claude") + +# Claude Code config file (lives next to ~/.claude/, not inside it) +if [[ -f "${HOME}/.claude.json" ]]; then + DOCKER_RUN_ARGS+=(-v "${HOME}/.claude.json:${CONTAINER_HOME}/.claude.json") +fi + +# Persist Bazel cache (avoids re-downloading hundreds of MB each run) +BAZEL_CACHE_DIR="${BAZEL_CACHE_DIR:-${HOME}/.cache/bazel-claude}" +mkdir -p "${BAZEL_CACHE_DIR}" +DOCKER_RUN_ARGS+=(-v "${BAZEL_CACHE_DIR}:${CONTAINER_HOME}/.cache/bazel") + +# --- Run --- +if [[ "${SHELL_MODE}" -eq 1 ]]; then + exec docker run \ + "${DOCKER_RUN_ARGS[@]}" \ + "${CLAUDE_IMAGE}" \ + bash +else + exec docker run \ + "${DOCKER_RUN_ARGS[@]}" \ + "${CLAUDE_IMAGE}" \ + claude --dangerously-skip-permissions "${CLAUDE_ARGS[@]}" +fi diff --git a/docker/Dockerfile.claude b/docker/Dockerfile.claude new file mode 100644 index 0000000000..545431f306 --- /dev/null +++ b/docker/Dockerfile.claude @@ -0,0 +1,57 @@ +# Docker image for running Claude Code with --dangerously-skip-permissions +# inside a container that can build/test OpenROAD (CMake + Bazel) and run ORFS. +# +# Usage: see claude.sh at the repo root. + +ARG fromImage=openroad/flow-ubuntu22.04-dev:latest + +FROM ${fromImage} + +# --- Bazel support --- + +# Java 21 (required by Bazel 8.x) +RUN apt-get update \ + && apt-get install -y --no-install-recommends openjdk-21-jre-headless \ + && rm -rf /var/lib/apt/lists/* + +# Bazelisk (respects .bazelversion to download the correct Bazel release) +RUN curl -Lo /usr/local/bin/bazelisk \ + https://github.com/bazelbuild/bazelisk/releases/latest/download/bazelisk-linux-amd64 \ + && chmod +x /usr/local/bin/bazelisk \ + && ln -sf /usr/local/bin/bazelisk /usr/local/bin/bazel + +# --- Claude Code support --- + +# Node.js 22 LTS (Claude Code requires Node 18+) +RUN curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \ + && apt-get install -y --no-install-recommends nodejs \ + && rm -rf /var/lib/apt/lists/* + +# Claude Code CLI +RUN npm install -g @anthropic-ai/claude-code + +# --- Convenience --- + +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + sudo \ + less \ + jq \ + ripgrep \ + && rm -rf /var/lib/apt/lists/* + +# VS Code CLI (code-tunnel for remote editing) +RUN curl -fsSL "https://code.visualstudio.com/sha/download?build=stable&os=cli-linux-x64" \ + -o /tmp/vscode-cli.tar.gz \ + && tar -xzf /tmp/vscode-cli.tar.gz -C /usr/local/bin \ + && rm /tmp/vscode-cli.tar.gz + +# Trust all directories (safe inside container; the container IS the boundary) +RUN git config --system safe.directory '*' + +# Entrypoint that maps host UID/GID then drops privileges +COPY claude-entrypoint.sh /usr/local/bin/claude-entrypoint.sh +RUN chmod +x /usr/local/bin/claude-entrypoint.sh + +WORKDIR /workspace +ENTRYPOINT ["claude-entrypoint.sh"] diff --git a/docker/claude-entrypoint.sh b/docker/claude-entrypoint.sh new file mode 100755 index 0000000000..18b77b98c9 --- /dev/null +++ b/docker/claude-entrypoint.sh @@ -0,0 +1,62 @@ +#!/usr/bin/env bash +# Entrypoint for the Claude Code container. +# Creates a user matching the host UID/GID so that files created +# inside the container have the correct ownership on the host volume. +set -e + +if [ -n "${HOST_UID}" ] && [ -n "${HOST_GID}" ]; then + USER_NAME="claude-user" + USER_HOME="/home/${USER_NAME}" + + # Create group (reuse existing if GID is taken) + if ! getent group "${HOST_GID}" > /dev/null 2>&1; then + groupadd --gid "${HOST_GID}" "${USER_NAME}" + fi + GROUP_NAME=$(getent group "${HOST_GID}" | cut -d: -f1) + + # Create user (reuse existing if UID is taken) + if ! getent passwd "${HOST_UID}" > /dev/null 2>&1; then + mkdir -p "${USER_HOME}" + useradd \ + --uid "${HOST_UID}" \ + --gid "${HOST_GID}" \ + --groups sudo \ + --no-create-home \ + --home-dir "${USER_HOME}" \ + --shell /bin/bash \ + "${USER_NAME}" + else + USER_NAME=$(getent passwd "${HOST_UID}" | cut -d: -f1) + USER_HOME=$(getent passwd "${HOST_UID}" | cut -d: -f6) + fi + + # Passwordless sudo + echo "${USER_NAME} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/claude-user + chmod 0440 /etc/sudoers.d/claude-user + + export HOME="${USER_HOME}" + chown "${HOST_UID}:${HOST_GID}" "${USER_HOME}" + touch "${USER_HOME}/.hushlogin" + + # Set up ORFS environment (PATH to tools, FLOW_HOME, etc.) + # Sourced here; inherited through setpriv into all child processes. + if [ -f /workspace/env.sh ]; then + set +e + . /workspace/env.sh > /dev/null + set -e + fi + + exec setpriv \ + --reuid "${HOST_UID}" \ + --regid "${HOST_GID}" \ + --init-groups \ + -- "$@" +else + # No UID/GID mapping requested; run as root + if [ -f /workspace/env.sh ]; then + set +e + . /workspace/env.sh > /dev/null + set -e + fi + exec "$@" +fi diff --git a/docs/toc.yml b/docs/toc.yml index 5fb7c2205e..722c933ca5 100644 --- a/docs/toc.yml +++ b/docs/toc.yml @@ -11,6 +11,8 @@ entries: entries: - file: user/DockerShell.md title: Docker Shell utility + - file: user/ClaudeCode.md + title: Claude Code in Docker - file: user/BuildLocally.md title: Build Locally - file: user/BuildWithWSL.md diff --git a/docs/user/ClaudeCode.md b/docs/user/ClaudeCode.md new file mode 100644 index 0000000000..c4128cb78f --- /dev/null +++ b/docs/user/ClaudeCode.md @@ -0,0 +1,162 @@ +# 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 src/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) | +| `$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-ubuntu22.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. Creates a user matching the host UID/GID +2. Sources `env.sh` to set up tool paths +3. Drops privileges via `setpriv` before running the command + +This follows the same pattern as `tools/OpenROAD/etc/docker-entrypoint.sh` +and the `--user` mode in `flow/util/docker_shell`. + +### `claude.sh` + +Wrapper script that assembles the `docker run` command with the correct +volume mounts, environment variables, and entrypoint arguments. + +## 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 +- 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. +::: From 0e1f9cf692b68d221bf790e56ef922531d5200ef Mon Sep 17 00:00:00 2001 From: Matt Liberty Date: Mon, 13 Apr 2026 15:23:38 +0000 Subject: [PATCH 2/2] claude-docker: fix VS Code CLI URL and non-interactive docker run The cli-linux-x64 identifier on code.visualstudio.com/sha/download returns 404; use update.code.visualstudio.com/latest/cli-linux-x64/stable, which delivers the glibc-linked binary that matches the Ubuntu base. Also stop forcing -it unconditionally, which broke CI-style invocations with "cannot attach stdin to a TTY-enabled container". Allocate a TTY only when both stdin and stdout are terminals; otherwise pass -i so piped input still reaches the container. Signed-off-by: Matt Liberty --- claude.sh | 14 +++++++++++++- docker/Dockerfile.claude | 8 ++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/claude.sh b/claude.sh index c74828daa9..6252e76989 100755 --- a/claude.sh +++ b/claude.sh @@ -86,7 +86,7 @@ CONTAINER_HOME="/home/claude-user" # --- Assemble docker run arguments --- DOCKER_RUN_ARGS=( - --rm -it + --rm --name "${CONTAINER_NAME}" -v "${SCRIPT_DIR}:/workspace" -e "HOST_UID=$(id -u)" @@ -94,6 +94,18 @@ DOCKER_RUN_ARGS=( -w /workspace ) +# TTY/stdin handling for interactive, CI, and piped-input cases: +# * terminal in + terminal out → -it (normal interactive use) +# * anything else → -i (pass stdin through; no TTY) +# Forcing -it unconditionally breaks non-interactive use +# ("cannot attach stdin to a TTY-enabled container"), and dropping -i +# would silently discard piped input like `cat prompt.txt | claude.sh ...`. +if [[ -t 0 && -t 1 ]]; then + DOCKER_RUN_ARGS+=(-it) +else + DOCKER_RUN_ARGS+=(-i) +fi + # Pass through optional environment variables for var in ANTHROPIC_API_KEY ANTHROPIC_MODEL CLAUDE_CODE_MAX_TURNS \ CLAUDE_CODE_USE_BEDROCK \ diff --git a/docker/Dockerfile.claude b/docker/Dockerfile.claude index 545431f306..3238c252df 100644 --- a/docker/Dockerfile.claude +++ b/docker/Dockerfile.claude @@ -40,8 +40,12 @@ RUN apt-get update \ ripgrep \ && rm -rf /var/lib/apt/lists/* -# VS Code CLI (code-tunnel for remote editing) -RUN curl -fsSL "https://code.visualstudio.com/sha/download?build=stable&os=cli-linux-x64" \ +# VS Code CLI (code-tunnel for remote editing). +# Use the glibc build to match the Ubuntu base image. (The cli-alpine-x64 +# variant on code.visualstudio.com is statically linked and also works, +# but the linux-x64 build from update.code.visualstudio.com is the +# canonical match for glibc distros.) +RUN curl -fsSL "https://update.code.visualstudio.com/latest/cli-linux-x64/stable" \ -o /tmp/vscode-cli.tar.gz \ && tar -xzf /tmp/vscode-cli.tar.gz -C /usr/local/bin \ && rm /tmp/vscode-cli.tar.gz