Docker Integration Intermediate

Run Claude Code inside Docker containers for reproducible, isolated development environments. Learn Dockerfile setup, Docker Compose workflows, dev containers, and multi-container architectures.

Dockerfile Setup

Create a Docker image with Claude Code pre-installed for consistent environments across your team:

Dockerfile
# Base image with Node.js
FROM node:20-slim

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

# Install common dev tools
RUN apt-get update && apt-get install -y \
    git \
    curl \
    vim \
    && rm -rf /var/lib/apt/lists/*

# Set working directory
WORKDIR /workspace

# API key is passed at runtime, not baked into the image
# ENV ANTHROPIC_API_KEY -- DO NOT DO THIS

# Default command
CMD ["bash"]
Never bake API keys into Docker images. Images can be shared, pushed to registries, and inspected. Always pass the API key as an environment variable at runtime using -e or --env-file.

Running Claude Code in Docker

Terminal
# Build the image
$ docker build -t claude-dev .

# Run with your code mounted and API key passed
$ docker run -it \
    -e ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY" \
    -v $(pwd):/workspace \
    claude-dev

# Inside the container, use Claude Code normally
root@container:/workspace$ claude -p "Fix the failing tests"

# One-shot mode without entering the container
$ docker run --rm \
    -e ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY" \
    -v $(pwd):/workspace \
    claude-dev \
    claude -p "Add error handling to all API routes"

Mounting Volumes for Code Access

Volumes let Claude Code access and modify your local code from inside the container:

Terminal
# Mount current directory
$ docker run -it -v $(pwd):/workspace -e ANTHROPIC_API_KEY claude-dev

# Mount specific directories
$ docker run -it \
    -v ~/projects/my-app/src:/workspace/src \
    -v ~/projects/my-app/tests:/workspace/tests \
    -e ANTHROPIC_API_KEY \
    claude-dev

# Read-only mount (Claude can analyze but not modify)
$ docker run -it \
    -v $(pwd):/workspace:ro \
    -e ANTHROPIC_API_KEY \
    claude-dev \
    claude -p "Review this codebase for security issues"

Docker Compose for Development

Use Docker Compose to define a complete development environment with Claude Code:

YAML (docker-compose.yml)
version: '3.8'

services:
  dev:
    build: .
    volumes:
      - .:/workspace
      - node_modules:/workspace/node_modules
    environment:
      - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
      - NODE_ENV=development
    stdin_open: true
    tty: true
    depends_on:
      - db
      - redis

  db:
    image: postgres:16
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: dev
      POSTGRES_PASSWORD: devpass
    volumes:
      - pgdata:/var/lib/postgresql/data

  redis:
    image: redis:7-alpine

volumes:
  node_modules:
  pgdata:
Terminal
# Start all services
$ docker compose up -d

# Attach to the dev container
$ docker compose exec dev bash

# Use Claude Code with full access to the database and cache
root@dev:/workspace$ claude -p "Write a migration to add user preferences table and update the models"

Dev Containers

VS Code Dev Containers provide a seamless Docker-based development experience. Add Claude Code to your dev container configuration:

JSON (.devcontainer/devcontainer.json)
{
  "name": "My Project Dev Container",
  "image": "mcr.microsoft.com/devcontainers/javascript-node:20",
  "features": {
    "ghcr.io/devcontainers/features/git:1": {}
  },
  "postCreateCommand": "npm install -g @anthropic-ai/claude-code && npm install",
  "containerEnv": {
    "ANTHROPIC_API_KEY": "${localEnv:ANTHROPIC_API_KEY}"
  },
  "customizations": {
    "vscode": {
      "extensions": [
        "anthropic.claude-code"
      ]
    }
  }
}

Multi-Container Setups

For complex architectures, Claude Code in one container can interact with services in other containers:

Terminal
# Claude Code can run commands that interact with other containers
root@dev:/workspace$ claude

> Connect to the PostgreSQL database at db:5432 and analyze the schema.
> Then optimize the slow queries I found in the error logs.

# Claude can run psql, curl services, etc. from inside the dev container

Reproducible Development Environments

The combination of Docker and Claude Code ensures every team member has the same environment:

Dockerfile (Dockerfile.dev)
FROM node:20-slim

# Install Claude Code at a specific version for consistency
RUN npm install -g @anthropic-ai/claude-code@latest

# Install project-specific tools
RUN apt-get update && apt-get install -y \
    git python3 python3-pip postgresql-client \
    && rm -rf /var/lib/apt/lists/*

# Copy CLAUDE.md for project context
COPY CLAUDE.md /workspace/CLAUDE.md

WORKDIR /workspace
CMD ["bash"]
Include CLAUDE.md: Copy your CLAUDE.md file into the Docker image so Claude Code always has project context, even when used by new team members who just pulled the image.

Try It Yourself

Create a simple Dockerfile that includes Claude Code, build it, and run a one-shot command. Next, learn how to integrate Claude Code into CI/CD pipelines for automated workflows.

Next: CI/CD Pipelines →

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.