Claude Code in Docker: dev container, image, sign-in, sandboxes
Run Claude Code in a Docker container: Anthropic's dev container and firewall, your own image with a config volume, signing in with no browser, Docker Sandboxes.
Claude Code runs in an ordinary Linux container, as a non-root user if you want to skip its permission prompts. Install it in the image with curl -fsSL https://claude.ai/install.sh | bash (after WORKDIR /tmp, or the install can hang), mount a named volume at the user’s ~/.claude and set CLAUDE_CONFIG_DIR to that path, so the sign-in and .claude.json survive a rebuild. Sign in once inside with claude auth login (open the URL on your own computer, paste the code back), or pass CLAUDE_CODE_OAUTH_TOKEN from claude setup-token or ANTHROPIC_API_KEY at run time, never as a build argument or ENV. Anthropic publishes a reference dev container with a default-deny firewall for running with --dangerously-skip-permissions, which the CLI refuses as root. Docker’s own Docker Sandboxes (sbx run claude) runs it in a microVM instead.
Three ways to run it, and who each suits
| Route | What you get | Suits |
|---|---|---|
Anthropic’s reference dev container | A Node 20 image, Claude Code, a per-project config volume, and an iptables firewall that allows a short list of hosts | Working in VS Code or another Dev Containers editor, and running unattended with the permission checks off |
Your own image with docker run or Compose | Whatever you put in it; you decide the user, the volume, the network and the credential | A server, CI, or a terminal-only workflow |
Docker Sandboxes ( sbx) | A microVM per sandbox with its own Docker daemon, Claude Code started with permissions bypassed | A laptop or a machine with KVM where you want a VM boundary without building one |
If containers themselves are new, what a Docker container is covers images, layers and why a container forgets everything not on a volume, which is the problem most of this page solves.
Anthropic’s reference dev container
The .devcontainer folder in Anthropic’s repository (read at v2.1.331, the release of November 23, 2026) is three files. Anthropic’s dev container page calls it “a working example rather than a maintained base image”: copy it and adjust it.
Dockerfile:FROM node:20, git, zsh, gh, jq and the firewall tools (iptables,ipset), thennpm install -g @anthropic-ai/claude-code@${CLAUDE_CODE_VERSION}with that build argument defaulting tolatest. It creates/home/node/.claude, hands it to thenodeuser, letsnoderun only the firewall script through sudo, and ends onUSER node.devcontainer.json:"remoteUser": "node", the workspace bind-mounted at/workspace, a volumeclaude-code-config-${devcontainerId}at/home/node/.claudewithCLAUDE_CONFIG_DIRpointing there,--cap-add=NET_ADMINand--cap-add=NET_RAWso the firewall can be set, andsudo /usr/local/bin/init-firewall.shas thepostStartCommand, which the editor waits for.init-firewall.sh: drops everything by default, keeps Docker’s DNS, outbound DNS, SSH on port 22 and the host’s own /24, and allows GitHub’s published address ranges plus the addresses ofregistry.npmjs.org,api.anthropic.com,sentry.io,statsig.comand three VS Code hosts. Everything else is rejected. It then checks thathttps://example.comis unreachable andhttps://api.github.comreachable, and fails if not.
Two things to know before relying on it. The firewall’s list has no claude.ai or platform.claude.com, and Anthropic’s network requirements say claude.ai account sign-in uses the first and that “OAuth token exchange, refresh, and revocation” go to the second for every account. If signing in with a Claude subscription fails or a login stops refreshing with the firewall up, add those two hosts to the script. And the image is Node 20, while Anthropic’s setup page says the npm package requires Node 22; on older Node, npm prints an EBADENGINE warning and the binary still runs.
To add Claude Code to a dev container you already have, the feature ghcr.io/anthropics/devcontainer-features/claude-code:1.0 installs the latest release (the :1.0 pins the install script, not Claude Code); the config volume is set the same way, as in Claude Code in VS Code.
--dangerously-skip-permissions in a container
Anthropic’s sandbox environments page says to “always run --dangerously-skip-permissions sessions inside a container, a VM, or the sandbox runtime”, and that on Linux and macOS Claude Code “refuses to start with this flag when running as root”. The message, as reported in issue #9184 (Claude Code 2.0.10), is:
--dangerously-skip-permissions cannot be used with root/sudo privileges for security reasonsCopiedThe fix is a non-root user in the image, which every example on this page has. The container narrows what a bypassed session reaches; it does not protect what is inside it. The dev container page warns that with the flag, a malicious project can exfiltrate “anything accessible inside the container, including the Claude Code credentials stored in ~/.claude”, and that Claude “can still modify any file in the bind-mounted workspace, which appears directly on your host.” Its advice: trusted repositories only, no host secrets such as ~/.ssh or cloud credential files mounted in, short-lived or repository-scoped tokens, and an egress limit like the firewall above. How the flag compares with auto mode is in running Claude Code without permission prompts.
Your own image on a server
Without an editor, a small image does the same job. This one uses the native installer, a user whose UID matches yours on the host (for the bind mount, below), and the config directory Anthropic’s own Dockerfile prepares:
FROM debian:bookworm-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends ca-certificates curl git bash \
&& rm -rf /var/lib/apt/lists/*
ARG USER_UID=1000
RUN useradd --create-home --uid "$USER_UID" --shell /bin/bash claude \
&& mkdir -p /workspace /home/claude/.claude \
&& chown claude:claude /workspace /home/claude/.claude
COPY claude-entrypoint /usr/local/bin/claude-entrypoint
RUN chmod 755 /usr/local/bin/claude-entrypoint
USER claude
WORKDIR /tmp
ARG CLAUDE_CODE_VERSION=stable
RUN curl -fsSL https://claude.ai/install.sh | bash -s "$CLAUDE_CODE_VERSION"
ENV PATH="/home/claude/.local/bin:${PATH}" \
CLAUDE_CONFIG_DIR=/home/claude/.claude \
DISABLE_AUTOUPDATER=1
WORKDIR /workspace
ENTRYPOINT ["/usr/local/bin/claude-entrypoint"]CopiedWORKDIR /tmpbefore the installer is Anthropic’s fix for an install that hangs in Docker. An install killed with exit code 137 usually means the build ran out of memory: the installer needs roughly 512 MB free, and Claude Code requires at least 4 GB of RAM to run.- The version is a build argument (
stable,latestor a number such as2.1.331). Native installs update themselves in the background;DISABLE_AUTOUPDATER=1stops that, so the container runs the version the image was built with and you update by rebuilding. CLAUDE_CONFIG_DIRis what keeps.claude.json(account details, personal MCP servers, per-project trust and the record that setup is done) inside the volume. Without it that file sits in the home folder and, in the dev container page’s words, is “discarded on rebuild”, and Claude Code repeats first-run setup.
The entrypoint reads a token from a secret file if one is mounted, then starts Claude Code with whatever arguments you pass:
#!/bin/sh
set -e
if [ -r /run/secrets/claude_oauth_token ]; then
CLAUDE_CODE_OAUTH_TOKEN="$(cat /run/secrets/claude_oauth_token)"
export CLAUDE_CODE_OAUTH_TOKEN
fi
exec claude "$@"CopiedBuild it with your UID, then run it in a project with a named volume for the config:
docker build --build-arg USER_UID=$(id -u) -t claude-code .
docker run -it --rm \
-v "$PWD":/workspace \
-v claude-config:/home/claude/.claude \
claude-codeCopiedArguments after the image name go to claude: ending the command with claude-code auth login signs in, and with claude-code -p "run the tests" runs one prompt and exits.
Signing in with no browser in the container
There are three ways. If more than one is present, the API key wins over the token, and both win over a stored login:
- A Claude subscription, signed in once. Run
claude auth loginin the container, open the URL it prints in your own computer’s browser, and paste the code the browser shows back at thePaste code hereprompt; per Anthropic’s troubleshooting page, this command reads the code from standard input, which works where pasting into the interactive prompt does not. The login is written to.credentials.json(mode 0600) in the config directory, so on the volume it lasts as long as the volume does. - A subscription token,
CLAUDE_CODE_OAUTH_TOKEN. Runclaude setup-tokenon any machine with a browser; it prints a one-year token and saves it nowhere. Per Anthropic’s authentication page it needs a Pro, Max, Ultra, Team or Enterprise plan and “can only make model requests”: no Remote Control, no claude.ai connectors. A run with--bareignores it. More in claude setup-token. - A Console API key,
ANTHROPIC_API_KEY, billed per token. Interactive sessions ask once before using it; with-pit “is always used when present”, even over a subscription login.
Keeping the credential out of the image
Docker’s build secrets page is plain about it: “Build arguments and environment variables are inappropriate for passing secrets” because “they persist in the final image.” No ARG or ENV with a token or key, and no COPY of one. Give it at run time instead. The short way passes a variable by name only, and Docker copies its value from your shell, where it is already exported, so it never appears in the command:
read -rs CLAUDE_CODE_OAUTH_TOKEN # paste the token, press Enter; nothing is shown or kept in history
export CLAUDE_CODE_OAUTH_TOKEN
docker run -it --rm -e CLAUDE_CODE_OAUTH_TOKEN \
-v "$PWD":/workspace -v claude-config:/home/claude/.claude claude-codeCopiedWith Compose, a secret is mounted as a file at /run/secrets/<name>, which the entrypoint above reads; Docker’s secrets page notes that environment variables in a Compose file “are often available to all processes” and can end up in logs:
services:
claude:
build:
context: .
args:
USER_UID: "1000" # your id -u
stdin_open: true
tty: true
volumes:
- ./:/workspace
- claude-config:/home/claude/.claude
secrets:
- claude_oauth_token
volumes:
claude-config:
secrets:
claude_oauth_token:
file: ${HOME}/.config/claude-docker/oauth_tokenCopiedStart a session with docker compose run --rm claude, or a one-off prompt with docker compose run --rm claude -p "…". Use run rather than exec into a running container: the token is set by the entrypoint, and a process started with exec does not go through it. Create the token file so the token never passes through your shell history: mkdir -p ~/.config/claude-docker && (umask 077; cat > ~/.config/claude-docker/oauth_token), then paste the token, press Enter and Ctrl+D. It sits outside the project, at mode 600, so it is neither in the bind-mounted workspace nor anywhere git could pick it up; the user built with your UID can still read it. Inside the container the token is readable by anything Claude runs, which is the dev container page’s warning again.
Docker Sandboxes: sbx run claude
Anthropic’s sandbox page lists Docker Sandboxes under virtual machines: “a microVM with its own Docker daemon and workspace sync”, “a free, standalone product from Docker that does not require Docker Desktop.” Docker’s own overview narrows that: the sbx CLI and local sandboxes are free, including for commercial work, while its cloud sandboxes are billed pay-as-you-go. Per Docker’s install page (November 2026), local sandboxes need macOS 14 on Apple silicon, Windows 11 with Windows Hypervisor Platform, or Ubuntu 24.04 or later with KVM, and inside a VM, nested virtualization. On Ubuntu:
curl -fsSL https://get.docker.com | sudo REPO_ONLY=1 sh
sudo apt install docker-sbx
sbx login
sbx run claude ~/my-projectCopiedPer Docker’s Claude Code page, with no extra arguments the sandbox runs claude --dangerously-skip-permissions. Store an API key with sbx secret set anthropic; without one, use /login inside Claude Code for a subscription. Your own ~/.claude is not carried in (“Sandboxes don’t pick up user-level configuration from your host”), only the project’s, and conversations survive removing a sandbox only if you add its session mixin when creating it. The base image is docker/sandbox-templates:claude-code, which you can build on. sbx login signs in to Docker in a browser.
What breaks
Permission deniedwriting to the project. On Linux, bind-mounted files keep their host owner and mode, “the exact same permissions as outside the container” per VS Code’s container docs. If the container user’s UID is not yours, it cannot write your files, and files it creates come out owned by another UID. Build with--build-arg USER_UID=$(id -u); dev containers in VS Code match the UID for you on Linux.- Mounting
~/.claude.jsonas a file. With-v, a host path that does not exist yet is created, and Docker’s page says “it’s always created as a directory”, so a first run mounts a directory where Claude Code expects a file. Mount one directory and setCLAUDE_CONFIG_DIRinstead, as above. - Setup repeats on every new container. The volume is not mounted at the path
CLAUDE_CONFIG_DIRnames, or the variable is missing. - Claude Code’s own Bash sandbox fails inside. bubblewrap cannot mount a fresh
/procin an unprivileged container and reportsCan't mount proc on /newroot/proc: Operation not permitted.sandbox.enableWeakerNestedSandbox: truegets it running; Anthropic says to use it only when the container is already the boundary you rely on. The rest is in Claude Code’s sandbox. - Docker commands from inside. Mounting the host’s
/var/run/docker.sockso Claude can run Docker hands it the host: Anthropic’s sandboxing page says access to that socket “effectively grants access to the host system”. Docker Sandboxes gives each sandbox its own daemon instead. - A login that stops working behind a firewall. Allow
api.anthropic.com,claude.aiandplatform.claude.comfor a subscription login, anddownloads.claude.aiif the native installer runs behind it.
On a server, the container sits inside a machine you also have to look after; Claude Code on Linux covers installing it on the host itself when a container is more than you need.