-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDockerfile
More file actions
144 lines (118 loc) · 4.81 KB
/
Copy pathDockerfile
File metadata and controls
144 lines (118 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# syntax=docker/dockerfile:1.7
# =============================================================================
# ClaudeCode4J Runtime Dockerfile
#
# Sample Dockerfile for applications that use ClaudeCode4J library.
# Provides Java 25 runtime + Node.js + Claude Code CLI pre-installed.
#
# Usage:
# docker build -f docker/Dockerfile -t myapp --target runtime .
# docker build -f docker/Dockerfile -t myapp-dev --target development .
#
# Or with docker-compose:
# cd docker && docker-compose up app
# =============================================================================
# -----------------------------------------------------------------------------
# Stage 1: Build stage (for building Java applications using this library)
# -----------------------------------------------------------------------------
FROM eclipse-temurin:25-jdk-noble AS builder
WORKDIR /build
# Copy Maven wrapper and pom files first for better layer caching
COPY mvnw pom.xml ./
COPY .mvn .mvn
COPY claudecode4j-bom/pom.xml claudecode4j-bom/
COPY claudecode4j-api/pom.xml claudecode4j-api/
COPY claudecode4j-core/pom.xml claudecode4j-core/
COPY claudecode4j-spring-boot-starter/pom.xml claudecode4j-spring-boot-starter/
COPY claudecode4j-rest-adapter/pom.xml claudecode4j-rest-adapter/
COPY claudecode4j-kafka-adapter/pom.xml claudecode4j-kafka-adapter/
# Download dependencies (cached unless pom.xml changes)
RUN --mount=type=cache,target=/root/.m2 \
./mvnw dependency:go-offline -B
# Copy source code
COPY . .
# Build the project
RUN --mount=type=cache,target=/root/.m2 \
./mvnw clean install -DskipTests -B
# -----------------------------------------------------------------------------
# Stage 2: Runtime stage
# -----------------------------------------------------------------------------
FROM eclipse-temurin:25-jre-noble AS runtime
LABEL maintainer="Mahdi Amirabdollahi"
LABEL org.opencontainers.image.title="ClaudeCode4J Runtime"
LABEL org.opencontainers.image.description="Java 25 runtime with Claude Code CLI for ClaudeCode4J applications"
LABEL org.opencontainers.image.source="https://github.com/sudoit/claudecode4j"
LABEL org.opencontainers.image.licenses="MIT"
# Environment variables
ENV NODE_VERSION=22 \
CLAUDE_HOME=/home/claude \
JAVA_OPTS="-XX:+UseZGC -XX:+ZGenerational --enable-preview" \
PATH="/home/claude/.npm-global/bin:$PATH"
# Install system dependencies
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get update && apt-get install -y --no-install-recommends \
# Required for Node.js installation
curl \
ca-certificates \
gnupg \
# Git for Claude Code operations
git \
# Process utilities
procps \
# For healthchecks
netcat-openbsd \
&& rm -rf /var/lib/apt/lists/*
# Install Node.js LTS
RUN curl -fsSL https://deb.nodesource.com/setup_${NODE_VERSION}.x | bash - \
&& apt-get install -y --no-install-recommends nodejs \
&& rm -rf /var/lib/apt/lists/* \
&& node --version \
&& npm --version
# Create non-root user with home directory
RUN groupadd --gid 1000 claude \
&& useradd --uid 1000 --gid claude --shell /bin/bash --create-home claude \
&& mkdir -p /app /home/claude/.npm-global \
&& chown -R claude:claude /app /home/claude
# Switch to non-root user
USER claude
WORKDIR /home/claude
# Configure npm to use user directory for global packages
RUN npm config set prefix '/home/claude/.npm-global'
# Install Claude Code CLI globally
RUN npm install -g @anthropic-ai/claude-code \
&& claude --version
# Set working directory for application
WORKDIR /app
# Copy application JAR (when used with an actual application)
# COPY --from=builder --chown=claude:claude /build/target/*.jar app.jar
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD claude --version || exit 1
# Default command (override in docker-compose or k8s)
ENTRYPOINT ["java"]
CMD ["--enable-preview", "-jar", "app.jar"]
# -----------------------------------------------------------------------------
# Stage 3: Development stage (includes build tools)
# -----------------------------------------------------------------------------
FROM runtime AS development
USER root
# Install development tools
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get update && apt-get install -y --no-install-recommends \
# Build tools
maven \
# Editors
vim \
# Debug tools
strace \
htop \
&& rm -rf /var/lib/apt/lists/*
# Install JDK instead of JRE for development
COPY --from=eclipse-temurin:25-jdk-noble /opt/java/openjdk /opt/java/openjdk
USER claude
WORKDIR /app
# Override entrypoint for development
ENTRYPOINT ["/bin/bash"]
CMD ["-c", "echo 'Development container ready. Run: mvn clean install'"]