claude-docker/Dockerfile
2025-06-23 16:49:18 +08:00

108 lines
3.7 KiB
Docker

FROM node:20-slim
# Install required system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
python3 \
build-essential \
sudo \
&& rm -rf /var/lib/apt/lists/*
# Install additional system packages if specified
ARG SYSTEM_PACKAGES=""
RUN if [ -n "$SYSTEM_PACKAGES" ]; then \
echo "Installing additional system packages: $SYSTEM_PACKAGES" && \
apt-get update && \
apt-get install -y $SYSTEM_PACKAGES && \
rm -rf /var/lib/apt/lists/*; \
else \
echo "No additional system packages specified"; \
fi
# Create a non-root user with matching host UID/GID
ARG USER_UID=1000
ARG USER_GID=1000
RUN addgroup --gid $USER_GID claude-user || true && \
adduser --disabled-password --gecos '' --uid $USER_UID --gid $USER_GID claude-user && \
echo "claude-user ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers && \
echo "User created:" && id claude-user && groups claude-user
# Create app directory
WORKDIR /app
# Install Claude Code globally
RUN npm install -g @anthropic-ai/claude-code@latest
RUN npm install -g tsx
RUN npm install -g nrm
RUN npm install -g pnpm
# Ensure npm global bin is in PATH
ENV PATH="/usr/local/bin:${PATH}"
# Create directories for configuration
RUN mkdir -p /app/.claude /home/claude-user/.claude
# Copy startup script
COPY scripts/startup.sh /app/
RUN chmod +x /app/startup.sh
# Copy .env file during build to bake credentials into the image
# This enables one-time setup - no need for .env in project directories
COPY .env /app/.env
# Copy CLAUDE.md template directly to final location
COPY templates/.claude/CLAUDE.md /home/claude-user/.claude/CLAUDE.md
# Copy Claude authentication files from host
# Note: These must exist - host must have authenticated Claude Code first
COPY .claude.json /tmp/.claude.json
COPY .claude /tmp/.claude
# Move auth files to proper location before switching user
RUN cp /tmp/.claude.json /home/claude-user/.claude.json && \
cp -r /tmp/.claude/* /home/claude-user/.claude/ && \
rm -rf /tmp/.claude*
# Set proper ownership for everything
RUN chown -R claude-user:claude-user /app /home/claude-user || \
chown -R $(id -u claude-user):$(id -g claude-user) /app /home/claude-user
# Switch to non-root user
USER claude-user
# Set HOME immediately after switching user
ENV HOME=/home/claude-user
# Configure MCP server during build if Twilio credentials are provided
RUN bash -c 'source /app/.env && \
if [ -n "$TWILIO_ACCOUNT_SID" ] && [ -n "$TWILIO_AUTH_TOKEN" ]; then \
echo "Configuring Twilio MCP server..." && \
/usr/local/bin/claude mcp add-json twilio -s user \
"{\"command\":\"npx\",\"args\":[\"-y\",\"@yiyang.1i/sms-mcp-server\"],\"env\":{\"ACCOUNT_SID\":\"$TWILIO_ACCOUNT_SID\",\"AUTH_TOKEN\":\"$TWILIO_AUTH_TOKEN\",\"FROM_NUMBER\":\"$TWILIO_FROM_NUMBER\"}}"; \
else \
echo "No Twilio credentials found, skipping MCP configuration"; \
fi'
# Configure git user during build using host git config passed as build args
ARG GIT_USER_NAME=""
ARG GIT_USER_EMAIL=""
RUN if [ -n "$GIT_USER_NAME" ] && [ -n "$GIT_USER_EMAIL" ]; then \
echo "Configuring git user from host: $GIT_USER_NAME <$GIT_USER_EMAIL>" && \
git config --global user.name "$GIT_USER_NAME" && \
git config --global user.email "$GIT_USER_EMAIL" && \
echo "Git configuration complete"; \
else \
echo "Warning: No git user configured on host system"; \
echo "Run 'git config --global user.name \"Your Name\"' and 'git config --global user.email \"you@example.com\"' on host first"; \
fi
# Set working directory to mounted volume
WORKDIR /workspace
# Environment variables will be passed from host
ENV NODE_ENV=production
# Start both MCP server and Claude Code
ENTRYPOINT ["/app/startup.sh"]