docker_rm
Remove Docker containers by name or ID, with options to force removal of running containers and delete associated anonymous volumes.
Instructions
Remove one or more containers
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| containers | Yes | Container name(s) or ID(s) to remove | |
| force | No | Force removal of running containers | |
| volumes | No | Remove anonymous volumes | |
| cwd | No | Working directory |
Implementation Reference
- src/tools/docker.ts:347-353 (handler)The core handler function for the 'docker_rm' tool. It processes input arguments, constructs the 'docker rm' command with optional flags, and executes it via the shared executeDockerCommand helper.export async function dockerRm(args: z.infer<typeof dockerRmSchema>): Promise<ToolResponse> { const containers = Array.isArray(args.containers) ? args.containers.join(' ') : args.containers; const forceFlag = args.force ? '-f' : ''; const volumesFlag = args.volumes ? '-v' : ''; return executeDockerCommand(`docker rm ${forceFlag} ${volumesFlag} ${containers}`.trim(), args.cwd); }
- src/tools/docker.ts:191-196 (schema)Zod schema used for input validation of the 'docker_rm' tool arguments.export const dockerRmSchema = z.object({ containers: z.union([z.string(), z.array(z.string())]).describe('Container name(s) or ID(s) to remove'), force: z.boolean().optional().default(false).describe('Force removal of running containers'), volumes: z.boolean().optional().default(false).describe('Remove anonymous volumes'), cwd: z.string().optional().describe('Working directory') });
- src/tools/docker.ts:578-596 (registration)MCP tool definition/registration for 'docker_rm' in the exported dockerTools array, providing the JSON schema for tool listing.name: 'docker_rm', description: 'Remove one or more containers', inputSchema: { type: 'object', properties: { containers: { oneOf: [ { type: 'string' }, { type: 'array', items: { type: 'string' } } ], description: 'Container name(s) or ID(s) to remove' }, force: { type: 'boolean', default: false, description: 'Force removal of running containers' }, volumes: { type: 'boolean', default: false, description: 'Remove anonymous volumes' }, cwd: { type: 'string', description: 'Working directory' } }, required: ['containers'] } },
- src/index.ts:499-502 (registration)Tool dispatch/registration in the main MCP server request handler for CallToolRequestSchema.if (name === 'docker_rm') { const validated = dockerRmSchema.parse(args); return await dockerRm(validated); }
- src/tools/docker.ts:21-62 (helper)Shared helper function that executes all Docker commands and formats ToolResponse, used by dockerRm.async function executeDockerCommand(command: string, cwd?: string): Promise<ToolResponse> { try { const { stdout, stderr } = await execAsync(command, { cwd: cwd || process.cwd(), shell: '/bin/bash', maxBuffer: 10 * 1024 * 1024, // 10MB buffer for logs timeout: 60000 // 60 second timeout for builds }); return { content: [ { type: "text" as const, text: JSON.stringify({ success: true, command: command, stdout: stdout.trim(), stderr: stderr.trim(), cwd: cwd || process.cwd() }, null, 2) } ] }; } catch (error: any) { return { content: [ { type: "text" as const, text: JSON.stringify({ success: false, command: command, stdout: error.stdout?.trim() || '', stderr: error.stderr?.trim() || error.message, exitCode: error.code || 1, cwd: cwd || process.cwd() }, null, 2) } ], isError: true }; } }