How to Manage Docker Containers with Natural Language Using MCP Docker Server
This guide explains how the open‑source Model Context Protocol (MCP) enables natural‑language control of Docker containers, walks through environment setup, Spring AI integration, client project configuration, and demonstrates a full execution log showing automated image pulling, port mapping, volume mounting, and container startup.
MCP (Model Context Protocol) is an innovative open‑source protocol designed to dramatically simplify AI application development by providing a standardized communication interface that bridges AI models and application contexts.
MCP Docker Server Usage Guide
With the growing popularity of containerization, Docker management often requires many commands and configurations. The MCP Docker Server is a revolutionary tool that lets you manage Docker containers using natural language, lowering the entry barrier.
By describing actions in everyday language, such as "Start an Nginx container and map it to port 8080, mounting a specific directory for static files," the AI automatically performs all necessary deployment steps, including:
Pulling the appropriate image
Configuring port mapping
Setting up directory mounts
Starting and managing the container
This approach streamlines Docker usage, improves team efficiency, and reduces human error, benefiting both Docker beginners and experienced operators.
Spring AI and Docker Integration
Spring AI offers robust MCP integration, allowing developers to easily embed AI capabilities into Docker container management.
Environment Preparation
Install
uv <code># The author uses macOS; on Windows, search for installation instructions
brew install uv
</code>Download the
mcp-docker-serverplugin
<code>git clone https://github.com/ckreiling/mcp-server-docker.git
</code>Create Client Project
First, add the required dependencies to your project:
<code><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>
</code>Configuration File
The example uses the DeepSeek v3 model. Add the following to
application.properties:
<code>spring.ai.openai.chat.options.model=deepseek-chat
spring.ai.openai.base-url=https://api.deepseek.com
spring.ai.openai.api-key=sk-XXX
</code>MCP Docker Client Implementation
Below is a sample implementation that uses Spring AI and MCP to manage Docker containers:
<code>@Bean
public CommandLineRunner dockerCommands(ChatClient.Builder chatClientBuilder,
List<McpFunctionCallback> functionCallbacks,
ConfigurableApplicationContext context) {
return args -> {
var chatClient = chatClientBuilder
.defaultFunctions(functionCallbacks.toArray(new McpFunctionCallback[0]))
.build();
// Example: manage Docker container with natural language
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() {
// Configure Docker MCP server, pointing to the mcp-server-docker plugin directory
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 service initialization status: " + init);
return mcpClient;
}
</code>Execution Log Example
The following shows a complete execution process, demonstrating how natural‑language commands manage Docker containers:
<code># 1. Execute natural language command
执行命令: 启动一个 Nginx 容器并映射到 8888 端口,并挂载 /Users/lengleng/work/open/pig-ui/docker/dist 的静态页面
# 2. System checks existing containers
发现冲突: 容器名称 "/nginx_container" 已被容器 "3f20f2720263..." 使用
状态: 系统自动处理冲突,移除旧容器
# 3. Create new container
容器ID: db72b87c2069...
容器名称: nginx_container
状态: 创建成功
# 4. Start container
状态: 运行中
端口映射: 80 -> 8888
目录挂载: /Users/lengleng/work/open/pig-ui/docker/dist -> /usr/share/nginx/html
# 5. Final result
✅ Nginx 容器成功启动
✅ 端口 8888 成功映射
✅ 静态文件目录成功挂载
</code>From this log we can see that the MCP Docker Server:
Intelligently understands natural‑language commands
Automatically resolves container name conflicts
Correctly configures port mappings and volume mounts
Completes container creation and startup
This automation greatly simplifies Docker container management, allowing even complex operations to be performed with simple natural‑language instructions.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.