Claude Code in Docker and Dev Containers: Official Setup, Safe Keys, Persistent Config and Firewall
Claude Code runs well inside Docker. As of 2026-11-25, Anthropic publishes an official Dev Container Feature (ghcr.io/anthropics/devcontainer-features/claude-code:1.0) and a reference dev container in the anthropics/claude-code repository with an egress firewall, persistent volumes and a non-root user. To point a containerised Claude Code at a third-party endpoint, pass ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN at run time, never bake them into the image, and mount a volume at ~/.claude with CLAUDE_CONFIG_DIR set to the same path so settings survive rebuilds. QCode is an independent third-party API service and is not affiliated with Anthropic.
Updated 2026-11-26
Key facts
- Official route: add the feature
ghcr.io/anthropics/devcontainer-features/claude-code:1.0to.devcontainer/devcontainer.json. The:1.0tag pins the install script, not the Claude Code version; the feature installs the latest release, which then auto-updates. - Config lives in two places:
~/.claude/(settings, credentials, history) and~/.claude.json(account, MCP servers, project trust). SetCLAUDE_CONFIG_DIRso both land in one mounted volume. - Reference firewall:
init-firewall.shsets iptables policies to DROP and allows only listed domains plus GitHub ranges. If you use a custom endpoint, add it to the list. - Skipping permissions:
--dangerously-skip-permissionsis refused when Claude Code runs as root, and Anthropic says it belongs only inside an isolated boundary such as a container or VM, with trusted repositories. - Pinning versions: to pin Claude Code, install it with
npm install -g @anthropic-ai/claude-code@X.Y.Zin your Dockerfile and setDISABLE_AUTOUPDATER=1.
Why run Claude Code in a container?
Three reasons come up again and again:
- The same environment for everyone. A dev container gives each teammate the same toolchain, so "works on my machine" stops being a Claude Code problem too.
- A blast-radius limit. Commands Claude runs execute inside the container, not on your laptop. Your project folder is still bind-mounted and writable, but your home directory, SSH keys and cloud credentials are not visible unless you mount them.
- Unattended work. Long agent runs, CI jobs and overnight refactors are safer when the network and filesystem are fenced off.
A container isn't a magic shield, though. Anthropic's own warning is blunt: with --dangerously-skip-permissions, a malicious project can still exfiltrate anything reachable inside the container, including the Claude Code credentials in ~/.claude. Use it with repositories you trust and keep host secrets out.
Option 1: the official Dev Container Feature
If your editor supports the Dev Containers spec (VS Code, GitHub Codespaces, JetBrains IDEs, Cursor), this is the shortest path. The feature installs Node.js if the base image lacks it, installs Claude Code, and in VS Code also adds the Claude Code extension.
// .devcontainer/devcontainer.json
{
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
"features": {
"ghcr.io/anthropics/devcontainer-features/claude-code:1.0": {}
},
"remoteUser": "vscode",
"mounts": [
"source=claude-code-config-${devcontainerId},target=/home/vscode/.claude,type=volume"
],
"containerEnv": {
"CLAUDE_CONFIG_DIR": "/home/vscode/.claude",
"ANTHROPIC_BASE_URL": "https://api.qcode.cc/api"
},
"remoteEnv": {
"ANTHROPIC_AUTH_TOKEN": "${localEnv:QCODE_API_KEY}"
}
}
What each part does:
mountscreates a named volume per project (${devcontainerId}keeps projects apart, the same pattern the reference container uses).CLAUDE_CONFIG_DIRmakes Claude Code write.claude.jsoninside that volume. Without it, mounting~/.claudealone still logs you out after a rebuild, because.claude.jsonsits outside the folder.ANTHROPIC_BASE_URLis not a secret, so it can live incontainerEnvand be committed.ANTHROPIC_AUTH_TOKENcomes fromQCODE_API_KEYon your host via${localEnv:...}, so thecr_key never appears in the repository. Putexport QCODE_API_KEY=cr_...in your host shell profile, then rebuild.
Rebuild with Dev Containers: Rebuild Container, open a terminal and run claude. In Codespaces, store the key as a Codespaces secret instead; Codespaces exposes secrets as environment variables inside the container.
If the build stops with Failed to install Node.js and npm, Anthropic's fix is to add "ghcr.io/devcontainers/features/node:1": {} above the Claude Code feature.
Option 2: the reference container with a firewall
The anthropics/claude-code repository ships a .devcontainer/ folder with three files: devcontainer.json, a Dockerfile and init-firewall.sh. Anthropic calls it a working example, not a maintained base image. As read on 2026-11-25, its devcontainer.json runs as user node, adds NET_ADMIN and NET_RAW through runArgs (needed for iptables inside the container), mounts claude-code-config-${devcontainerId} at /home/node/.claude, and runs sudo /usr/local/bin/init-firewall.sh as its postStartCommand.
The firewall script:
- sets INPUT, FORWARD and OUTPUT policies to DROP;
- allows DNS (UDP 53), SSH (TCP 22), loopback and the host's /24 network;
- allows GitHub's published IP ranges plus a fixed domain list:
registry.npmjs.org,api.anthropic.com,sentry.io,statsig.comand three VS Code domains; - checks itself:
https://example.commust fail andhttps://api.github.com/zenmust succeed.
If you use a custom endpoint, add it to that list, or every request from Claude Code will be rejected:
# init-firewall.sh (excerpt) - add the endpoint you actually use
for domain in \
"registry.npmjs.org" \
"api.anthropic.com" \
"api.qcode.cc" \
"sentry.io" \
"statsig.com" \
"marketplace.visualstudio.com" \
"vscode.blob.core.windows.net" \
"update.code.visualstudio.com"; do
Two caveats. The script resolves each domain once at startup, so if an endpoint's IP changes, re-run the script or restart the container. And Anthropic's sandbox docs note that on a third-party provider you should keep api.anthropic.com allowed too, because the WebFetch domain safety check still calls it unless you set skipWebFetchPreflight: true. If you also set CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1, telemetry and error reporting stop, so sentry.io and statsig.com are no longer needed.
Option 3: plain Docker, no editor
For servers, CI runners or anyone who doesn't use dev containers, a small image is enough.
# Dockerfile
FROM node:22-bookworm-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends git ripgrep jq ca-certificates curl \
&& rm -rf /var/lib/apt/lists/*
# Pin for reproducible builds; replace "latest" with a version you tested
ARG CLAUDE_CODE_VERSION=latest
RUN npm install -g @anthropic-ai/claude-code@${CLAUDE_CODE_VERSION}
# Pre-create the config dir so the named volume is owned by "node"
RUN mkdir -p /home/node/.claude && chown -R node:node /home/node/.claude
USER node
WORKDIR /workspace
ENV CLAUDE_CONFIG_DIR=/home/node/.claude \
DISABLE_AUTOUPDATER=1
Keep the key in an env file that never enters the image or Git:
# qcode.env (chmod 600; add to .gitignore and .dockerignore)
ANTHROPIC_BASE_URL=https://api.qcode.cc/api
ANTHROPIC_AUTH_TOKEN=cr_your_key
docker build -t claude-code:local .
# Interactive session on the current project
docker run -it --rm \
--env-file ./qcode.env \
-v "$PWD":/workspace \
-v claude-code-config:/home/node/.claude \
claude-code:local claude
# One-shot headless task with a turn cap
docker run --rm \
--env-file ./qcode.env \
-v "$PWD":/workspace \
-v claude-code-config:/home/node/.claude \
claude-code:local claude -p "Run the test suite and summarise failures" --max-turns 20
Rules that keep this safe:
- Never use
ENVorARGfor the key. Both end up in image metadata anddocker history. Pass it with--env-file,-e ANTHROPIC_AUTH_TOKEN(reads from your shell), Composeenv_file, or your CI's secret store. - Don't mount
~/.ssh,~/.awsor similar. Anthropic recommends repository-scoped or short-lived tokens instead. - Run as a non-root user. The
nodeuser in the official Node image works; Claude Code refuses--dangerously-skip-permissionsas root. - Pick the endpoint nearest to the server:
https://api.qcode.cc/api(global routing),https://asia.qcode.cc/apiorhttps://us.qcode.cc/api.
Settings, policy and MCP inside the container
- Team policy: Claude Code on Linux reads
/etc/claude-code/managed-settings.jsonat the highest precedence. Copy it in from your Dockerfile. Anyone with write access to the repo can remove that step, so for policy people must not bypass, Anthropic points to server-managed settings or MDM. - Permissions: commit allow and deny rules in
.claude/settings.json; see our settings.json and permissions guide. A good container default is to denyRead(./.env)andRead(./.env.*). - MCP servers: define them at project scope in
.mcp.json, install any binaries they need in the Dockerfile, and add remote MCP domains to the firewall allowlist. - Quiet the network:
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1turns off auto-updates, telemetry and error reporting. Note that setting it to0still disables that traffic; unset it to turn traffic back on.
When is skipping permissions acceptable?
Use this checklist before you type --dangerously-skip-permissions:
| Condition | Why it matters |
|---|---|
| Running inside a container or VM, as a non-root user | The CLI refuses root, and the boundary is your only protection once prompts are gone |
| Egress restricted to an allowlist | Limits where data can be sent if a prompt injection succeeds |
| No host secrets mounted | A bypassed session can read anything inside the container |
| A repository you trust | Untrusted code belongs in a dedicated VM |
| You review the diff afterwards | Edits to the bind-mounted workspace appear on your host immediately |
If you only want fewer prompts on your own machine, Anthropic suggests auto mode or the built-in Bash sandbox (/sandbox) instead. Organisations can block bypass entirely with permissions.disableBypassPermissionsMode set to "disable" in managed settings.
Running containers on a cloud server
Agents that run for hours are a natural fit for a small always-on server: your laptop can sleep while the container keeps working inside tmux. If you need a hosted machine, see QCode's cloud servers for current regions, specifications and prices. A minimal routine:
# On the server (Ubuntu): create a non-root user first
sudo adduser dev && sudo usermod -aG docker dev
su - dev
tmux new -s agent
docker run -it --rm --env-file ~/qcode.env \
-v ~/projects/app:/workspace -v claude-code-config:/home/node/.claude \
claude-code:local claude
# Detach with Ctrl-b then d; re-attach with: tmux attach -t agent
Harden SSH first (key login, then disable passwords). Our existing guides cover the non-container route: Claude Code on a cloud server and OpenCode on a cloud server.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Asked to sign in after every rebuild | .claude.json is outside the volume |
Set CLAUDE_CONFIG_DIR to the volume path |
Unable to connect to API inside the firewall container |
Endpoint not allowlisted, or its IP changed | Add the domain to init-firewall.sh and restart |
EACCES writing ~/.claude |
Named volume created as root | mkdir and chown the folder in the Dockerfile before USER |
| Bypass flag rejected | Running as root | Set remoteUser or USER to a non-root account |
Failed to install Node.js and npm |
Base image without Node | Add the Node feature above the Claude Code feature |
FAQ
Is there an official Claude Code dev container?
Yes. Anthropic publishes the Dev Container Feature ghcr.io/anthropics/devcontainer-features/claude-code:1.0 and a reference .devcontainer/ in the anthropics/claude-code repository. The reference is a working example rather than a maintained base image, so copy and adapt it.
How do I pass my API key without committing it?
Use ${localEnv:QCODE_API_KEY} in remoteEnv for dev containers, a Codespaces secret, --env-file for plain Docker, or your CI's secret store. Never put the key in a Dockerfile ENV or ARG, because both are stored in the image.
Why do I have to log in again after rebuilding the container?
Claude Code keeps account and trust data in ~/.claude.json, which sits outside ~/.claude. Mount a volume at ~/.claude and set CLAUDE_CONFIG_DIR to the same path so both files persist.
The reference firewall blocks my requests. What should I change?
Add your endpoint, such as api.qcode.cc, to the domain loop in init-firewall.sh, then restart the container. Keep api.anthropic.com if you rely on WebFetch's safety check.
Can I use --dangerously-skip-permissions in Docker?
Only in an isolated container running as a non-root user, with restricted egress, no host secrets mounted and a repository you trust. Claude Code refuses the flag as root, and Anthropic warns that a bypassed session can still leak anything inside the container.
Which QCode endpoint should a container use?
Use https://api.qcode.cc/api by default, or asia.qcode.cc and us.qcode.cc for servers in those regions. Set it with ANTHROPIC_BASE_URL and allowlist the same domain in your firewall.
Do I need Docker to isolate Claude Code?
No. The built-in Bash sandbox (/sandbox) and Anthropic's sandbox runtime work without Docker, but the Bash sandbox only covers shell commands. For unattended runs, Anthropic recommends a container, a VM or the sandbox runtime.
Sources: Anthropic's Claude Code docs (Development containers, Choose a sandbox environment, Environment variables) and the anthropics/claude-code .devcontainer/ files; QCode docs on running agents on cloud servers and the /cloud page, all read 2026-11-25.
Run your agents on an always-on server
If you want containers that keep working after you close your laptop, see QCode cloud servers for current options. Personal API plans start from $9 per month, and Anthropic and OpenAI models are billed at the official API price ×1.0; see /pricing.