kalyanram262/docker-mcp-server
If you are the rightful owner of docker-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
A Model Context Protocol (MCP) server for managing Docker containers, images, networks, and volumes on your local machine, with FastAPI HTTP interface.
Docker MCP Server
A Model Context Protocol (MCP) server for managing Docker containers, images, networks, and volumes on your local machine, with FastAPI HTTP interface.
Features
- Container Management: List, create, start, stop, and remove containers
- Image Management: List, pull, and remove Docker images
- Network Management: List and manage Docker networks
- Volume Management: List and manage Docker volumes
- FastAPI HTTP Interface: RESTful API endpoints for all operations
- Health Check: Built-in health check endpoint for monitoring
Prerequisites
- Docker and Docker Compose installed and running on your system
- Port 8000 available for the FastAPI server
๐ Installation
๐ Quick Start with Docker Compose
-
Clone the repository:
git clone https://github.com/yourusername/docker-mcp-server.git cd docker-mcp-server
-
Start the services:
docker-compose up -d
This will:
- Build the Docker image
- Start the MCP server with FastAPI interface
- Mount the Docker socket for container management
- Start an example Nginx service on port 8080
- Expose port 8000 for the FastAPI server
-
Verify the services are running:
docker-compose ps
You should see both
docker-mcp-server
andexample-service
running. -
Check the health status:
curl http://localhost:8000/health
Should return:
{"status":"ok"}
Option 2: Local Development
-
Clone the repository:
git clone https://github.com/yourusername/docker-mcp-server.git cd docker-mcp-server
-
Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Start the server:
python docker_mcp_server.py
Building the Docker Image Manually
If you prefer to build the Docker image manually:
docker build -t docker-mcp-server .
docker run -d \
--name docker-mcp-server \
-p 8000:8000 \
-v /var/run/docker.sock:/var/run/docker.sock \
docker-mcp-server
๐ Project Structure
docker-mcp-server/
โโโ .dockerignore # Docker ignore file
โโโ .gitignore # Git ignore file
โโโ Dockerfile # Docker configuration
โโโ docker-compose.yml # Docker Compose configuration
โโโ LICENSE # MIT License
โโโ README.md # This file
โโโ chart/ # Helm chart for Kubernetes deployment
โ โโโ docker-mcp-server/
โ โโโ Chart.yaml # Chart metadata
โ โโโ values.yaml # Default configuration values
โ โโโ templates/ # Kubernetes templates
โ โโโ deployment.yaml
โ โโโ service.yaml
โ โโโ ingress.yaml
โ โโโ serviceaccount.yaml
โโโ docker_mcp_server.py # Main server code
โโโ requirements.txt # Python dependencies
๐ Kubernetes Deployment with Helm
Prerequisites
- Kubernetes cluster (Minikube, Docker Desktop, or cloud-based)
- Helm 3.x installed
- kubectl configured to communicate with your cluster
Installation
-
Add the chart repository (if published):
helm repo add docker-mcp-server https://your-chart-repo-url/ helm repo update
-
Install the chart (from local chart):
# From the project root directory helm install docker-mcp-server ./chart/docker-mcp-server -n docker-mcp --create-namespace
Configuration
The following table lists the configurable parameters of the Docker MCP Server chart and their default values.
Parameter | Description | Default |
---|---|---|
replicaCount | Number of replicas | 1 |
image.repository | Docker image repository | kalyanram262/mcp-test |
image.tag | Docker image tag | v3 |
image.pullPolicy | Image pull policy | IfNotPresent |
service.type | Kubernetes service type | ClusterIP |
service.port | Service port | 80 |
ingress.enabled | Enable ingress | false |
ingress.hosts[0].host | Ingress hostname | docker-mcp.local |
resources.limits.cpu | CPU limit | 500m |
resources.limits.memory | Memory limit | 512Mi |
resources.requests.cpu | CPU request | 100m |
resources.requests.memory | Memory request | 128Mi |
Upgrading
To upgrade your installation with the latest chart:
# From the project root directory
helm upgrade --install docker-mcp-server ./chart/docker-mcp-server -n docker-mcp
Uninstalling
To uninstall/delete the docker-mcp-server
release:
helm uninstall docker-mcp-server -n docker-mcp
kubectl delete namespace docker-mcp
Accessing the Service
Using Port-Forwarding
kubectl port-forward -n docker-mcp svc/docker-mcp-server 8080:80 &
curl http://localhost:8080/health
Using Minikube Service
minikube service docker-mcp-server -n docker-mcp
Using Ingress (if enabled)
- Make sure your
ingress.hosts[0].host
(default:docker-mcp.local
) resolves to your cluster's ingress IP - Access the service at
http://docker-mcp.local/health
๐ ๏ธ Development
Running Tests
docker-compose exec docker-mcp-server python -m pytest
Viewing Logs
docker-compose logs -f
Stopping Services
docker-compose down
๐ Example: Using the API
Starting an Nginx Container
curl -X POST http://localhost:8000/run_container \
-H "Content-Type: application/json" \
-d '{
"image": "nginx:alpine",
"ports": {"80/tcp": 8080},
"name": "my-nginx"
}'
Listing Containers
curl -X POST http://localhost:8000/list_containers
Viewing Container Logs
docker logs docker-mcp-server
Troubleshooting
-
If you get port conflicts, make sure no other instances are running:
# Find and kill any existing processes lsof -i :8000 kill <process_id>
-
For connection issues, check Claude Desktop's logs for detailed error messages
๐ Monitoring
The server includes a health check endpoint at /health
that can be used for monitoring. The Docker Compose configuration includes a health check that verifies this endpoint.
Health Check Configuration
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
interval: 10s
timeout: 5s
retries: 3
start_period: 10s
๐ ๏ธ Available Tools
Container Management
list_containers(all_containers: bool = False)
: List all containerscreate_container(image: str, command: str = None, name: str = None, ports: dict = None, environment: dict = None, volumes: dict = None)
: Create a new containerrun_container(image: str, command: str = None, name: str = None, ports: dict = None, environment: dict = None, volumes: dict = None)
: Create and start a containerstart_container(container_id: str)
: Start a stopped containerstop_container(container_id: str)
: Stop a running containerremove_container(container_id: str, force: bool = False)
: Remove a containerinspect_container(container_id: str)
: Get detailed information about a containerget_container_stats(container_id: str, stream: bool = False)
: Get real-time container statistics (CPU, memory, network, disk I/O)
Image Management
build_image(path: str, tag: str = None, dockerfile: str = None, build_args: dict = None, labels: dict = None, pull: bool = False, no_cache: bool = False, rm: bool = True, timeout: int = 3600)
: Build a Docker image from a Dockerfilelist_images()
: List all local imagespull_image(repository: str, tag: str = "latest")
: Pull an image from a registrytag_image(image_reference: str, repository: str, tag: str = "latest")
: Tag a local imagepush_image(repository: str, tag: str = "latest", auth_config: dict = None)
: Push an image to a registry
Network Management
list_networks()
: List all Docker networks
Volume Management
list_volumes()
: List all Docker volumes
The server will start on http://0.0.0.0:8000
.
Available Endpoints
Containers
POST /list_containers
- List all containersPOST /create_container
- Create a new containerPOST /run_container
- Create and start a containerPOST /start_container
- Start a stopped containerPOST /stop_container
- Stop a running containerPOST /remove_container
- Remove a containerPOST /inspect_container
- Get detailed container informationPOST /get_container_stats
- Get container statistics (CPU, memory, etc.)
Images
POST /build_image
- Build an image from a DockerfilePOST /list_images
- List all local imagesPOST /pull_image
- Pull an image from a registryPOST /tag_image
- Tag a local imagePOST /push_image
- Push an image to a registry
Networks
POST /list_networks
- List all Docker networks
Volumes
POST /list_volumes
- List all Docker volumes
๐ Example Usage
Using Python Client
Container Operations
# Get detailed container information
container_info = await client.inspect_container("my-container")
print(f"Container IP: {container_info['NetworkSettings']['IPAddress']}")
# Get container statistics
stats = await client.get_container_stats("my-container")
print(f"CPU Usage: {stats['cpu_stats']['cpu_usage']['total_usage']}")
# Stream container stats in real-time
stats_stream = await client.get_container_stats("my-container", stream=True)
print(f"Initial CPU: {stats_stream['first_stats']['cpu_stats']['cpu_usage']['total_usage']}")
for stat in stats_stream['stream']:
print(f"CPU: {stat['cpu_stats']['cpu_usage']['total_usage']}")
Image Operations
# Build an image
build_result = await client.build_image(
path="./my-app",
tag="myapp:1.0",
dockerfile="Dockerfile.prod",
build_args={"NODE_ENV": "production"},
labels={"version": "1.0"}
)
print(f"Built image: {build_result['tags']}")
# Tag an image
tag_result = await client.tag_image(
image_reference="myapp:1.0",
repository="myregistry.example.com/team/myapp",
tag="production"
)
print(f"Tagged as: {tag_result['new_reference']}")
# Push an image (using Docker's credential store)
push_result = await client.push_image(
repository="myregistry.example.com/team/myapp",
tag="production"
)
print(f"Push logs: {push_result['logs']}")
Using cURL
Get container details
curl -X POST http://localhost:8000/inspect_container \
-H "Content-Type: application/json" \
-d '{"container_id": "my-container"}'
Build and Push Images
# Build an image
curl -X POST http://localhost:8000/build_image \
-H "Content-Type: application/json" \
-d '{
"path": "/path/to/your/app",
"tag": "myapp:1.0",
"dockerfile": "Dockerfile.prod",
"build_args": {"NODE_ENV": "production"}
}'
# Tag an image
curl -X POST http://localhost:8000/tag_image \
-H "Content-Type: application/json" \
-d '{
"image_reference": "myapp:1.0",
"repository": "myregistry.example.com/team/myapp",
"tag": "production"
}'
# Push an image
curl -X POST http://localhost:8000/push_image \
-H "Content-Type: application/json" \
-d '{
"repository": "myregistry.example.com/team/myapp",
"tag": "production"
}'
Get container statistics
# Single stats snapshot
curl -X POST http://localhost:8000/get_container_stats \
-H "Content-Type: application/json" \
-d '{"container_id": "my-container"}'
## ๐งช Testing
### Running Tests
To run tests inside the container:
```bash
docker-compose exec docker-mcp-server python -m pytest
Health Check
The service includes a health check endpoint:
curl http://localhost:8000/health
Monitoring
You can view the container logs with:
docker-compose logs -f
๐ ๏ธ Development
Rebuilding the Container
After making changes to the code or dependencies:
docker-compose up -d --build
Accessing the Container Shell
docker-compose exec docker-mcp-server /bin/bash
๐ Deployment
Production Considerations
-
Security:
- Use TLS for MCP communication
- Implement proper authentication
- Limit Docker socket access
- Use secrets for sensitive data
-
Scaling:
- The service is stateless and can be scaled horizontally
- Use a reverse proxy (like Nginx) for load balancing
-
Monitoring:
- Set up logging and monitoring
- Configure alerts for the health check
Testing
To test the server, you can use the included test script:
python -m pytest test_docker_mcp.py -v
License
MIT