Get the free coreGet core

Run Claude Code in Docker: setup steps and safe settings on a Mac

Updated 6 min readSection 02 / Sandboxing
SHORT ANSWER

The quickest way to run Claude Code in Docker is Docker's own tool, Docker Sandboxes. In your project folder, run sbx run --name my-sandbox claude, then sign in with /login inside it, and your session token stays on your Mac. Each sandbox is a small virtual machine with its own Docker daemon, filesystem and network. Your project folder is shared in read-write, so Claude can still change your files.

If you prefer to build the container yourself, the checklist further down covers what it needs. If you would rather have your editor handle the container, see Claude Code devcontainer.

01What Claude Code in Docker changes

A container is a lightweight isolated environment for running programs, and Docker is the common tool for starting one. With Claude Code installed in a container, the commands it runs execute inside the container, not on your host machine. Edits to your project files still appear in your local repository.

That is why Anthropic's docs list containers among the isolated places for the no-prompt flag. Their dev container runs Claude Code as a non-root user, meaning an account without administrator rights, and confines command execution to the container. The background on that flag is on dangerously-skip-permissions.

02Set up Claude Code in Docker Sandboxes

Docker Sandboxes runs AI coding agents in isolated environments on your machine. You manage them with the sbx command-line tool. Each environment is a microVM, a very small virtual machine of its own. The tool and local sandbox compute are free, including for commercial work. You don't need Docker Desktop or Docker Engine for it. On a Mac it needs macOS Sonoma 14 or later and Apple silicon.

  1. Install sbx and sign in to Docker. Install it with Homebrew, then sign in; sbx login opens a browser for Docker's sign-in:
brew trust docker/tap
brew install docker/tap/sbx
sbx login
  1. Go to your project. The agent sees this folder and nothing else of your machine.
  2. Start Claude Code in a sandbox. The name is yours to choose:
cd ~/my-project
sbx run --name my-sandbox claude
  1. Pick a network policy when sbx asks. Balanced permits traffic to common development services and blocks other destinations unless you approve them. To skip the question in a script, run sbx policy init balanced before sbx run.
  2. Sign in to Claude. With a Claude subscription, type /login inside the sandbox and finish the OAuth sign-in. The session token stays on your host and is never stored inside the sandbox.

That is the whole setup. Know one default: without extra arguments, the sandbox starts Claude Code with --dangerously-skip-permissions, so it does not ask before commands. The sandbox walls are what keep that safe. Your ~/.claude settings from the Mac are not used inside; only the project's own configuration is. Claude Code now works in the sandbox. The packages it installs, the images it pulls and the containers it starts stay inside it, and your host system is untouched.

03Choose the network policy

The network policy decides where the agent can connect. It starts from a preset, and two presets keep traffic limited.

PresetWhat it does
BalancedPermits common development services, blocks other destinations unless you approve them
Locked DownEvery destination needs an allow rule from you or a kit

To open one more host, add a rule. For example, to let the agent reach the npm registry:

sbx policy allow network registry.npmjs.org

List the active rules with sbx policy ls. Keep the list short, because every host you allow is a place data can be sent. A broad host such as github.com is a wide opening. Start with Balanced and add rules only when a task fails for lack of access.

04Your project folder, stop and remove

When you run sbx run from a project directory without a workspace path, the current directory is mounted read-write. Claude's edits show up in your working tree on the Mac. If you want the agent kept away from your working tree, use clone mode, which gives it a private clone instead.

Day-to-day commands:

sbx ls
sbx stop my-sandbox
sbx rm my-sandbox

sbx ls lists your sandboxes. sbx stop stops one and sbx rm removes it. Removing deletes everything inside: installed packages, Docker images and, in clone mode, the in-sandbox Git clone. Files in your host working tree are unaffected.

05What a container does not change

Three holes stay open even in a well-built container.

The mounted folder. To work on your code, the container must see it. Claude can still modify any file in the bind-mounted workspace, and the changes appear directly on your host. A bind mount is a folder that the Mac and the container see as one. Other agents behave the same way: Gemini CLI's Docker sandbox mounts your working directory at the same path as on your Mac.

Whatever is reachable inside. With the no-prompt flag on, a dev container does not stop a malicious project from sending out anything it can reach inside, including the Claude Code credentials stored in ~/.claude. Sending data out like that is called exfiltration.

The network. Claude can also reach anything the container's network policy allows. An open network can still upload what the agent can read.

Anthropic puts it plainly: even a dev container gives substantial protection, but no system is completely immune to all attacks. Use it only with repositories you trust, and watch what Claude does.

06Build your own container instead

If you start a plain container yourself, it needs the same things Docker Sandboxes gives you. Check each item.

  1. Keep your work committed. The mounted folder can be changed freely. git restore brings paths in the working tree back from a restore source.
  2. Mount only the project. Nothing else from your Mac belongs in the container.
  3. Keep secrets out. Do not mount ~/.ssh or cloud credential files. Prefer repository-scoped or short-lived tokens.
  4. Use a non-root user. The CLI rejects the no-prompt flag when launched as root.
  5. Limit the network. Pair the flag with network egress restrictions to limit what a bypassed session can reach. Egress means traffic leaving the container.
  6. Start the agent inside. Run claude --dangerously-skip-permissions there, not on your Mac.

Anthropic's reference dev container does step 5 with a firewall script, init-firewall.sh, that limits outbound traffic to the destinations the script allows. Read it before you copy it. The walk-through is on the devcontainer page.

07Docker, dev container or built-in sandbox

SetupWhat it fencesWhere to read more
Docker SandboxesA microVM with its own Docker daemon, filesystem and networkThis page
Dev containerA container opened by your editor, described in a devcontainer.json fileClaude Code devcontainer
Built-in sandboxShell commands onlyClaude Code sandbox

Codex and Gemini CLI also run in containers. Codex's docs describe a Docker pattern for when its own Linux sandbox cannot run, and Gemini CLI uses a container sandbox when Docker or Podman is running. See Codex sandbox and the comparison in sandbox for an AI agent.

08Should you run Claude Code in Docker?

Run Claude Code in Docker when you want the no-prompt mode, when you open code you do not fully trust, or when you want unattended runs. Docker Sandboxes is the shortest path. Skip the container as a cure-all, though. If you share more than the project folder, or leave the network open, it protects little.

If you only want fewer prompts on your own Mac, the built-in sandbox is lighter, and Claude Code permissions covers the allow rules.

A container limits what a command can touch; it doesn't show what already ran. For agent work that still happens on the Mac itself, Artificial Immunity's free, open-source core watches agent sessions and program starts and reports what it finds.

FAQ

How do I run Claude Code in Docker?

Use Docker Sandboxes. Install sbx with Homebrew and sign in with sbx login, then go to your project folder and run sbx run --name my-sandbox claude. Then type /login inside the sandbox to sign in; the session token stays on your Mac.

Is it safe to run Claude Code in Docker?

It is safer than running it bare on your Mac, but not safe by itself. Docker Sandboxes puts the agent in its own microVM with a network preset. A plain container with the no-prompt flag on still does not stop data from being sent out over the network.

Can I use --dangerously-skip-permissions in a container?

Yes, a container is the kind of place Anthropic's docs allow it. Run as a non-root user, because the CLI rejects the flag as root, and pair it with network limits.

Does Docker protect my project files from Claude?

Not in the default setup. The project folder is mounted read-write, so Claude can change any file in it and the changes appear on your Mac. Clone mode gives the agent a private clone instead.

Should I mount my ~/.ssh folder into the container?

No. Anthropic's docs say to avoid mounting host secrets such as ~/.ssh or cloud credential files, and to prefer repository-scoped or short-lived tokens.

Is a plain Docker container the same as a dev container?

Not quite. A dev container is a container your editor opens your project in, described by a devcontainer.json file. A plain container is one you start yourself and run the agent in.

Can Codex or Gemini CLI run in Docker too?

Yes. Codex's docs describe running with full access inside a container you configured, and Gemini CLI has a built-in Docker sandbox that needs Docker or Podman running.

Sources

  1. Claude Code docs: development containers
  2. Claude Code docs: sandboxing
  3. Claude Code docs: permission modes
  4. OpenAI Codex docs: approvals and security
  5. Gemini CLI docs: sandbox
  6. Docker Sandboxes docs
  7. Docker Sandboxes: get started
  8. Git: git-restore
  9. Artificial Immunity
Agent in a container? Artificial Immunity watches the Mac where your agents work.Get the free core