Claude Code in Docker

How to run Claude Code inside Docker containers — for reproducible dev environments, remote servers, CI pipelines, and sandboxed agentic workloads. Full Dockerfile and docker-compose examples.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

Running Claude Code inside Docker gives you a reproducible, sandboxed environment that works the same on any machine, CI system, or remote server. Here's the complete setup.

Minimal Dockerfile

FROM node:20-slim

# Install Claude Code globally
RUN npm install -g @anthropic-ai/claude-code

# Optional: install common dev tools Claude Code might invoke
RUN apt-get update && apt-get install -y     git     python3     python3-pip     curl     && rm -rf /var/lib/apt/lists/*

WORKDIR /workspace

# API key injected at runtime — never bake it in
ENV ANTHROPIC_API_KEY=""

# Default to headless mode
CMD ["claude", "--print", "Help available — pass a prompt as CMD"]

Build and run

# Build the image
docker build -t claude-code-env .

# Run against a local project (interactive)
docker run -it   -e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY   -v $(pwd):/workspace   claude-code-env   claude

# Run headless (non-interactive, for CI or scripting)
docker run --rm   -e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY   -v $(pwd):/workspace   claude-code-env   claude --print "Fix the type errors in src/api.ts"

docker-compose for dev teams

# docker-compose.yml
version: "3.9"
services:
  claude:
    build: .
    volumes:
      - .:/workspace          # mount current project
      - ~/.gitconfig:/root/.gitconfig:ro  # share git identity
    environment:
      - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
    stdin_open: true
    tty: true
    working_dir: /workspace
# Start an interactive Claude Code session in the container
docker-compose run --rm claude claude

# Run a one-shot command
docker-compose run --rm claude claude --print "Summarize the git log since yesterday"

Remote server setup (SSH + Docker)

For access to a powerful cloud VM from your laptop:

# On the remote server: install Docker, build the image, start a named container
ssh user@remote-server

docker build -t claude-code-env .
docker run -d --name claude-session   -e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY   -v /home/user/projects:/workspace   -it   claude-code-env   /bin/bash

# Reconnect to the running session any time
docker exec -it claude-session claude

Security sandboxing

Running Claude Code inside Docker restricts what it can access:

# Read-only source, writable output dir only
docker run --rm   -e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY   -v $(pwd)/src:/workspace/src:ro      # source read-only
  -v $(pwd)/output:/workspace/output   # Claude writes here only
  --network=none                       # no internet except via host proxy
  --memory=2g                          # memory cap
  --cpus=2                             # CPU cap
  claude-code-env   claude --print "Analyze the code in /workspace/src and write a report to /workspace/output/report.md"

CI/CD integration (GitHub Actions)

# .github/workflows/claude-review.yml
name: Claude Code PR Review
on:
  pull_request:

jobs:
  review:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Run Claude Code review
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
        run: |
          docker run --rm \
            -e ANTHROPIC_API_KEY \
            -v ${{ github.workspace }}:/workspace \
            ghcr.io/myorg/claude-code-env:latest \
            claude --print "Review the diff against main. List: missing tests, security issues, breaking API changes."

Dockerfile for Python-heavy projects

FROM python:3.12-slim

RUN apt-get update && apt-get install -y     curl gnupg nodejs npm git     && rm -rf /var/lib/apt/lists/*

RUN npm install -g @anthropic-ai/claude-code

# Project dependencies pre-installed for faster Claude Code iterations
COPY requirements.txt /tmp/
RUN pip install -r /tmp/requirements.txt

WORKDIR /workspace
ENV ANTHROPIC_API_KEY=""

For using AWS Bedrock as the model backend (keeps traffic in your VPC), see Claude Code on AWS Bedrock. For running automated agentic pipelines without Docker, see Claude Code headless mode.

Frequently asked questions

Can Claude Code run inside a Docker container?
Yes — Claude Code is a Node.js CLI tool that runs on any Linux/macOS environment with Node 18+. Install it with npm inside a Docker image and set ANTHROPIC_API_KEY. The only requirement is outbound HTTPS to api.anthropic.com (or your Bedrock endpoint).
Why run Claude Code in Docker instead of locally?
Main reasons: reproducible environment for all developers (no 'works on my machine'), sandboxed agentic workloads (Claude Code can only modify files in the container, not your host), remote server access (run Claude Code on a powerful cloud VM and connect via SSH or web terminal), and CI/CD integration without local installs.
Does Claude Code work on a headless Linux server?
Yes — use the --print flag for non-interactive (headless) mode: `claude --print 'Fix the failing tests'`. Claude Code outputs the response as text, runs tools, and exits. No TTY required. This is the recommended pattern for remote servers and CI.
How do I pass the API key securely into a Docker container?
Use Docker secrets or environment injection at runtime — never bake the key into the image. For local dev: `docker run -e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY ...`. For production: use AWS Secrets Manager, Vault, or Docker Swarm secrets and inject at container startup.
Can Claude Code in Docker access my local files?
Only via volume mounts. Mount the project directory with `-v /path/to/project:/workspace`. Claude Code inside the container can only read/write the mounted paths. This is also a useful sandbox: Claude Code can never touch files outside the mounted volume.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScript