Docker MCP Server

Docker MCP Server

Enables LLMs to safely execute code in isolated Docker containers with resource limits and security controls, supporting session management and automatic dependency installation.

Category
访问服务器

README

Docker MCP Server

A Model Context Protocol (MCP) server that enables LLMs to safely execute code in isolated Docker containers with strict resource limits and security controls.

Features

  • 🔒 Secure Isolation: Containers run with strict resource limits (memory, CPU, PIDs)
  • 🏷️ Session Management: Group containers by session with persistent workspaces
  • ♻️ Container Reuse: Optimize performance by reusing existing containers
  • 📦 Smart Dependencies: Auto-detect and install packages (pip, npm, apt, apk)
  • 🔄 Streaming Output: Real-time output for long-running processes
  • 💾 Persistent Workspaces: Session-based volumes maintain state across executions

Installation

# Clone the repository
git clone https://github.com/cevatkerim/docker-mcp.git
cd docker-mcp

# Install in development mode
pip install -e .

# Install development dependencies
pip install -r requirements-dev.txt

Prerequisites

  • Python 3.10+
  • Docker Engine running locally
  • MCP-compatible client (e.g., Claude Desktop)

Quick Start

1. Start the MCP Server

python -m docker_mcp

2. Configure Your MCP Client

Add to your MCP client configuration:

{
  "mcpServers": {
    "docker": {
      "command": "python",
      "args": ["-m", "docker_mcp"]
    }
  }
}

Available Tools

1. check_engine

Check Docker engine availability and version.

result = check_engine()
# Returns: Docker version and status

2. list_containers

List Docker containers with optional filtering.

result = list_containers(
    show_all=True,  # Show all containers, not just running
    session_id="my-session"  # Filter by session
)

3. create_container

Create and start a new container with resource limits.

result = create_container(
    image="python:3.11-slim",
    name="my-container",
    session_id="my-session",
    network_enabled=False,  # Network isolation by default
    reuse_existing=True,    # Reuse if exists
    environment={"KEY": "value"}
)

4. execute_code

Execute commands in a container.

result = execute_code(
    container_id="my-container",
    command="echo 'Hello, World!'",
    timeout=30,
    stream=True,  # Stream output in real-time
    working_dir="/workspace"
)

5. execute_python_script

Execute Python scripts with automatic dependency management.

result = execute_python_script(
    container_id="my-container",
    script="import numpy; print(numpy.__version__)",
    packages=["numpy"],  # Auto-install if needed
    timeout=60
)

6. add_dependencies

Install packages in a running container.

result = add_dependencies(
    container_id="my-container",
    packages=["requests", "pandas"],
    package_manager="pip"  # Auto-detected if not specified
)

7. cleanup_container

Stop and remove containers with optional volume cleanup.

# Remove specific container
result = cleanup_container(container_id="my-container")

# Remove all containers for a session
result = cleanup_container(session_id="my-session", remove_volumes=True)

# Remove all MCP-managed containers
result = cleanup_container(cleanup_all=True)

Security Features

Resource Limits

  • Memory: 1GB default (configurable)
  • CPU: 1.0 cores default (configurable)
  • Process IDs: 512 max (configurable)
  • Network: Isolated by default, opt-in for network access

Container Labels

All containers are labeled with mcp-managed=true for easy identification and cleanup.

Workspace Isolation

Each container gets a /workspace directory backed by a named volume, preventing host filesystem access.

Configuration

Configure via environment variables:

export DOCKER_MCP_MEMORY_LIMIT=2147483648  # 2GB in bytes
export DOCKER_MCP_CPU_LIMIT=2.0            # 2 CPU cores
export DOCKER_MCP_PIDS_LIMIT=1024          # Max processes
export DOCKER_MCP_TIMEOUT=60               # Default timeout
export DOCKER_MCP_DEBUG=true               # Enable debug logging

Examples

Example 1: Python Data Analysis

# Create a container for data analysis
container = create_container(
    image="python:3.11-slim",
    session_id="data-analysis"
)

# Install required packages
add_dependencies(
    container_id=container['container_id'],
    packages=["pandas", "matplotlib", "seaborn"]
)

# Execute analysis script
script = """
import pandas as pd
import matplotlib.pyplot as plt

# Create sample data
df = pd.DataFrame({
    'x': range(10),
    'y': [i**2 for i in range(10)]
})

# Save plot
df.plot(x='x', y='y')
plt.savefig('/workspace/plot.png')
print("Plot saved to /workspace/plot.png")
print(df.describe())
"""

execute_python_script(
    container_id=container['container_id'],
    script=script
)

Example 2: Node.js Development

# Create Node.js container
container = create_container(
    image="node:18-alpine",
    session_id="nodejs-dev",
    network_enabled=True  # Need network for npm
)

# Install packages
add_dependencies(
    container_id=container['container_id'],
    packages=["express", "axios"],
    package_manager="npm"
)

# Run Node.js code
execute_code(
    container_id=container['container_id'],
    command="node -e \"console.log('Node version:', process.version)\""
)

Example 3: Multi-Language Project

# Create container with Python and Node.js
container = create_container(
    image="nikolaik/python-nodejs:python3.11-nodejs18",
    session_id="multi-lang"
)

# Install Python packages
add_dependencies(
    container_id=container['container_id'],
    packages=["fastapi", "uvicorn"],
    package_manager="pip"
)

# Install Node packages
add_dependencies(
    container_id=container['container_id'],
    packages=["webpack", "babel-core"],
    package_manager="npm"
)

Development

Running Tests

# Run all tests
pytest

# Run with coverage
pytest --cov=src --cov-report=term

# Run specific test file
pytest tests/test_docker_client.py -v

Project Structure

docker-mcp/
├── src/
│   └── docker_mcp/
│       ├── __init__.py
│       ├── server.py         # MCP server implementation
│       ├── container_ops.py  # Tool implementations
│       ├── docker_client.py  # Docker SDK wrapper
│       ├── config.py         # Configuration
│       └── schemas.py        # Data models
├── tests/
│   ├── test_docker_client.py
│   ├── test_tools_comprehensive.py
│   └── ...
├── pyproject.toml
├── requirements.txt
└── README.md

Troubleshooting

Docker Not Available

Error: Cannot connect to Docker daemon

Solution: Ensure Docker Desktop is running and the Docker socket is accessible.

Permission Denied

Error: Permission denied while trying to connect to Docker daemon

Solution: Add your user to the docker group or run with appropriate permissions.

Container Creation Failed

Error: Image not found

Solution: The image will be automatically pulled. Ensure you have internet connectivity.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Write tests for new functionality
  4. Implement the feature
  5. Ensure all tests pass
  6. Submit a pull request

License

MIT License - see LICENSE file for details.

Support

For issues and questions, please open an issue on GitHub.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选