Skip to content

Commit b953f5e

Browse files
committed
Initial commit: Claude Code Docker container
- Dockerfile with Node 22 slim base and Claude Code CLI - docker-compose.yml for easy local running - Entrypoint script with config initialization - GitHub Actions workflow for multi-arch builds - Comprehensive README with usage examples MIT License
0 parents  commit b953f5e

File tree

9 files changed

+418
-0
lines changed

9 files changed

+418
-0
lines changed

.dockerignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Git
2+
.git
3+
.gitignore
4+
5+
# Documentation
6+
README.md
7+
LICENSE
8+
9+
# Environment files
10+
.env
11+
.env.*
12+
13+
# Workspace
14+
workspace/
15+
16+
# GitHub
17+
.github/
18+
19+
# OS
20+
.DS_Store
21+
Thumbs.db

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Anthropic API Key (get one at https://console.anthropic.com/)
2+
ANTHROPIC_API_KEY=your-api-key-here
3+
4+
# Optional: Anthropic API base URL (for enterprise/proxy setups)
5+
# ANTHROPIC_API_BASE_URL=https://api.anthropic.com
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Build and Push Docker Image
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
tags:
8+
- 'v*'
9+
pull_request:
10+
branches:
11+
- main
12+
13+
env:
14+
REGISTRY: docker.io
15+
IMAGE_NAME: ungb/claude-code
16+
17+
jobs:
18+
build-and-push:
19+
runs-on: ubuntu-latest
20+
permissions:
21+
contents: read
22+
packages: write
23+
24+
steps:
25+
- name: Checkout repository
26+
uses: actions/checkout@v4
27+
28+
- name: Set up QEMU
29+
uses: docker/setup-qemu-action@v3
30+
31+
- name: Set up Docker Buildx
32+
uses: docker/setup-buildx-action@v3
33+
34+
- name: Log in to Docker Hub
35+
if: github.event_name != 'pull_request'
36+
uses: docker/login-action@v3
37+
with:
38+
username: ${{ secrets.DOCKERHUB_USERNAME }}
39+
password: ${{ secrets.DOCKERHUB_TOKEN }}
40+
41+
- name: Extract metadata
42+
id: meta
43+
uses: docker/metadata-action@v5
44+
with:
45+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
46+
tags: |
47+
type=ref,event=branch
48+
type=semver,pattern={{version}}
49+
type=semver,pattern={{major}}.{{minor}}
50+
type=raw,value=latest,enable={{is_default_branch}}
51+
52+
- name: Build and push
53+
uses: docker/build-push-action@v5
54+
with:
55+
context: .
56+
platforms: linux/amd64,linux/arm64
57+
push: ${{ github.event_name != 'pull_request' }}
58+
tags: ${{ steps.meta.outputs.tags }}
59+
labels: ${{ steps.meta.outputs.labels }}
60+
cache-from: type=gha
61+
cache-to: type=gha,mode=max

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Environment files
2+
.env
3+
.env.local
4+
5+
# Workspace directory (user's code)
6+
workspace/
7+
8+
# OS files
9+
.DS_Store
10+
Thumbs.db
11+
12+
# IDE
13+
.idea/
14+
.vscode/
15+
*.swp
16+
*.swo

Dockerfile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
FROM node:22-slim
2+
3+
LABEL maintainer="ungb"
4+
LABEL description="Claude Code CLI in a Docker container"
5+
LABEL org.opencontainers.image.source="https://github.com/ungb/claude-code-docker"
6+
7+
# Install system dependencies
8+
RUN apt-get update && apt-get install -y --no-install-recommends \
9+
git \
10+
curl \
11+
openssh-client \
12+
ca-certificates \
13+
jq \
14+
&& rm -rf /var/lib/apt/lists/*
15+
16+
# Install Claude Code CLI globally
17+
RUN npm install -g @anthropic-ai/claude-code
18+
19+
# Create non-root user for security
20+
RUN useradd -m -s /bin/bash coder \
21+
&& mkdir -p /home/coder/.claude \
22+
&& chown -R coder:coder /home/coder
23+
24+
# Set up workspace directory
25+
RUN mkdir -p /workspace && chown coder:coder /workspace
26+
27+
# Copy entrypoint script
28+
COPY --chown=coder:coder entrypoint.sh /entrypoint.sh
29+
RUN chmod +x /entrypoint.sh
30+
31+
# Switch to non-root user
32+
USER coder
33+
WORKDIR /workspace
34+
35+
# Environment variables
36+
ENV HOME=/home/coder
37+
ENV CLAUDE_CONFIG_DIR=/home/coder/.claude
38+
39+
ENTRYPOINT ["/entrypoint.sh"]
40+
CMD ["claude"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
# Claude Code Docker
2+
3+
Run [Claude Code](https://github.com/anthropics/claude-code) CLI in a Docker container. Claude Code is Anthropic's agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster.
4+
5+
## Quick Start
6+
7+
```bash
8+
# Pull and run (replace with your API key)
9+
docker run -it --rm \
10+
-v $(pwd):/workspace \
11+
-e ANTHROPIC_API_KEY=your-key \
12+
ungb/claude-code
13+
```
14+
15+
## Prerequisites
16+
17+
- [Docker](https://docs.docker.com/get-docker/) installed
18+
- [Anthropic API key](https://console.anthropic.com/) or Claude account for OAuth
19+
20+
## Usage
21+
22+
### Using Docker Run
23+
24+
```bash
25+
# Basic usage with API key
26+
docker run -it --rm \
27+
-v $(pwd):/workspace \
28+
-e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY \
29+
ungb/claude-code
30+
31+
# With persistent config (remembers settings between runs)
32+
docker run -it --rm \
33+
-v $(pwd):/workspace \
34+
-v claude-config:/home/coder/.claude \
35+
-e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY \
36+
ungb/claude-code
37+
38+
# With git/ssh support
39+
docker run -it --rm \
40+
-v $(pwd):/workspace \
41+
-v claude-config:/home/coder/.claude \
42+
-v ~/.ssh:/home/coder/.ssh:ro \
43+
-v ~/.gitconfig:/home/coder/.gitconfig:ro \
44+
-e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY \
45+
ungb/claude-code
46+
```
47+
48+
### Using Docker Compose
49+
50+
1. Clone this repo or copy `docker-compose.yml` to your project:
51+
52+
```bash
53+
curl -O https://raw.githubusercontent.com/ungb/claude-code-docker/main/docker-compose.yml
54+
```
55+
56+
2. Create a `.env` file with your API key:
57+
58+
```bash
59+
echo "ANTHROPIC_API_KEY=your-key-here" > .env
60+
```
61+
62+
3. Run:
63+
64+
```bash
65+
docker compose run --rm claude
66+
```
67+
68+
### Run a Specific Command
69+
70+
```bash
71+
# Run claude with arguments
72+
docker run -it --rm \
73+
-v $(pwd):/workspace \
74+
-e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY \
75+
ungb/claude-code \
76+
claude "explain this codebase"
77+
78+
# Check version
79+
docker run -it --rm ungb/claude-code claude --version
80+
81+
# Run health check
82+
docker run -it --rm \
83+
-e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY \
84+
ungb/claude-code \
85+
claude doctor
86+
```
87+
88+
## Authentication
89+
90+
### Option 1: API Key (Recommended for Docker)
91+
92+
Get an API key from [Anthropic Console](https://console.anthropic.com/) and pass it as an environment variable:
93+
94+
```bash
95+
-e ANTHROPIC_API_KEY=sk-ant-...
96+
```
97+
98+
### Option 2: OAuth Login
99+
100+
For browser-based OAuth (requires host network):
101+
102+
```bash
103+
docker run -it --rm \
104+
--network host \
105+
-v $(pwd):/workspace \
106+
-v claude-config:/home/coder/.claude \
107+
ungb/claude-code \
108+
claude login
109+
```
110+
111+
## Volume Mounts
112+
113+
| Mount | Purpose |
114+
|-------|---------|
115+
| `/workspace` | Your project directory (required) |
116+
| `/home/coder/.claude` | Claude config and cache (optional, for persistence) |
117+
| `/home/coder/.ssh` | SSH keys for git operations (optional, read-only) |
118+
| `/home/coder/.gitconfig` | Git configuration (optional, read-only) |
119+
120+
## Environment Variables
121+
122+
| Variable | Required | Description |
123+
|----------|----------|-------------|
124+
| `ANTHROPIC_API_KEY` | Yes* | Your Anthropic API key |
125+
| `ANTHROPIC_API_BASE_URL` | No | Custom API endpoint (for proxies) |
126+
127+
*Required unless using OAuth login
128+
129+
## Building Locally
130+
131+
```bash
132+
git clone https://github.com/ungb/claude-code-docker.git
133+
cd claude-code-docker
134+
docker build -t claude-code .
135+
```
136+
137+
## Troubleshooting
138+
139+
### Permission Denied on Mounted Files
140+
141+
The container runs as user `coder` (UID 1000). If you have permission issues:
142+
143+
```bash
144+
# Run with your user ID
145+
docker run -it --rm \
146+
--user $(id -u):$(id -g) \
147+
-v $(pwd):/workspace \
148+
-e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY \
149+
ungb/claude-code
150+
```
151+
152+
### Git Operations Failing
153+
154+
Ensure SSH keys are mounted and git is configured:
155+
156+
```bash
157+
docker run -it --rm \
158+
-v $(pwd):/workspace \
159+
-v ~/.ssh:/home/coder/.ssh:ro \
160+
-v ~/.gitconfig:/home/coder/.gitconfig:ro \
161+
-e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY \
162+
ungb/claude-code
163+
```
164+
165+
### OAuth Login Not Working
166+
167+
Use `--network host` to allow the OAuth callback:
168+
169+
```bash
170+
docker run -it --rm \
171+
--network host \
172+
-v $(pwd):/workspace \
173+
-v claude-config:/home/coder/.claude \
174+
ungb/claude-code \
175+
claude login
176+
```
177+
178+
## License
179+
180+
MIT License - see [LICENSE](LICENSE)
181+
182+
## Links
183+
184+
- [Claude Code Documentation](https://docs.anthropic.com/claude-code)
185+
- [Anthropic Console](https://console.anthropic.com/)
186+
- [Claude Code GitHub](https://github.com/anthropics/claude-code)

docker-compose.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
services:
2+
claude:
3+
image: ungb/claude-code:latest
4+
build: .
5+
container_name: claude-code
6+
volumes:
7+
# Mount your project directory
8+
- ./workspace:/workspace
9+
# Persist Claude configuration between runs
10+
- claude-config:/home/coder/.claude
11+
# SSH keys for git operations (read-only)
12+
- ~/.ssh:/home/coder/.ssh:ro
13+
# Git configuration (read-only)
14+
- ~/.gitconfig:/home/coder/.gitconfig:ro
15+
environment:
16+
# Set your Anthropic API key
17+
- ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
18+
stdin_open: true
19+
tty: true
20+
# Uncomment for host network mode (useful for some integrations)
21+
# network_mode: host
22+
23+
volumes:
24+
claude-config:
25+
name: claude-code-config

0 commit comments

Comments
 (0)