MCP server Docker deployment
MCP server in Docker by creating a Dockerfile that installs Python and your MCP server code, then run it with docker build and docker run. Use the official mcp Python package and the stdio_server for standard I/O communication inside the container.PREREQUISITES
Docker installed on your machinePython 3.8+pip install mcpBasic knowledge of Dockerfile and container commands
Setup
Install Docker from https://docs.docker.com/get-docker/ and ensure Python 3.8+ is available locally for development. Install the mcp Python package with pip install mcp. Prepare your MCP server Python script that uses mcp.server.stdio.stdio_server to run the server over standard input/output.
pip install mcp Step by step
Create a Python script mcp_server.py that runs the MCP stdio server, then write a Dockerfile to containerize it. Build and run the Docker container to deploy the MCP server.
# mcp_server.py
from mcp.server.stdio import stdio_server
if __name__ == "__main__":
stdio_server()
---
# Dockerfile
FROM python:3.10-slim
WORKDIR /app
COPY mcp_server.py ./
RUN pip install mcp
CMD ["python", "mcp_server.py"]
---
# Build the Docker image
# Run this in the directory containing Dockerfile and mcp_server.py
docker build -t mcp-server .
# Run the container
docker run --rm -it mcp-server Sending build context to Docker daemon 3.072kB Step 1/6 : FROM python:3.10-slim ---> <image_id> Step 2/6 : WORKDIR /app ---> Using cache ---> <image_id> Step 3/6 : COPY mcp_server.py ./ ---> Using cache ---> <image_id> Step 4/6 : RUN pip install mcp ---> Running in <container_id> Successfully installed mcp-<version> Removing intermediate container <container_id> ---> <image_id> Step 5/6 : CMD ["python", "mcp_server.py"] ---> Using cache ---> <image_id> Step 6/6 : Successfully built <image_id> Successfully tagged mcp-server:latest # Running container starts MCP server and waits for stdio communication
Common variations
You can run the MCP server asynchronously by modifying the Python script to use async MCP server APIs if available. To expose the MCP server over TCP inside Docker, add a TCP server wrapper around the stdio server and expose ports in the Dockerfile and docker run command. Use different base images like python:3.11-alpine for smaller containers.
# Async MCP server example (if supported)
import asyncio
from mcp.server.stdio import stdio_server
async def main():
await stdio_server()
if __name__ == "__main__":
asyncio.run(main()) Troubleshooting
If the container exits immediately, check that mcp_server.py is correctly copied and the CMD is correct. Use docker logs <container_id> to inspect errors. If you see permission errors, ensure Docker has access to your working directory. For networking issues, verify port mappings and firewall rules.
Key Takeaways
- Use a minimal Python base image and install the official
mcppackage inside Docker. - Run the MCP server with
stdio_serverfor simple stdio-based communication inside containers. - Build and run your Docker container with standard Docker CLI commands for easy deployment.
- Modify the MCP server script for async or TCP communication as needed for your use case.
- Check Docker logs and container status for troubleshooting common deployment issues.