run_container
Run Docker containers with specified configurations, including image, name, ports, volumes, environment variables, and commands. Integrates with the Docker MCP Server to manage containers efficiently.
Instructions
Run a Docker container
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| command | No | Command to run in the container | |
| detach | No | Run container in background | |
| env | No | Environment variables (e.g. ["KEY=value"]) | |
| image | Yes | Docker image to run | |
| name | No | Name for the container | |
| ports | No | Port mappings (e.g. ["8080:80"]) | |
| volumes | No | Volume mappings (e.g. ["/host/path:/container/path"]) |
Input Schema (JSON Schema)
{
"properties": {
"command": {
"description": "Command to run in the container",
"type": "string"
},
"detach": {
"description": "Run container in background",
"type": "boolean"
},
"env": {
"description": "Environment variables (e.g. [\"KEY=value\"])",
"items": {
"type": "string"
},
"type": "array"
},
"image": {
"description": "Docker image to run",
"type": "string"
},
"name": {
"description": "Name for the container",
"type": "string"
},
"ports": {
"description": "Port mappings (e.g. [\"8080:80\"])",
"items": {
"type": "string"
},
"type": "array"
},
"volumes": {
"description": "Volume mappings (e.g. [\"/host/path:/container/path\"])",
"items": {
"type": "string"
},
"type": "array"
}
},
"required": [
"image"
],
"type": "object"
}
Implementation Reference
- src/index.ts:258-310 (handler)The handler function that constructs and executes the 'docker run' command based on the provided arguments, handling options like detach, ports, volumes, env, and command.private async runContainer(args: RunContainerArgs) { if (!args.image) { throw new McpError( ErrorCode.InvalidParams, 'Image parameter is required' ); } let command = 'docker run'; if (args.detach) { command += ' -d'; } if (args.name) { command += ` --name ${args.name}`; } if (args.ports && Array.isArray(args.ports)) { args.ports.forEach((port: string) => { command += ` -p ${port}`; }); } if (args.volumes && Array.isArray(args.volumes)) { args.volumes.forEach((volume: string) => { command += ` -v ${volume}`; }); } if (args.env && Array.isArray(args.env)) { args.env.forEach((env: string) => { command += ` -e ${env}`; }); } command += ` ${args.image}`; if (args.command) { command += ` ${args.command}`; } const { stdout } = await execAsync(command); return { content: [ { type: 'text', text: stdout.trim(), }, ], }; }
- src/index.ts:90-136 (registration)Tool registration in the ListTools response, including name, description, and detailed inputSchema matching the handler arguments.{ name: 'run_container', description: 'Run a Docker container', inputSchema: { type: 'object', properties: { image: { type: 'string', description: 'Docker image to run', }, name: { type: 'string', description: 'Name for the container', }, detach: { type: 'boolean', description: 'Run container in background', }, ports: { type: 'array', items: { type: 'string', }, description: 'Port mappings (e.g. ["8080:80"])', }, volumes: { type: 'array', items: { type: 'string', }, description: 'Volume mappings (e.g. ["/host/path:/container/path"])', }, env: { type: 'array', items: { type: 'string', }, description: 'Environment variables (e.g. ["KEY=value"])', }, command: { type: 'string', description: 'Command to run in the container', }, }, required: ['image'], }, },
- src/index.ts:21-29 (schema)TypeScript interface defining the structure of arguments for the run_container tool, used in the handler signature.interface RunContainerArgs { image: string; name?: string; detach?: boolean; ports?: string[]; volumes?: string[]; env?: string[]; command?: string; }
- src/index.ts:193-194 (registration)Dispatch case in the CallToolRequest handler that routes requests for 'run_container' to the runContainer method.case 'run_container': return await this.runContainer(request.params.arguments as unknown as RunContainerArgs);