使用说明
Docker MCP服务器
一个模型上下文协议(MCP)服务器,使Claude和其他AI助手能够通过自然语言命令管理Docker容器、映像、网络和卷。
状态:第4阶段-测试版
第4阶段,具备网络和卷管理功能。
可用工具
系统工具(第一阶段)
| 工具 | 描述 | 状态 |
|---|---|---|
ping |
测试与Docker守护进程的连接 | ✅ 可用 |
version |
获取Docker版本信息 | ✅ 可用 |
集装箱管理(第二阶段)
| 工具 | 描述 | 状态 |
|---|---|---|
list_containers |
列出带有过滤器的容器(全部、限制、状态) | ✅ 可用 |
start_container |
启动已停止的容器 | ✅ 可用 |
stop_container |
超时停止正在运行的容器 | ✅ 可用 |
restart_container |
在超时的情况下重新启动容器 | ✅ 可用 |
remove_container |
移除容器(力、体积) | ✅ 可用 |
fetch_container_logs |
获取带有尾部/时间戳的容器日志 | ✅ 可用 |
run_container |
创建并启动新容器 | ✅ 可用 |
inspect_container |
获取集装箱详细信息 | ✅ 可用 |
图像管理(第3阶段)
| 工具 | 描述 | 状态 |
|---|---|---|
list_images |
列出带过滤器的图像 | ✅ 可用 |
pull_image |
通过身份验证从注册表中提取图像 | ✅ 可用 |
push_image |
通过身份验证将图像推送到注册表 | ✅ 可用 |
remove_image |
删除图像(强制,无修剪选项) | ✅ 可用 |
build_image |
使用上下文从Dockerfile构建映像 | ✅ 可用 |
search_images |
在Docker Hub中搜索映像 | ✅ 可用 |
网络管理(第四阶段)
| 工具 | 描述 | 状态 |
|---|---|---|
list_networks |
列出所有带过滤器的Docker网络 | ✅ 可用 |
inspect_network |
获取详细的网络信息 | ✅ 可用 |
create_network |
创建具有驱动程序支持的新Docker网络 | ✅ 可用 |
remove_network |
删除Docker网络 | ✅ 可用 |
connect_network |
将容器连接到网络 | ✅ 可用 |
disconnect_network |
断开容器与网络的连接 | ✅ 可用 |
卷管理(第4阶段)
| 工具 | 描述 | 状态 |
|---|---|---|
list_volumes |
列出所有带过滤器的Docker卷 | ✅ 可用 |
inspect_volume |
获取详细的卷信息 | ✅ 可用 |
create_volume |
创建新的Docker卷 | ✅ 可用 |
remove_volume |
删除Docker卷(⚠️ 破坏性) | ✅ 可用 |
prune_volumes |
删除所有未使用的卷(⚠️ 破坏性) | ✅ 可用 |
路线图
- 第一阶段 ✅ 完整:基础架构、ping、版本工具
- 第2阶段 ✅ 完成:容器管理(列表、启动、停止、日志、运行、检查)
- 第三期 ✅ 完成:图像管理(列表、拉取、推送、删除、构建、搜索)
- 阶段4 ✅ 完成:网络和卷管理(列表、创建、删除、连接/断开连接)
- 阶段5 【下一篇】:波兰语、高级功能、量产发布
需求
- 达到1.21或更高
- Docker引擎在本地运行或可通过Docker_HOST访问
- Claude Desktop或其他兼容MCP的客户端
安装
从源代码构建
# Clone or navigate to the repository
cd docker-mcp-server
# Build the binary
go build -o docker-mcp-server
# Make it executable (Unix-like systems)
chmod +x docker-mcp-server
跨平台构建
# Linux (amd64)
GOOS=linux GOARCH=amd64 go build -o docker-mcp-server-linux-amd64
# macOS (arm64, Apple Silicon)
GOOS=darwin GOARCH=arm64 go build -o docker-mcp-server-darwin-arm64
# macOS (amd64, Intel)
GOOS=darwin GOARCH=amd64 go build -o docker-mcp-server-darwin-amd64
# Windows (amd64)
GOOS=windows GOARCH=amd64 go build -o docker-mcp-server-windows-amd64.exe
配置
克劳德桌面版
将以下内容添加到您的Claude Desktop配置文件中:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
视窗: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"docker": {
"command": "/absolute/path/to/docker-mcp-server",
"args": [],
"env": {
"DOCKER_HOST": "unix:///var/run/docker.sock"
}
}
}
}
环境变量
| 变量 | 描述 | 默认值 |
|---|---|---|
DOCKER_HOST |
Docker守护进程套接字或TCP地址 | unix:///var/run/docker.sock |
DOCKER_TLS_VERIFY |
启用TLS验证 | 未设置 |
DOCKER_CERT_PATH |
TLS证书的路径 | 未设置 |
远程Docker主机
要连接到远程Docker主机:
{
"mcpServers": {
"docker-remote": {
"command": "/path/to/docker-mcp-server",
"env": {
"DOCKER_HOST": "tcp://192.168.1.100:2375"
}
}
}
}
SSH连接
{
"mcpServers": {
"docker-ssh": {
"command": "/path/to/docker-mcp-server",
"env": {
"DOCKER_HOST": "ssh://user@remote-host"
}
}
}
}
测试
使用MCP检查器
# Install MCP Inspector (Node.js required)
npx @modelcontextprotocol/inspector ./docker-mcp-server
# The inspector will open in your browser
# Test available tools:
# 1. tools/list - Should show 27 tools (2 system + 8 container + 6 image + 6 network + 5 volume)
# 2. tools/call ping - Should return "Docker daemon is reachable"
# 3. tools/call version - Should return Docker version information
# 4. tools/call list_containers - Should list running containers
# 5. tools/call list_images - Should list Docker images
# 6. tools/call list_networks - Should list Docker networks
# 7. tools/call list_volumes - Should list Docker volumes
手动测试
# Verify Docker is running
docker ps
# Build the server
go build -o docker-mcp-server
# Test the binary (it will wait for MCP protocol input on stdin)
# Press Ctrl+C to exit
./docker-mcp-server
与Claude Desktop一起使用
- 如上所述进行构建和配置
- 重新启动克劳德桌面
- 开始对话并尝试:
系统命令:
- “你有什么Docker工具?”
- “Ping Docker守护进程”
- “我运行的是哪个版本的Docker?”
集装箱管理:
- “列出所有正在运行的容器”
- “显示所有容器,包括已停止的容器”
- “启动名为my app的容器”
- “停止容器abc123,超时5秒”
- “从容器中获取我的应用程序的日志,最后50行”
- “在端口8080上运行一个新的nginx容器”
- “检查容器我的应用程序并显示其配置”
- “删除已停止的容器abc123”
图像管理:
- “列出所有Docker镜像”
- “在Docker Hub上搜索nginx镜像”
- “拉nginx:alpine镜像”
- “删除nginx:latest镜像”
- “从当前目录中的Dockerfile构建映像”
网络管理:
- “列出所有Docker网络”
- “创建一个名为我的网络的新网桥网络”
- “将容器我的应用程序连接到网络我的网络”
- “检查网桥”
- “断开容器与网络的连接”
卷管理:
- “列出所有Docker卷”
- “创建一个名为“我的数据”的新卷”
- “检查我的数据量”
- “删除未使用的卷”
使用示例
容器管理
列出所有正在运行的容器:
Claude: "List all running Docker containers"
列出所有容器,包括已停止的:
Claude: "Show me all containers including stopped ones"
启动容器:
Claude: "Start the container named my-nginx"
使用自定义超时停止容器:
Claude: "Stop container abc123 with 30 second timeout"
获取容器日志:
Claude: "Get the last 100 lines of logs from container my-app"
Claude: "Show all logs from container nginx-prod with timestamps"
运行一个新容器:
Claude: "Run a new nginx container named web-server on port 8080"
Claude: "Create and start a redis container with port 6379 exposed"
检查集装箱细节:
Claude: "Inspect container my-app and show its configuration"
移除已停止的容器:
Claude: "Remove the stopped container abc123"
Claude: "Force remove container xyz789 and its volumes"
重新启动容器:
Claude: "Restart the container named api-server"
图像管理
列出所有图像:
Claude: "List all Docker images on my system"
搜索Docker Hub:
Claude: "Search for official nginx images on Docker Hub"
拉取图像:
Claude: "Pull the nginx:alpine image from Docker Hub"
Claude: "Pull redis:latest"
推送图像:
Claude: "Push myapp:latest to Docker Hub"
删除图像:
Claude: "Remove the image nginx:old-version"
Claude: "Force remove the image abc123"
塑造形象:
Claude: "Build an image from Dockerfile in current directory with tag myapp:v1.0"
Claude: "Build image with context ./app and tags myapp:latest, myapp:1.0"
网络管理
列出所有网络:
Claude: "List all Docker networks"
Claude: "Show me Docker networks filtered by driver=bridge"
创建网络:
Claude: "Create a new bridge network named my-app-network"
Claude: "Create an internal network named backend-net with subnet 172.20.0.0/16"
检查网络:
Claude: "Inspect the bridge network and show its configuration"
Claude: "Get details about the my-app-network network"
将容器连接到网络:
Claude: "Connect container my-app to network my-app-network"
Claude: "Connect nginx container to backend-net with alias web-server"
断开容器与网络的连接:
Claude: "Disconnect container my-app from network my-app-network"
删除网络:
Claude: "Remove the network my-app-network"
卷管理
列出所有卷:
Claude: "List all Docker volumes"
Claude: "Show me volumes that are dangling"
创建卷:
Claude: "Create a new volume named my-data"
Claude: "Create a volume with custom driver options"
检查体积:
Claude: "Inspect volume my-data and show its configuration"
删除卷:
Claude: "Remove the volume old-data"
Claude: "Force remove volume my-data even if it's in use"
修剪未使用的体积:
Claude: "Remove all unused Docker volumes to free up space"
发展
项目结构
docker-mcp-server/
├── main.go # Entry point
├── go.mod # Go module definition
├── internal/
│ ├── docker/
│ │ ├── client.go # Docker client wrapper
│ │ ├── system.go # System operations (ping, version)
│ │ ├── container.go # Container operations (Phase 2)
│ │ ├── image.go # Image operations (Phase 3)
│ │ ├── network.go # Network operations (Phase 4)
│ │ └── volume.go # Volume operations (Phase 4)
│ ├── tools/
│ │ ├── registry.go # Tool registration
│ │ ├── system.go # System tool handlers
│ │ ├── container.go # Container tool handlers (Phase 2)
│ │ ├── image.go # Image tool handlers (Phase 3)
│ │ ├── network.go # Network tool handlers (Phase 4)
│ │ └── volume.go # Volume tool handlers (Phase 4)
│ └── types/
│ ├── inputs.go # Input struct definitions
│ └── errors.go # Error types
└── examples/
└── claude_desktop_config.json
添加新工具
即将到来的阶段:
- 阶段5 (下一篇):高级功能(exec、stats、其他图像/容器工具)
有关实施细节,请参阅:
安全考虑
- 在没有正确身份验证的情况下,永远不要暴露Docker套接字
- 使用远程Docker连接时要小心
- 在生产使用之前审查人工智能创建的容器
- 避免在容器配置中存储敏感数据
故障排除
服务器无法启动
# Check Docker is running
docker ps
# Check Docker socket permissions
ls -la /var/run/docker.sock
# View server logs (they go to stderr)
./docker-mcp-server 2>&1 | tee server.log
Claude Desktop看不到服务器
- 验证配置文件路径是否正确
- 确保配置中的二进制路径是绝对的
- 检查二进制文件的权限
- 完全重新启动克劳德桌面
- 检查克劳德桌面日志
连接错误
- 验证
DOCKER_HOST设置正确 - 检查远程连接的防火墙设置
- 确保Docker守护进程可访问
贡献
该项目尚处于早期开发阶段。一旦我们进入第二阶段,欢迎捐款。
参考文献
许可证
MIT许可证(待添加)
版本
当前: 0.4.0-beta (第4阶段-网络和卷管理完成)
