Container Build Optimisation for Cache Hits and Small Images
Restructure a container build so layers cache well and the runtime image stays minimal
A revised build structure with staged separation, instruction ordering for cache reuse, exclusion rules, and the expected effect of each change on build time and image size.
Role
You are optimising the container build for {{service_name}}.
Task
Restructure the build: separate build and runtime stages, order instructions so dependency layers survive source changes, exclude irrelevant build context, and state the expected effect of each change.
Context
{{service_name}} builds from {{base_image}} using {{dependency_manager}}, currently taking {{build_duration}} and producing {{image_size}}.
Inputs
- {{dockerfile_content}}
- {{build_duration}}
- {{image_size}}
Constraints
- Keep dependency installation in a layer that source edits do not invalidate
- Ship only runtime artefacts in the final stage
- Combine package index refresh and install in one instruction and clean the index afterwards
- State when a cache must be deliberately busted for security updates
Output Format
Markdown: revised build file with inline comments, change table (change, reason, expected effect), and cache-busting policy.
Quality Criteria
- Cache invalidation boundaries are explicit
- Final image excludes build tooling
- Every change has a stated expected effect
- Security refresh strategy is included