Manage Docker Containers
manage_containersPerform actions on Docker containers including listing, starting, stopping, removing, and restarting them to manage container lifecycle.
Instructions
Manage Docker containers (list, start, stop, remove)
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"action": "<action>"
},
"name": "manage_containers"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | Action to perform on containers | |
| container | No | Container name or ID (required for start, stop, remove, restart) | |
| all | No | Include stopped containers when listing |
Implementation Reference
- src/index.ts:1209-1256 (handler)Handler function that takes action (list/start/stop/remove/restart), container name, and all flag, constructs the appropriate docker command, executes it via executeDockerCommand, and returns the result or error.
async ({ action, container, all }) => { try { let command: string; switch (action) { case "list": command = all ? "docker ps -a" : "docker ps"; break; case "start": if (!container) throw new Error("Container name or ID is required for start action"); command = `docker start ${container}`; break; case "stop": if (!container) throw new Error("Container name or ID is required for stop action"); command = `docker stop ${container}`; break; case "remove": if (!container) throw new Error("Container name or ID is required for remove action"); command = `docker rm ${container}`; break; case "restart": if (!container) throw new Error("Container name or ID is required for restart action"); command = `docker restart ${container}`; break; } const result = await executeDockerCommand(command); return { content: [ { type: "text", text: `Container ${action} completed:\n\n${result.stdout}${result.stderr ? `\nWarnings:\n${result.stderr}` : ""}` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error managing containers: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } } - src/index.ts:1203-1207 (schema)Zod input schema defining the parameters for the manage_containers tool: action enum, optional container string, optional all boolean.
inputSchema: { action: z.enum(["list", "start", "stop", "remove", "restart"]).describe("Action to perform on containers"), container: z.string().optional().describe("Container name or ID (required for start, stop, remove, restart)"), all: z.boolean().optional().describe("Include stopped containers when listing") } - src/index.ts:1198-1257 (registration)Registration of the manage_containers tool with McpServer using server.registerTool, including title, description, inputSchema, and handler function.
server.registerTool( "manage_containers", { title: "Manage Docker Containers", description: "Manage Docker containers (list, start, stop, remove)", inputSchema: { action: z.enum(["list", "start", "stop", "remove", "restart"]).describe("Action to perform on containers"), container: z.string().optional().describe("Container name or ID (required for start, stop, remove, restart)"), all: z.boolean().optional().describe("Include stopped containers when listing") } }, async ({ action, container, all }) => { try { let command: string; switch (action) { case "list": command = all ? "docker ps -a" : "docker ps"; break; case "start": if (!container) throw new Error("Container name or ID is required for start action"); command = `docker start ${container}`; break; case "stop": if (!container) throw new Error("Container name or ID is required for stop action"); command = `docker stop ${container}`; break; case "remove": if (!container) throw new Error("Container name or ID is required for remove action"); command = `docker rm ${container}`; break; case "restart": if (!container) throw new Error("Container name or ID is required for restart action"); command = `docker restart ${container}`; break; } const result = await executeDockerCommand(command); return { content: [ { type: "text", text: `Container ${action} completed:\n\n${result.stdout}${result.stderr ? `\nWarnings:\n${result.stderr}` : ""}` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error managing containers: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } } ); - src/index.ts:383-390 (helper)Helper function used by the handler to execute Docker commands asynchronously using child_process exec.
async function executeDockerCommand(command: string): Promise<{ stdout: string; stderr: string }> { try { const result = await execAsync(command); return result; } catch (error: any) { throw new Error(`Docker command failed: ${error.message}`); } }