Manage Docker Images
manage_imagesPerform Docker image operations including listing available images, pulling from registries, removing unused images, and building new images from Dockerfiles.
Instructions
Manage Docker images (list, pull, remove, build)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | Action to perform on images | |
| image | No | Image name or ID (required for pull, remove, build) | |
| tag | No | Tag for the image (optional for pull, required for build) | |
| dockerfile | No | Path to Dockerfile (required for build) |
Implementation Reference
- src/index.ts:1272-1317 (handler)The handler function for the 'manage_images' MCP tool. It takes input parameters (action, image, tag, dockerfile) and executes corresponding Docker commands: list images, pull image, remove image, or build image from Dockerfile or context.
async ({ action, image, tag, dockerfile }) => { try { let command: string; switch (action) { case "list": command = "docker images"; break; case "pull": if (!image) throw new Error("Image name is required for pull action"); command = tag ? `docker pull ${image}:${tag}` : `docker pull ${image}`; break; case "remove": if (!image) throw new Error("Image name or ID is required for remove action"); command = `docker rmi ${image}`; break; case "build": if (!image) throw new Error("Image name is required for build action"); const buildTag = tag ? `${image}:${tag}` : image; const context = dockerfile ? dockerfile : "."; command = `docker build -t ${buildTag} ${context}`; break; } const result = await executeDockerCommand(command); return { content: [ { type: "text", text: `Image ${action} completed:\n\n${result.stdout}${result.stderr ? `\nWarnings:\n${result.stderr}` : ""}` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error managing images: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } } - src/index.ts:1262-1271 (schema)The input schema definition for the 'manage_images' tool using Zod validation. Defines parameters: action (enum: list/pull/remove/build), image (string optional), tag (string optional), dockerfile (string optional).
{ title: "Manage Docker Images", description: "Manage Docker images (list, pull, remove, build)", inputSchema: { action: z.enum(["list", "pull", "remove", "build"]).describe("Action to perform on images"), image: z.string().optional().describe("Image name or ID (required for pull, remove, build)"), tag: z.string().optional().describe("Tag for the image (optional for pull, required for build)"), dockerfile: z.string().optional().describe("Path to Dockerfile (required for build)") } }, - src/index.ts:1260-1318 (registration)The registration of the 'manage_images' tool on the MCP server using server.registerTool, including title, description, inputSchema, and handler function.
server.registerTool( "manage_images", { title: "Manage Docker Images", description: "Manage Docker images (list, pull, remove, build)", inputSchema: { action: z.enum(["list", "pull", "remove", "build"]).describe("Action to perform on images"), image: z.string().optional().describe("Image name or ID (required for pull, remove, build)"), tag: z.string().optional().describe("Tag for the image (optional for pull, required for build)"), dockerfile: z.string().optional().describe("Path to Dockerfile (required for build)") } }, async ({ action, image, tag, dockerfile }) => { try { let command: string; switch (action) { case "list": command = "docker images"; break; case "pull": if (!image) throw new Error("Image name is required for pull action"); command = tag ? `docker pull ${image}:${tag}` : `docker pull ${image}`; break; case "remove": if (!image) throw new Error("Image name or ID is required for remove action"); command = `docker rmi ${image}`; break; case "build": if (!image) throw new Error("Image name is required for build action"); const buildTag = tag ? `${image}:${tag}` : image; const context = dockerfile ? dockerfile : "."; command = `docker build -t ${buildTag} ${context}`; break; } const result = await executeDockerCommand(command); return { content: [ { type: "text", text: `Image ${action} completed:\n\n${result.stdout}${result.stderr ? `\nWarnings:\n${result.stderr}` : ""}` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error managing images: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } } );