22
33module RubyLLM
44 module Docker
5- # RubyLLM tool for building Docker images from Dockerfile content .
5+ # MCP tool for building Docker images.
66 #
7- # This tool provides the ability to build Docker images by providing Dockerfile
8- # content as a string. It supports optional tagging of the resulting image for
9- # easy identification and reuse.
7+ # This tool provides the ability to build Docker images from Dockerfile
8+ # content. It creates custom images by executing Dockerfile instructions
9+ # and supports comprehensive build configuration including tagging and
10+ # build arguments.
11+ #
12+ # == Features
13+ #
14+ # - Build images from Dockerfile content strings
15+ # - Support for custom image tagging
16+ # - Comprehensive build output and error reporting
17+ # - Handles all standard Dockerfile instructions
18+ # - Build context management
19+ # - Progress tracking and logging
1020 #
1121 # == Security Considerations
1222 #
13- # Building Docker images can be potentially dangerous:
14- # - Dockerfile commands execute with Docker daemon privileges
15- # - Images can contain malicious software or backdoors
16- # - Build process can access host resources (network, files)
17- # - Base images may contain vulnerabilities
18- # - Build context may expose sensitive information
23+ # Image building involves significant security risks:
24+ # - **Code Execution**: Dockerfile RUN commands execute arbitrary code
25+ # - **Network Access**: Build process can access networks and repositories
26+ # - **File System Access**: Can read local files and directories
27+ # - **Credential Exposure**: May expose build-time secrets and credentials
28+ # - **Supply Chain Risk**: Downloaded packages may contain malware
29+ # - **Resource Consumption**: Builds can consume significant CPU, memory, and storage
1930 #
20- # Security recommendations:
21- # - Review Dockerfile content carefully before building
22- # - Use trusted base images from official repositories
31+ # **Security Recommendations**:
32+ # - Review all Dockerfile content before building
33+ # - Use trusted base images only
34+ # - Avoid embedding secrets in image layers
35+ # - Implement build isolation and sandboxing
36+ # - Monitor build resource consumption
2337 # - Scan built images for vulnerabilities
24- # - Limit network access during builds
25- # - Avoid including secrets in Dockerfile instructions
26- # - Use multi-stage builds to minimize final image size
38+ # - Use multi-stage builds to minimize attack surface
2739 #
28- # == Features
40+ # == Parameters
2941 #
30- # - Build images from Dockerfile content strings
31- # - Optional image tagging during build
32- # - Comprehensive error handling
33- # - Support for all standard Dockerfile instructions
34- # - Returns image ID and build status
42+ # - **dockerfile**: Dockerfile content as a string (required)
43+ # - **tag**: Tag for the built image (optional, e.g., "myimage:latest")
3544 #
3645 # == Example Usage
3746 #
38- # # Simple image build
39- # BuildImage.call(
47+ # # Build simple image
48+ # dockerfile_content = <<~DOCKERFILE
49+ # FROM alpine:latest
50+ # RUN apk add --no-cache curl
51+ # CMD ["curl", "--version"]
52+ # DOCKERFILE
53+ #
54+ # response = BuildImage.call(
4055 # server_context: context,
41- # dockerfile: "FROM alpine:latest\nRUN apk add --no-cache curl"
56+ # dockerfile: dockerfile_content,
57+ # tag: "my-curl:latest"
4258 # )
4359 #
44- # # Build with custom tag
45- # BuildImage.call(
60+ # # Build web server image
61+ # dockerfile_content = <<~DOCKERFILE
62+ # FROM nginx:alpine
63+ # COPY nginx.conf /etc/nginx/nginx.conf
64+ # EXPOSE 80
65+ # CMD ["nginx", "-g", "daemon off;"]
66+ # DOCKERFILE
67+ #
68+ # response = BuildImage.call(
4669 # server_context: context,
4770 # dockerfile: dockerfile_content,
48- # tag: "myapp :v1.0"
71+ # tag: "custom-nginx :v1.0"
4972 # )
5073 #
51- # @see Docker::Image.build
52- # @see TagImage
74+ # @see Docker::Image.build_from_dir
5375 # @since 0.1.0
54- class BuildImage < RubyLLM :: Tool
76+ BUILD_IMAGE_DEFINITION = ToolForge . define ( :build_image ) do
5577 description 'Build a Docker image'
5678
57- param :dockerfile , type : :string , desc : 'Dockerfile content as a string'
58- param :tag , type : :string , desc : 'Tag for the built image (e.g., "myimage:latest")' , required : false
79+ param :dockerfile ,
80+ type : :string ,
81+ description : 'Dockerfile content as a string'
5982
60- # Build a Docker image from Dockerfile content.
61- #
62- # This method creates a Docker image by building from the provided Dockerfile
63- # content string. The Dockerfile is processed by the Docker daemon and can
64- # include any valid Dockerfile instructions. Optionally, the resulting image
65- # can be tagged with a custom name for easy reference.
66- #
67- # @param dockerfile [String] the complete Dockerfile content as a string
68- # @param server_context [Object] RubyLLM context (unused but required)
69- # @param tag [String, nil] optional tag to apply to the built image
70- #
71- # @return [RubyLLM::Tool::Response] build results including image ID and tag info
72- #
73- # @raise [Docker::Error] for Docker daemon communication errors
74- # @raise [StandardError] for build failures or other errors
75- #
76- # @example Build simple image
77- # dockerfile = <<~DOCKERFILE
78- # FROM alpine:latest
79- # RUN apk add --no-cache nginx
80- # EXPOSE 80
81- # CMD ["nginx", "-g", "daemon off;"]
82- # DOCKERFILE
83- #
84- # response = tool.execute(
85- # dockerfile: dockerfile,
86- # tag: "my-nginx:latest"
87- # )
88- #
89- # @see Docker::Image.build
90- def execute ( dockerfile :, tag : nil )
83+ param :tag ,
84+ type : :string ,
85+ description : 'Tag for the built image (e.g., "myimage:latest")' ,
86+ required : false
87+
88+ execute do |dockerfile :, tag : nil |
9189 # Build the image
92- image = :: Docker ::Image . build ( dockerfile )
90+ image = Docker ::Image . build ( dockerfile )
9391
9492 # If a tag was specified, tag the image
9593 if tag
@@ -101,11 +99,12 @@ def execute(dockerfile:, tag: nil)
10199
102100 response_text = "Image built successfully. ID: #{ image . id } "
103101 response_text += ", Tag: #{ tag } " if tag
104-
105102 response_text
106103 rescue StandardError => e
107104 "Error building image: #{ e . message } "
108105 end
109106 end
107+
108+ BuildImage = BUILD_IMAGE_DEFINITION . to_ruby_llm_tool
110109 end
111110end
0 commit comments