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.
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.
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 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.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"
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
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"
# .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."
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.