使用自然语言管理 Docker 容器的 MCP 服务器使用教程
# 笔者这里使用 mac , 如果Windows的话去搜索一下安装就行啦 brew install uv
git clone https://github.com/ckreiling/mcp-server-docker.git
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-bom</artifactId> <version>1.0.0-M5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-openai-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.experimental</groupId> <artifactId>spring-ai-mcp</artifactId> <version>0.4.1</version> </dependency> </dependencies>
spring.ai.openai.chat.options.model=deepseek-chat spring.ai.openai.base-url=https://api.deepseek.com spring.ai.openai.api-key=sk-XXX
@Bean public CommandLineRunner dockerCommands(ChatClient.Builder chatClientBuilder, List<McpFunctionCallback> functionCallbacks, ConfigurableApplicationContext context) { return args -> { var chatClient = chatClientBuilder .defaultFunctions(functionCallbacks.toArray(new McpFunctionCallback[0])) .build(); // 示例:使用自然语言管理 Docker 容器 String command = "启动一个 Nginx 容器并映射到 8888 端口 ,并挂载 /Users/lengleng/work/open/pig-ui/docker/dist 的静态页面"; System.out.println("执行命令: " + command); System.out.println("AI 响应: " + chatClient.prompt(command).call().content()); context.close(); }; } @Bean public List<McpFunctionCallback> functionCallbacks(McpSyncClient mcpClient) { return mcpClient.listTools(null) .tools() .stream() .map(tool -> new McpFunctionCallback(mcpClient, tool)) .toList(); } @Bean(destroyMethod = "close") public McpSyncClient mcpClient() { // 配置 Docker MCP 服务器,指向 mcp-server-docker 插件的路径 var dockerParams = ServerParameters.builder("uv") .args("--directory", "/Users/lengleng/Downloads/mcp-server-docker", "run", "mcp-server-docker") .build(); var mcpClient = McpClient.using(new StdioClientTransport(dockerParams)) .requestTimeout(Duration.ofSeconds(30)) .sync(); var init = mcpClient.initialize(); System.out.println("Docker MCP 服务初始化状态: " + init); return mcpClient; }
# 1. 执行自然语言命令 执行命令: 启动一个 Nginx 容器并映射到 8888 端口,并挂载 /Users/lengleng/work/open/pig-ui/docker/dist 的静态页面 # 2. 系统检查现有容器 发现冲突: 容器名称 "/nginx_container" 已被容器 "3f20f2720263..." 使用 状态: 系统自动处理冲突,移除旧容器 # 3. 创建新容器 容器ID: db72b87c2069... 容器名称: nginx_container 状态: 创建成功 # 4. 启动容器 状态: 运行中 端口映射: 80 -> 8888 目录挂载: /Users/lengleng/work/open/pig-ui/docker/dist -> /usr/share/nginx/html # 5. 最终结果 ✅ Nginx 容器成功启动 ✅ 端口 8888 成功映射 ✅ 静态文件目录成功挂载