Claude Code is Anthropic’s agentic coding tool that lives in your terminal, reads your codebase, and carries out tasks through natural language prompts. It can refactor code, write tests, navigate git history, and run arbitrary shell commands without you typing a single command manually. That power is exactly why you should think carefully about where it runs.
Why Docker Changes the Risk Profile
By default, Claude Code runs as your user account on your host machine. It has access to every file you can read, every credential stored in your home directory, and every shell command available on your PATH. For most day-to-day use this is fine, but it violates the principle of least privilege. An agentic tool that can run arbitrary commands as you has a blast radius that extends well beyond your project.
Running Claude Code inside a Docker container draws a hard boundary around that blast radius. The container gets its own filesystem namespace, so it cannot see your SSH keys, cloud credential files, or anything else on the host unless you explicitly mount it. The container’s process namespace is isolated, so a runaway subprocess stays inside the container. Docker bind mounts let you expose exactly one directory, giving Claude Code full access to the project it needs to work on while keeping everything else off-limits.
One caveat from Anthropic’s own documentation: if the container can reach the network, it can still exfiltrate data it reads inside the mounted directory. Network egress restrictions, covered at the end of this post, close that gap if your threat model requires it.
Writing the Dockerfiles
Each Dockerfile starts from the official slim image for its language, adds git and other utilities, installs Claude Code, then creates a non-root user. The non-root user is not optional. Claude Code refuses to start with the --dangerously-skip-permissions flag when running as root, and you will want that flag when working inside a container where the permission prompts are redundant.
Claude Code is an npm package, so Node.js is a runtime dependency regardless of which language you are working in. The Python, Java, and Rust images all install a minimal Node.js alongside their primary runtime. It stays out of your way but needs to be there for Claude Code to execute.
Node.js
The node:26-slim image already has Node.js and npm, so the Dockerfile is about as lean as it gets.
# Dockerfile.node
FROM node:26-slim
RUN apt-get update && apt-get install -y \
git \
tmux \
&& rm -rf /var/lib/apt/lists/*
RUN npm install -g @anthropic-ai/claude-code
RUN useradd -m -s /bin/bash developer
USER developer
WORKDIR /project
Python
python:3.15-slim ships with Python, pip, and venv already configured. We add Node.js via NodeSource solely for Claude Code.
# Dockerfile.python
FROM python:3.15-slim
RUN apt-get update && apt-get install -y \
curl \
git \
ca-certificates \
gnupg \
tmux \
&& rm -rf /var/lib/apt/lists/*
# Node.js is required to run Claude Code.
RUN curl -fsSL https://deb.nodesource.com/setup_26.x | bash - \
&& apt-get install -y nodejs \
&& rm -rf /var/lib/apt/lists/*
RUN npm install -g @anthropic-ai/claude-code
RUN useradd -m -s /bin/bash developer
USER developer
WORKDIR /project
Java
eclipse-temurin:25-jdk-bookworm is the Adoptium project’s production-grade JDK build on a Debian Bookworm base. JAVA_HOME is already set and the JDK is on PATH.
# Dockerfile.java
FROM eclipse-temurin:25-jdk-bookworm
RUN apt-get update && apt-get install -y \
curl \
git \
ca-certificates \
gnupg \
tmux \
&& rm -rf /var/lib/apt/lists/*
# Node.js is required to run Claude Code.
RUN curl -fsSL https://deb.nodesource.com/setup_26.x | bash - \
&& apt-get install -y nodejs \
&& rm -rf /var/lib/apt/lists/*
RUN npm install -g @anthropic-ai/claude-code
RUN useradd -m -s /bin/bash developer
USER developer
WORKDIR /project
Rust
rust:slim already has the full Rust toolchain installed via rustup, with RUSTUP_HOME, CARGO_HOME, and PATH all set correctly. Nothing left to configure manually.
# Dockerfile.rust
FROM rust:slim
RUN apt-get update && apt-get install -y \
curl \
git \
ca-certificates \
gnupg \
tmux \
&& rm -rf /var/lib/apt/lists/*
# Node.js is required to run Claude Code.
RUN curl -fsSL https://deb.nodesource.com/setup_26.x | bash - \
&& apt-get install -y nodejs \
&& rm -rf /var/lib/apt/lists/*
RUN npm install -g @anthropic-ai/claude-code
RUN useradd -m -s /bin/bash developer
USER developer
WORKDIR /project
In each file the layer order follows the same logic: system packages first, then Claude Code. Bumping the Claude Code version only invalidates the final layers and does not trigger a base image or runtime reinstall.
Building the Images
Build only the images you need. The -f flag points Docker at the specific Dockerfile, and the tag gives you a name to reference at run time:
docker build -f Dockerfile.node -t claude-code-node:latest .
docker build -f Dockerfile.python -t claude-code-python:latest .
docker build -f Dockerfile.java -t claude-code-java:latest .
docker build -f Dockerfile.rust -t claude-code-rust:latest .
The Node.js and Python images build in well under a minute. Java is a bit heavier given the JDK size. The Rust image takes the longest on first run because the toolchain is large, but Docker caches each layer so subsequent builds after changing only the Claude Code version are fast.
To pin a specific Claude Code version for reproducible team environments, replace the npm install line with an explicit version:
RUN npm install -g @anthropic-ai/claude-code@1.0.6
Add ENV DISABLE_AUTOUPDATER=1 after that line to prevent Claude Code from updating itself inside the container, which matters when you need every run to use the same version.
Running Claude Code Against Your Project
The -v flag bind-mounts your local project directory into the container at /project. Any file Claude Code reads or writes inside the container appears directly in your local working tree. Your editor, git history, and other tools see the changes in real time.
One-off runs
For a quick task where you do not need to resume later, the ephemeral pattern works fine:
docker run -it \
--rm \
-e ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY" \
-v "$(pwd):/project" \
claude-code-python:latest \
claude --dangerously-skip-permissions
The container exits and cleans up when you close Claude Code.
Long-running sessions with tmux
For extended work you want to pause and resume, run a persistent container and manage Claude Code inside a tmux session. With tmux, the Claude Code process keeps running when you detach, so you return to the exact conversation you left. No context rebuilding, no “as I mentioned earlier.”
Start the container as a background daemon using sleep infinity to keep it alive:
docker run -d \
--name myproject \
-e ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY" \
-e CLAUDE_CONFIG_DIR=/home/developer/.claude \
-v "$(pwd):/project" \
-v claude-code-config:/home/developer/.claude \
claude-code-python:latest \
sleep infinity
Then create a tmux session and start Claude Code inside it:
docker exec -it myproject tmux new-session -s claude 'claude --dangerously-skip-permissions'
This drops you straight into Claude Code inside a named tmux session. When you need to step away, detach with Ctrl-B D. The session and the Claude Code process keep running inside the container. Reattach any time with:
docker exec -it myproject tmux attach -t claude
To stop the container at the end of the day:
docker stop myproject
To bring it back:
docker start myproject
docker exec -it myproject tmux attach -t claude
The tmux session does not survive a docker stop since the processes are killed when the container stops. The named volume does survive: credentials, project settings, and Claude Code’s session history are all intact when you restart. Authentication works immediately and Claude Code has its full record of past sessions to draw from.
If you are juggling multiple projects, give each its own named container and volume:
docker run -d \
--name project-b \
-e ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY" \
-e CLAUDE_CONFIG_DIR=/home/developer/.claude \
-v "/path/to/project-b:/project" \
-v claude-code-project-b:/home/developer/.claude \
claude-code-node:latest \
sleep infinity
Each container is isolated, carries its own volume, and runs its own tmux session independently.
Network egress
The setup above lets the container reach the internet freely, which Claude Code needs to call the Anthropic API. If you are working with sensitive code and want to block all other outbound traffic, you can apply an iptables allowlist inside the container. Anthropic’s reference dev container ships an init-firewall.sh script that demonstrates this pattern. It requires the NET_ADMIN and NET_RAW capabilities, added with --cap-add NET_ADMIN --cap-add NET_RAW on the docker run command.
The setup takes about five minutes and costs you nothing in functionality. You get a hard boundary between Claude Code and your host, persistent sessions you can walk away from and return to, and an isolated environment for every project in your stack.