克劳德桌面Docker MCP集成
一个全面、干净的MCP(模型上下文协议)服务器,用于通过Claude Desktop控制Docker Desktop。此实现提供了完整的Docker管理功能,同时兼容自定义服务器和Docker网关。
🎯 特性
完整的Docker控制
- 容器:启动、停止、重启、日志、统计、exec命令
- 图像:列出、拉、推、删除、构建、检查
- 网络:创建、列出、删除、检查
- 卷:创建、列出、删除、检查
- 系统:信息、版本、磁盘使用情况、清理/修剪
双重集成模式
- 自定义MCP服务器:功能齐全的Python实现
- Docker网关:官方Docker MCP集成
- 两者:同时运行两台服务器以获得最大兼容性
生产就绪
- 全面的错误处理
- Docker命令的安全验证
- 结构化JSON响应
- 完全符合MCP协议
- 零外部依赖(仅限Python stdlib)
🚀 快速设置
先决条件
- Docker桌面已安装并正在运行
- Python 3.7+(用于自定义服务器)
- Claude桌面应用程序
自动设置
cd Claude_modifcation
python3 setup_claude_desktop.py选择选项3(两台服务器)以获得最大兼容性。
手动设置
选项1:自定义MCP服务器
复制 claude_desktop_config.json 将内容添加到您的Claude Desktop配置中:
cp claude_desktop_config.json ~/Library/Application\ Support/Claude/claude_desktop_config.json选项2:仅限Docker网关
复制 claude_desktop_config_gateway.json 相反:
cp claude_desktop_config_gateway.json ~/Library/Application\ Support/Claude/claude_desktop_config.json重新启动克劳德桌面
完全关闭并重新启动Claude Desktop以使更改生效。
🧪 测试
测试MCP服务器
python3 test_mcp_server.py测试Docker集成
# Test Docker availability
docker --version
# Test Docker Gateway (if using)
docker mcp gateway --help在Claude Desktop中进行测试
在Claude Desktop中尝试以下命令:
"List all my Docker containers"
"Show Docker system information"
"Pull the hello-world image"
"Show me my Docker images"
"Start container named jupyter_embedded_dd_vm"📋 可用工具
容器管理
docker_ps-列出容器(正在运行或全部)docker_start-启动容器docker_stop-停止集装箱docker_restart-重新启动容器docker_logs-获取容器日志docker_stats-显示资源使用情况docker_exec-在容器中执行命令docker_inspect-检查集装箱
图像管理
docker_images-列出图片docker_pull-从注册表中提取图像docker_push-将图像推送到注册表docker_rmi-删除图像docker_build-从Dockerfile构建映像
网络管理
docker_network_ls-列出网络docker_network_create-创建网络docker_network_rm-删除网络
卷管理
docker_volume_ls-列出卷docker_volume_create-创建卷docker_volume_rm-删除卷
系统管理
docker_info-系统信息docker_version-版本信息docker_system_df-磁盘使用情况docker_system_prune-清理未使用的对象
集装箱作业
docker_run-运行具有完整配置的新容器
🔧 配置详情
自定义MCP服务器配置
{
"mcpServers": {
"docker-desktop-mcp": {
"command": "python3",
"args": ["/path/to/docker_mcp_server.py"],
"env": {
"PYTHONPATH": "/path/to/Claude_modifcation"
}
}
}
}Docker网关配置
{
"mcpServers": {
"MCP_DOCKER_GATEWAY": {
"command": "docker",
"args": ["mcp", "gateway", "run"]
}
}
}组合配置
安装脚本可以同时配置两台服务器,提供冗余和最大的功能覆盖。
🛡️ 安全特性
- 命令验证:所有Docker命令都经过验证和净化
- 论点逃逸:通过外壳注射保护
shlex.quote() - 超时保护:默认情况下,命令在30秒后超时
- 错误隔离:故障被安全地控制和报告
- 无特权:服务器仅以用户权限运行
🔍 故障排除
MCP服务器未启动
- 检查配置中的Python路径
- 验证脚本权限:
chmod +x docker_mcp_server.py - 手动测试:
python3 docker_mcp_server.py
Docker命令失败
- 验证Docker桌面是否正在运行
- 测试Docker CLI:
docker ps - 检查Docker权限
Claude Desktop无法识别服务器
- 验证配置文件位置
- 完全重新启动克劳德桌面
- 检查Claude Desktop日志中的MCP错误
Docker网关问题
- 将Docker Desktop更新到最新版本
- 测试网关:
docker mcp gateway --help - 尝试将自定义服务器作为后备
📁 文件结构
Claude_modifcation/
├── docker_mcp_server.py # Main MCP server implementation
├── claude_desktop_config.json # Custom server configuration
├── claude_desktop_config_gateway.json # Docker Gateway configuration
├── setup_claude_desktop.py # Automatic setup script
├── test_mcp_server.py # Test suite
├── requirements.txt # Dependencies (none required)
└── README.md # This documentation🎨 示例用法
基本集装箱管理
Claude: "Show me all my Docker containers"
> Lists all containers with status
Claude: "Start the container named web-server"
> Starts the specified container
Claude: "Get the logs for my nginx container"
> Retrieves and displays container logs系统管理
Claude: "Show Docker system information and disk usage"
> Displays comprehensive system info
Claude: "Clean up unused Docker objects to free space"
> Performs system cleanup开发流程
Claude: "Pull the latest Ubuntu image"
> Downloads Ubuntu image
Claude: "Run a temporary Ubuntu container with bash"
> Creates and starts interactive container
Claude: "Build a Docker image from the current directory"
> Builds image using local Dockerfile🔄 与Docker网关集成
此实现旨在与Docker的官方MCP网关兼容:
- 遵循MCP协议规范
- 使用标准的Docker CLI命令
- 提供结构化JSON响应
- 保持与Docker网关工具的兼容性
您可以同时运行自定义服务器和Docker网关,以获得最大的覆盖率和冗余。
🤝 贡献
服务器设计为易于扩展:
- 添加新工具
get_available_tools() - 在中实现工具处理程序
handle_tool_call() - 遵循现有的错误处理模式
- 使用提供的测试套件进行测试
📝 许可证
此实现仅使用Python标准库和Docker CLI,确保兼容性和最小依赖性。
