# How to run Claude Code into a container.

This is the equivalent of `dev/build/docker-vibe` (Mistral Vibe) for Claude Code.
The image provides the same tools (PHP, phpunit, phpstan, phan, phpcs, hurl, gh, tx, pre-commit, rtk, mariadb client, ...),
including Playwright for Python and Chromium (browsers into `/opt/pw-browsers`, full browser also at `/usr/local/bin/chromium`)
to test pages in a browser and take screenshots, and the same protections (no email can be sent from the container). See `dev/build/docker-vibe/README.md` for the details
of the common parts. The files `open-on-host`, `xdg-open-shim`, `smtpblock.sh`, `sendmail-stub` and `bashrc-prompt` are shared with docker-vibe: they are
taken from the directory `dev/build/docker-vibe` when the image is built.

## Prepare AI api key

The configuration directory `~/.claude` of the host is mounted at the same path into the container, so the container uses
the same credentials (`~/.claude/.credentials.json`, created by a `claude` login on the host), settings, plugins, memory and
sessions as the host.

If you never logged in on the host, run `claude` once on the host to log in, or log in from inside the container (the login
URL is opened in the browser of the host, see "Clipboard and links" into `dev/build/docker-vibe/README.md`).

You can also use an API key or a long lived token: when the environment variable `ANTHROPIC_API_KEY` or `CLAUDE_CODE_OAUTH_TOKEN`
(created with `claude setup-token`) is set on the host, it is passed to the container.

The state file of Claude Code (onboarding, trusted directories, MCP servers, ...) is a single file `~/.claude.json` that is replaced
at each write, so it can not be bind mounted. Into the container, `~/.claude.json` is a link to `~/.claude/.claude.json` (into the
mounted directory), and `claudes.sh` copies the `~/.claude.json` of the host into `~/.claude/.claude.json` at each launch: the
container gets the MCP servers, the trusted directories and the theme of the host, but the changes made into the container
(for example a MCP server added with `claude mcp add`) are lost at the next launch. Add them on the host instead.

## You can add an alias into your /etc/bash.bashrc or ~/.bashrc the line
````
alias claudes='dev/build/docker-claude/claudes.sh'
````

## Prepare GitHub token key

````
gh auth login -h github.com -p https -w 
(or for a branch maintener: gh auth login -h github.com -p https -w -s workflow)
gh auth status
````


## Run Claude Code into a container
Run from the root of your repository:
````
dev/build/docker-claude/claudes.sh
````

This script will build the docker image, and then run it with the current directory mounted into the container and launch `claude`.

Claude is launched by default without the option `--dangerously-skip-permissions` (mode `--no-yolo`), so permissions are asked.
If you want to run claude with this option (the container being the sandbox), you can run
````
claudes --yolo
````

When you exit claude, the container is stopped too, so you return immediately to your host.
If you want to stay into the container with a bash shell after claude has ended, you can run
````
claudes --no-exit
````

If you want to run claude in Remote Control mode (`claude remote-control`, to drive the session from claude.ai/code
or the Claude mobile app), you can run
````
claudes --rc
````
The sessions are then started with `--permission-mode auto`: the safe actions are approved automatically, the risky or
outward-facing ones (push, pull request, ...) are still asked. Add `--yolo` to have all the permissions automatically
accepted with `--permission-mode bypassPermissions`.

The other arguments are passed to claude, for example to resume the last conversation:
````
claudes --continue
````

Each time the image is rebuilt with changes, the previous image is kept untagged and fills the disk. To list the images generated by `claudes.sh` and remove the obsolete ones (without building nor running the container), you can run
````
claudes --clean-images
````
To only list these images (the current one, with its status against the build context, and the obsolete ones) and the containers using them, without removing anything, run `claudes --list-images`.
A container still running with an obsolete image prevents its removal (even with `docker rmi -f`), so the containers still using an obsolete image are stopped and removed first (the containers started by `claudes.sh` use `--rm`: stopping them removes them). Their Claude Code sessions are not lost, they are stored in `~/.claude` of the host and can be resumed with `claudes --continue`.

Into the container, the directory `.claude` (with the links `skills`, `agents` and `hooks` to the same entries of `.agents`) and
the link `CLAUDE.md` (to `.agents/AGENTS.md`) are created at the root of the repository when they do not exist, so Claude Code
uses the instructions, skills and agents shared by all the AI agents.
`.claude` is a real directory and not a link to `.agents`: Claude Code refuses to create its worktrees (`.claude/worktrees/`)
when `.claude` is a link. An old link `.claude` -> `.agents` is replaced automatically.

### Build (or rebuild) image of the container.
Claude Code is installed into the image when it is built, and its auto-updater is disabled. To get a new version of Claude Code, rebuild the image with:
````
claudes --no-cache
````
Without `--no-cache`, `claudes.sh` builds the image only when its build context has changed since the last build (the directory `dev/build/docker-claude` without `claudes.sh` and `README.md`, and the files shared with `docker-vibe`; a hash of it is stored as a label of the image). Otherwise the last image built is used as is, whatever the directory `claudes.sh` is launched from: after a rebuild with `claudes --no-cache` from one repository, the containers launched later from the other repositories use this new image.
The build context does not contain any file of the repository: the pre-commit hooks are not preloaded into the image, they are kept into the directory `~/.cache/docker-claude/pre-commit` of the host, mounted into the container (see the pre-commit section of `dev/build/docker-vibe/README.md`).

### Send emails
The outbound SMTP ports are blocked like for docker-vibe. To start a container that is allowed to send emails, run it with:
````
DOCKER_ALLOW_SMTP=1 claudes
````

### Mail logs of the host
Like for docker-vibe, the files `/var/log/mail.log`, `/var/log/phpmail.log` and `/var/log/phpsendmail.log` are each mounted
read-only at the same path into the container when they exist on the host, and the user of the container is added to the groups
owning them (except `root`). After a rotation of the log on the host, the container keeps showing the old file until it is restarted.

## Documents produced by the agent
Like for docker-vibe, `claudes.sh` creates the directory `/tmp/agents_documents` on the host and mounts it at the same path into the container,
so the documents produced by the agent for the user (reports, slides, graphs, exports, test artifacts, ...) are shared with the host
without touching the git working directory of the project. The directory is shared between the host and all the AI agent containers
running on it: each agent should write into a subdirectory named after itself (for example `/tmp/agents_documents/claude/`).
Beware: `/tmp` is purged at reboot: the documents must be taken out of the host directory before a reboot of the host.
