docker_build
Build a Docker image from a Dockerfile with configurable context, tag, build arguments, multi-stage target, cache usage, base image pulling, and platform.
Instructions
Build a Docker image from a Dockerfile
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| context | No | Build context path (default: current directory) | |
| dockerfile | No | Path to Dockerfile (relative to context) | |
| tag | No | Tag for the built image (e.g., myapp:latest) | |
| build_args | No | Build arguments as key-value pairs | |
| target | No | Target stage for multi-stage builds | |
| no_cache | No | Do not use cache when building | |
| pull | No | Always attempt to pull newer version of base image | |
| platform | No | Target platform (e.g., linux/amd64, linux/arm64) |
Implementation Reference
- src/services/DockerService.ts:310-366 (handler)Core handler function that builds a Docker image. Constructs a 'docker build' CLI command from arguments and executes it via executeDockerCommand.
async buildImage(args: DockerBuildArgs): Promise<ToolResult> { const { context = '.', dockerfile, tag, build_args, target, no_cache, pull, quiet, squash, platform, progress = 'auto', secret, ssh, network } = args; // Validate context exists const contextPath = this.workspaceService.resolvePath(context); try { await fs.access(contextPath); } catch { throw new Error(`Build context not found: ${contextPath}`); } let command = `docker build ${this.quotePath(contextPath)}`; if (dockerfile) command += ` -f ${this.quotePath(dockerfile)}`; if (tag) command += ` -t ${tag}`; if (target) command += ` --target ${target}`; if (no_cache) command += ' --no-cache'; if (pull) command += ' --pull'; if (quiet) command += ' --quiet'; if (squash) command += ' --squash'; if (platform) command += ` --platform ${platform}`; if (progress) command += ` --progress ${progress}`; if (network) command += ` --network ${network}`; if (build_args) { for (const [key, value] of Object.entries(build_args)) { command += ` --build-arg ${key}=${value}`; } } if (secret) { secret.forEach(s => command += ` --secret ${s}`); } if (ssh) command += ` --ssh ${ssh}`; try { return await this.executeDockerCommand(command, { cwd: this.getCurrentWorkspace() }, this.buildTimeout); } catch (error: any) { throw new Error(`Docker build failed: ${error.message}`); } } - src/services/DockerService.ts:12-27 (schema)TypeScript interface defining the input schema/arguments for the docker_build tool.
export interface DockerBuildArgs { context?: string; dockerfile?: string; tag?: string; build_args?: Record<string, string>; target?: string; no_cache?: boolean; pull?: boolean; quiet?: boolean; squash?: boolean; platform?: string; progress?: 'auto' | 'plain' | 'tty'; secret?: string[]; ssh?: string; network?: string; } - src/toolDefinitions.ts:420-436 (schema)MCP tool definition/registration schema for docker_build, specifying name, description, and input JSON schema.
{ name: 'docker_build', description: 'Build a Docker image from a Dockerfile', inputSchema: { type: 'object', properties: { context: { type: 'string', description: 'Build context path (default: current directory)' }, dockerfile: { type: 'string', description: 'Path to Dockerfile (relative to context)' }, tag: { type: 'string', description: 'Tag for the built image (e.g., myapp:latest)' }, build_args: { type: 'object', description: 'Build arguments as key-value pairs' }, target: { type: 'string', description: 'Target stage for multi-stage builds' }, no_cache: { type: 'boolean', description: 'Do not use cache when building' }, pull: { type: 'boolean', description: 'Always attempt to pull newer version of base image' }, platform: { type: 'string', description: 'Target platform (e.g., linux/amd64, linux/arm64)' }, }, }, }, - src/index.ts:205-206 (registration)Registration in the main server switch statement that routes 'docker_build' tool calls to DockerService.buildImage().
case 'docker_build': return await this.dockerService.buildImage(args as DockerBuildArgs); - Helper/focused wrapper method that delegates to buildImage with a subset of parameters.
async dockerImageBuild(args: { context?: string; dockerfile?: string; tag?: string; build_args?: Record<string, string>; no_cache?: boolean; }): Promise<ToolResult> { const buildArgs: DockerBuildArgs = { context: args.context, dockerfile: args.dockerfile, tag: args.tag, build_args: args.build_args, no_cache: args.no_cache }; return this.buildImage(buildArgs); }