File size: 2,273 Bytes
d664931 | 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 | #!/usr/bin/env bash
# Entrypoint for the Claude Code container.
# Creates a user matching the host UID/GID so that files created
# inside the container have the correct ownership on the host volume.
set -e
# claude.sh starts a git worktree in its host path. env.sh sets paths from
# its own location, so source the copy there to keep the paths the same.
ENV_SH=/workspace/env.sh
if [ -f "${PWD}/env.sh" ]; then
ENV_SH="${PWD}/env.sh"
fi
if [ -n "${HOST_UID}" ] && [ -n "${HOST_GID}" ]; then
USER_NAME="claude-user"
USER_HOME="/home/${USER_NAME}"
# Create group (reuse existing if GID is taken)
if ! getent group "${HOST_GID}" > /dev/null 2>&1; then
groupadd --gid "${HOST_GID}" "${USER_NAME}"
fi
GROUP_NAME=$(getent group "${HOST_GID}" | cut -d: -f1)
# Create user (reuse existing if UID is taken)
if ! getent passwd "${HOST_UID}" > /dev/null 2>&1; then
useradd \
--uid "${HOST_UID}" \
--gid "${HOST_GID}" \
--groups sudo \
--no-create-home \
--home-dir "${USER_HOME}" \
--shell /bin/bash \
"${USER_NAME}"
else
USER_NAME=$(getent passwd "${HOST_UID}" | cut -d: -f1)
usermod --home "${USER_HOME}" "${USER_NAME}"
fi
# Passwordless sudo
echo "${USER_NAME} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/claude-user
chmod 0440 /etc/sudoers.d/claude-user
# The engine creates .cache as root when it mounts .cache/bazel.
# Bazelisk writes to .cache/bazelisk, so the user owns .cache too.
mkdir -p "${USER_HOME}/.cache"
export HOME="${USER_HOME}"
chown "${HOST_UID}:${HOST_GID}" "${USER_HOME}" "${USER_HOME}/.cache"
touch "${USER_HOME}/.hushlogin"
# Set up ORFS environment (PATH to tools, FLOW_HOME, etc.)
# Sourced here; inherited through setpriv into all child processes.
if [ -f "${ENV_SH}" ]; then
set +e
. "${ENV_SH}" > /dev/null
set -e
fi
exec setpriv \
--reuid "${HOST_UID}" \
--regid "${HOST_GID}" \
--init-groups \
-- "$@"
else
# No UID/GID mapping requested; run as root
if [ -f "${ENV_SH}" ]; then
set +e
. "${ENV_SH}" > /dev/null
set -e
fi
exec "$@"
fi
|