Skip to content

Instantly share code, notes, and snippets.

@chaws
Created December 18, 2025 17:02
Show Gist options
  • Select an option

  • Save chaws/8a06b66b769030dd91fa42b24a9bd3cf to your computer and use it in GitHub Desktop.

Select an option

Save chaws/8a06b66b769030dd91fa42b24a9bd3cf to your computer and use it in GitHub Desktop.
Run Claude Code on docker
#!/bin/bash
set -xe
IMAGE_NAME="claude-code-docker"
DOCKERFILE_PATH="$(dirname "$0")/Dockerfile.claude"
# Build the image if it doesn't exist
if ! docker image inspect "$IMAGE_NAME" &> /dev/null; then
echo "Image $IMAGE_NAME not found. Building..."
docker build -f "$DOCKERFILE_PATH" -t "$IMAGE_NAME" "$(dirname "$0")"
fi
# Ensure Claude config directory and file exist
mkdir -p "$HOME/.claude"
touch "$HOME/.claude.json"
# Run Claude Code in Docker container
# - Mount current directory to /workspace
# - Mount Claude config directory and file for authentication persistence
# - Pass current user UID/GID to maintain file permissions
# - Interactive with TTY for proper CLI experience
docker run -it --rm \
-e HOST_UID="$(id -u)" \
-e HOST_GID="$(id -g)" \
-e HOST_USER="$USER" \
-e HOST_HOME="/home/$USER" \
-v "$PWD:/workspace" \
-v "$HOME/.claude:/home/$USER/.claude" \
-v "$HOME/.claude.json:/home/$USER/.claude.json" \
-w /workspace \
"$IMAGE_NAME" "$@"
FROM debian:latest
# Install dependencies
RUN apt-get update && apt-get install -y \
curl \
git \
ca-certificates \
gnupg \
sudo \
&& rm -rf /var/lib/apt/lists/*
# Install Node.js (required for Claude Code)
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
&& apt-get install -y nodejs \
&& rm -rf /var/lib/apt/lists/*
# Install Claude Code globally
RUN npm install -g @anthropic-ai/claude-code
# Create workspace directory
RUN mkdir -p /workspace
# Set working directory
WORKDIR /workspace
# Script to create user matching host UID/GID and run Claude
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
#!/bin/bash
# Create a user with the same UID/GID as the host user
# This ensures files created in the container have the correct ownership
USER_NAME="${HOST_USER:-claude-user}"
USER_UID="${HOST_UID:-1000}"
USER_GID="${HOST_GID:-1000}"
USER_HOME="${HOST_HOME:-/home/$USER_NAME}"
# Create group if it doesn't exist
if ! getent group "$USER_GID" > /dev/null 2>&1; then
groupadd -g "$USER_GID" "$USER_NAME"
fi
# Create home directory first to avoid useradd warnings
mkdir -p "$USER_HOME"
# Create user if it doesn't exist
if ! id "$USER_UID" > /dev/null 2>&1; then
useradd -u "$USER_UID" -g "$USER_GID" -d "$USER_HOME" -M -s /bin/bash "$USER_NAME"
fi
# Ensure .claude directory and file exist
mkdir -p "$USER_HOME/.claude"
touch "$USER_HOME/.claude.json"
# Set ownership on home directory and all contents
# Do this explicitly for the directory itself and its contents
chown "$USER_UID:$USER_GID" "$USER_HOME"
chown -R "$USER_UID:$USER_GID" "$USER_HOME/.claude"
chown "$USER_UID:$USER_GID" "$USER_HOME/.claude.json"
# Ensure the user has access to workspace
chown -R "$USER_UID:$USER_GID" /workspace 2>/dev/null || true
# Make sure home directory and config files are writable
chmod 755 "$USER_HOME"
chmod -R u+w "$USER_HOME/.claude"
chmod u+w "$USER_HOME/.claude.json"
# Switch to the user and run Claude Code
exec sudo -u "#$USER_UID" -g "#$USER_GID" HOME="$USER_HOME" claude "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment