-
Notifications
You must be signed in to change notification settings - Fork 8
Open
Description
问题描述
在使用Claude Docker模式时,当容器挂载宿主机的git目录后,在容器内执行git操作会遇到"safe directory"错误。
根本原因
- 用户ID不匹配:容器内的
codeagent
用户(uid 24368)与宿主机用户不同 - Git安全检查:现代Git版本会检查目录所有权,认为跨用户的目录是"不安全"的
- 挂载目录权限:挂载的
/workspace
目录属于宿主机用户,但Git命令在容器内以codeagent
用户执行
错误表现
fatal: detected dubious ownership in repository at '/workspace'
To add an exception for this directory, call:
git config --global --add safe.directory /workspace
解决方案
采用Dockerfile层面预配置的方式,在构建容器镜像时就为codeagent
用户预设git safe.directory配置。
修改内容
在Dockerfile
中添加:
# 配置Git safe.directory以解决挂载目录权限问题
RUN git config --global --add safe.directory /workspace
优势
- 一次配置,持久生效:在镜像构建时就解决问题
- 性能最优:不需要在每次容器启动或命令执行时重复配置
- 简单可靠:直接在用户环境中预设信任的目录
测试验证
修复后需要验证:
- 重新构建Docker镜像
- 启动容器挂载git目录
- 在容器内执行git命令确认不再报错
相关文件
Dockerfile
: 添加safe.directory配置internal/code/claude_docker.go
: Docker容器创建逻辑
Metadata
Metadata
Assignees
Labels
No labels