The Docker MCP Server provides a secure way to execute code in isolated Docker containers and return results to language models like Claude. It supports multiple languages, complex scripts, package management, and container management with robust error handling.
Clone the repository:
git clone https://github.com/yourusername/docker_mcp_server.git
cd docker_mcp_server
Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
Install required packages:
pip install -r requirements.txt
To test and explore the server's functionality:
python run_server.py
The MCP Inspector interface will open in your browser at http://localhost:5173.
Lists all Docker containers and their details:
list_containers(show_all=True) # show_all is optional, defaults to True
Creates and starts a Docker container with optional dependencies:
create_container(
image="python:3.9-slim",
container_name="python-example",
dependencies="numpy pandas" # optional
)
Installs additional packages in an existing Docker container:
add_dependencies(
container_name="python-example",
dependencies="matplotlib scikit-learn"
)
Executes a command inside a running Docker container:
execute_code(
container_name="python-example",
command="python -c 'import numpy as np; print(\"NumPy version:\", np.__version__)'"
)
Executes a multi-line Python script inside a running Docker container:
execute_python_script(
container_name="python-example",
script_content="your_python_code_here",
script_args="" # optional
)
Stops and removes a Docker container:
cleanup_container(container_name="python-example")
# 1. List existing containers to see what's already running
list_containers()
# 2. Create a new container
create_container(
image="python:3.9-slim",
container_name="python-example",
dependencies="numpy pandas"
)
# 3. Execute a command in the container
execute_code(
container_name="python-example",
command="python -c 'import numpy as np; print(\"NumPy version:\", np.__version__)'"
)
# 4. Add more dependencies later
add_dependencies(
container_name="python-example",
dependencies="matplotlib scikit-learn"
)
# 5. List containers again to confirm status
list_containers(show_all=False) # Only show running containers
# 6. Clean up when done
cleanup_container(container_name="python-example")
# 1. Create a container with dependencies
create_container(
image="python:3.9-slim",
container_name="python-test",
dependencies="numpy pandas matplotlib"
)
# 2. Execute a Python script
script = """
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Create some data
data = pd.DataFrame({
'x': np.random.randn(100),
'y': np.random.randn(100)
})
print(f"Data shape: {data.shape}")
print(f"Data correlation: {data.corr().iloc[0,1]:.4f}")
"""
execute_python_script(container_name="python-test", script_content=script)
# 3. Add additional dependencies later if needed
add_dependencies(container_name="python-test", dependencies="scikit-learn")
# 4. Verify container is running
list_containers(show_all=False)
# 5. Clean up when done
cleanup_container(container_name="python-test")
# 1. Check for existing Node.js containers
list_containers()
# 2. Create a Node.js container
create_container(
image="node:16",
container_name="node-test",
dependencies="express axios"
)
# 3. Execute a Node.js script
execute_code(
container_name="node-test",
command="node -e \"console.log('Node.js version: ' + process.version); console.log('Express installed: ' + require.resolve('express'));\""
)
# 4. Add more dependencies
add_dependencies(container_name="node-test", dependencies="lodash moment")
# 5. Clean up when done
cleanup_container(container_name="node-test")
The Docker MCP server automatically detects and uses the appropriate package manager:
pip
npm
apt-get
apk
To register the MCP server with Claude:
fastmcp install src/docker_mcp.py
docker --version
To add this MCP server to Claude Code, run this command in your terminal:
claude mcp add-json "docker-mcp" '{"command":"python","args":["run_server.py"]}'
See the official Claude Code MCP documentation for more details.
There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json
file so that it is available in all of your projects.
If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json
file.
To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".
When you click that button the ~/.cursor/mcp.json
file will be opened and you can add your server like this:
{
"mcpServers": {
"docker-mcp": {
"command": "python",
"args": [
"run_server.py"
]
}
}
}
To add an MCP server to a project you can create a new .cursor/mcp.json
file or add it to the existing one. This will look exactly the same as the global MCP server example above.
Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.
The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.
You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.
To add this MCP server to Claude Desktop:
1. Find your configuration file:
~/Library/Application Support/Claude/claude_desktop_config.json
%APPDATA%\Claude\claude_desktop_config.json
~/.config/Claude/claude_desktop_config.json
2. Add this to your configuration file:
{
"mcpServers": {
"docker-mcp": {
"command": "python",
"args": [
"run_server.py"
]
}
}
}
3. Restart Claude Desktop for the changes to take effect