-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaude-cage
More file actions
executable file
·285 lines (255 loc) · 8.96 KB
/
claude-cage
File metadata and controls
executable file
·285 lines (255 loc) · 8.96 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env bash
set -euo pipefail
# --- Constants ---
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEFAULT_DOCKERFILE="${SCRIPT_DIR}/claude-cage.Dockerfile"
DEFAULT_IMAGE="claude-code-dev:latest"
CUSTOM_IMAGE="claude-code-custom:latest"
CLAUDE_HOST_PATH="${HOME}/.local/bin/claude"
# --- State ---
declare -a RW_DIRS=()
declare -a RO_DIRS=()
declare -a CLAUDE_ARGS=()
declare -a GPU_ARGS=()
declare -a TTY_ARGS=()
declare -a VOLUME_ARGS=()
declare -a EXTRA_CLAUDE_ARGS=()
CUSTOM_DOCKERFILE=""
IMAGE_OVERRIDE=""
IMAGE=""
CONTAINER_NAME=""
NO_GPU=false
FORCE_BUILD=false
DRY_RUN=false
WORKDIR=""
RESUME_CONTINUE=false
RESUME_FLAG=false
RESUME_ID=""
# --- Functions ---
function usage() {
cat <<'EOF'
Usage: claude-cage [OPTIONS] [--] [CLAUDE_ARGS...]
-w, --workdir DIR Project directory: mounted read-write and used as working
directory inside the container (default: first -d dir)
-d, --dir DIR Mount DIR read-write into the container (repeatable)
-r, --ro-dir DIR Mount DIR read-only into the container (repeatable)
-f, --dockerfile FILE Use a project-specific Dockerfile instead of the default
-i, --image IMAGE Use a pre-built image name instead of the default
-c, --continue Resume the most recent conversation (passes --continue to claude)
-R, --resume [ID] Resume a specific session (passes --resume [ID] to claude)
--no-gpu Skip NVIDIA runtime
--build Force rebuild the base image
--dry-run Print the docker command without executing
--name NAME Custom container name
-h, --help Show usage
If no -w given, the first -d directory is used as the working directory.
If no -d given either, the current directory is used.
Use -- to pass additional arguments to the claude binary.
Examples:
claude-cage -w /path/to/project
claude-cage -w /path/to/proj1 -d /path/to/proj2 -- --model opus
claude-cage -d . -- -p "explain this codebase"
claude-cage -w . -r /data/shared/datasets
claude-cage -c -w .
claude-cage -R abc123 -w .
claude-cage -f ./Dockerfile.claude -w .
EOF
exit 0
}
function die() { echo "error: $*" >&2; exit 1; }
function parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
-w|--workdir)
[[ -n "${2:-}" ]] || die "-w requires a directory argument"
WORKDIR="$2"; shift 2 ;;
-d|--dir)
[[ -n "${2:-}" ]] || die "-d requires a directory argument"
RW_DIRS+=("$2"); shift 2 ;;
-r|--ro-dir)
[[ -n "${2:-}" ]] || die "-r requires a directory argument"
RO_DIRS+=("$2"); shift 2 ;;
-f|--dockerfile)
[[ -n "${2:-}" ]] || die "-f requires a file argument"
CUSTOM_DOCKERFILE="$2"; shift 2 ;;
-i|--image)
[[ -n "${2:-}" ]] || die "-i requires an image name"
IMAGE_OVERRIDE="$2"; shift 2 ;;
-c|--continue)
RESUME_CONTINUE=true; shift ;;
-R|--resume)
RESUME_FLAG=true
if [[ -n "${2:-}" && "${2:0:1}" != "-" ]]; then
RESUME_ID="$2"; shift 2
else
RESUME_ID=""; shift
fi ;;
--no-gpu)
NO_GPU=true; shift ;;
--build)
FORCE_BUILD=true; shift ;;
--dry-run)
DRY_RUN=true; shift ;;
--name)
[[ -n "${2:-}" ]] || die "--name requires a name"
CONTAINER_NAME="$2"; shift 2 ;;
-h|--help)
usage ;;
--)
shift; CLAUDE_ARGS+=("$@"); break ;;
*)
die "unknown option: $1 (use -- to pass arguments to claude)" ;;
esac
done
# Default workdir to first -d dir, or current directory
if [[ -z "$WORKDIR" ]]; then
if [[ ${#RW_DIRS[@]} -gt 0 ]]; then
WORKDIR="${RW_DIRS[0]}"
else
WORKDIR="$(pwd)"
echo "warning: no -w or -d given, using current directory: $WORKDIR" >&2
fi
fi
# Ensure workdir is also mounted read-write
WORKDIR="$(readlink -f "$WORKDIR")"
WORKDIR_ALREADY_MOUNTED=false
for d in "${RW_DIRS[@]}"; do
if [[ "$(readlink -f "$d")" == "$WORKDIR" ]]; then
WORKDIR_ALREADY_MOUNTED=true
break
fi
done
if ! $WORKDIR_ALREADY_MOUNTED; then
RW_DIRS+=("$WORKDIR")
fi
# Resolve all paths to absolute
for i in "${!RW_DIRS[@]}"; do
RW_DIRS[$i]="$(readlink -f "${RW_DIRS[$i]}")"
done
for i in "${!RO_DIRS[@]}"; do
RO_DIRS[$i]="$(readlink -f "${RO_DIRS[$i]}")"
done
if [[ -n "$CUSTOM_DOCKERFILE" ]]; then
CUSTOM_DOCKERFILE="$(readlink -f "$CUSTOM_DOCKERFILE")"
fi
}
function validate() {
command -v docker >/dev/null 2>&1 || die "docker is not installed or not in PATH"
for d in "${RW_DIRS[@]}"; do
[[ -d "$d" ]] || die "directory does not exist: $d"
done
for d in "${RO_DIRS[@]}"; do
[[ -d "$d" ]] || die "read-only directory does not exist: $d"
done
[[ -d "$WORKDIR" ]] || die "working directory does not exist: $WORKDIR"
[[ -e "$CLAUDE_HOST_PATH" ]] || die "claude binary not found at $CLAUDE_HOST_PATH"
[[ -f "${HOME}/.claude.json" ]] || die "${HOME}/.claude.json not found — run claude on the host first to initialize config"
# Warn about overlapping RW/RO mounts
for ro in "${RO_DIRS[@]}"; do
for rw in "${RW_DIRS[@]}"; do
if [[ "$ro" == "$rw"/* || "$rw" == "$ro"/* ]]; then
echo "warning: overlapping mounts: rw=$rw and ro=$ro — the more specific mount takes precedence" >&2
fi
done
done
}
function determine_image() {
if [[ -n "$IMAGE_OVERRIDE" ]]; then
IMAGE="$IMAGE_OVERRIDE"
elif [[ -n "$CUSTOM_DOCKERFILE" ]]; then
[[ -f "$CUSTOM_DOCKERFILE" ]] || die "dockerfile not found: $CUSTOM_DOCKERFILE"
echo "Building custom image from ${CUSTOM_DOCKERFILE}..."
docker build -t "$CUSTOM_IMAGE" -f "$CUSTOM_DOCKERFILE" "$(dirname "$CUSTOM_DOCKERFILE")"
IMAGE="$CUSTOM_IMAGE"
else
IMAGE="$DEFAULT_IMAGE"
if $FORCE_BUILD || ! docker image inspect "$IMAGE" >/dev/null 2>&1; then
[[ -f "$DEFAULT_DOCKERFILE" ]] || die "default Dockerfile not found: $DEFAULT_DOCKERFILE"
echo "Building ${IMAGE} from ${DEFAULT_DOCKERFILE}..."
docker build -t "$IMAGE" -f "$DEFAULT_DOCKERFILE" "$SCRIPT_DIR"
fi
fi
}
function detect_gpu() {
if ! $NO_GPU; then
if docker info 2>/dev/null | grep -qi nvidia; then
GPU_ARGS+=(--runtime=nvidia --gpus all)
else
echo "warning: NVIDIA runtime not detected; launching without GPU support" >&2
fi
fi
}
function detect_tty() {
TTY_ARGS=(-i)
if [[ -t 0 && -t 1 ]]; then
TTY_ARGS=(-it)
fi
}
function build_volumes() {
# Claude install tree (read-write — let Claude manage its own state normally)
VOLUME_ARGS+=(-v "${HOME}/.local/share/claude:${HOME}/.local/share/claude")
VOLUME_ARGS+=(-v "${HOME}/.local/bin/claude:${HOME}/.local/bin/claude")
# Claude config/auth/memory (read-write for token refresh + session persistence)
VOLUME_ARGS+=(-v "${HOME}/.claude:${HOME}/.claude")
VOLUME_ARGS+=(-v "${HOME}/.claude.json:${HOME}/.claude.json")
# UID/GID resolution
VOLUME_ARGS+=(-v "/etc/passwd:/etc/passwd:ro")
VOLUME_ARGS+=(-v "/etc/group:/etc/group:ro")
# User-specified read-write directories (mounted at same absolute path)
for d in "${RW_DIRS[@]}"; do
VOLUME_ARGS+=(-v "${d}:${d}")
done
# User-specified read-only directories
for d in "${RO_DIRS[@]}"; do
VOLUME_ARGS+=(-v "${d}:${d}:ro")
done
}
function build_claude_args() {
if $RESUME_CONTINUE; then
EXTRA_CLAUDE_ARGS+=(--continue)
elif $RESUME_FLAG; then
if [[ -n "$RESUME_ID" ]]; then
EXTRA_CLAUDE_ARGS+=(--resume "$RESUME_ID")
else
EXTRA_CLAUDE_ARGS+=(--resume)
fi
fi
}
function run() {
if [[ -z "$CONTAINER_NAME" ]]; then
CONTAINER_NAME="claude-cage-$$"
fi
CMD=(
docker run --rm
"${TTY_ARGS[@]}"
"${GPU_ARGS[@]}"
--user "$(id -u):$(id -g)"
--name "$CONTAINER_NAME"
--hostname claude-cage
--tmpfs /tmp:exec
"${VOLUME_ARGS[@]}"
-e "HOME=${HOME}"
-e "TERM=${TERM:-xterm-256color}"
-e "LANG=${LANG:-en_US.UTF-8}"
-e "GIT_CONFIG_COUNT=1"
-e "GIT_CONFIG_KEY_0=safe.directory"
-e "GIT_CONFIG_VALUE_0=*"
-w "$WORKDIR"
"$IMAGE"
"${CLAUDE_HOST_PATH}" "${EXTRA_CLAUDE_ARGS[@]}" "${CLAUDE_ARGS[@]}"
)
if $DRY_RUN; then
echo "${CMD[@]}"
exit 0
fi
exec "${CMD[@]}"
}
# --- Main ---
parse_args "$@"
validate
determine_image
detect_gpu
detect_tty
build_volumes
build_claude_args
run