Abstract
Docker’s layer caching is a critical feature for accelerating container builds, yet its effectiveness is frequently hampered by suboptimal instruction ordering. Developers often place volatile instructions near the top of a Dockerfile, causing expensive, stable layers to be rebuilt unnecessarily. This paper introduces Doctor, a tool that automatically re-orchestrates Dockerfiles by predicting instruction modification frequencies from Git history and measuring execution costs. Using a weighted topological sorting algorithm, Doctor pushes high-variance instructions toward the end of the build sequence. Our evaluation across 2,000 GitHub repositories demonstrates an average rebuild speedup of 26.5% while maintaining functional integrity in over 86% of cases.
Introduction
In modern CI/CD pipelines, Docker images are rebuilt hundreds of times. Docker optimizes this process using layer caching: if an instruction remains unchanged, its cached result is reused. However, if an instruction changes, Docker must rebuild that layer and every subsequent layer. Human developers often prioritize semantic grouping or place frequently modified commands (like COPY . .) early in the file, inadvertently "busting" the cache for heavy dependencies that follow.
As shown in the figure above, reordering instructions to place stable, time-consuming layers before volatile ones can significantly increase cache reuse during subsequent rebuilds.
System Design: The Doctor Pipeline
Doctor employs a four-step automated pipeline to transform a raw Dockerfile into an optimized version.
- Dependency Extraction: To maintain container functionality, Doctor identifies dependencies between instructions (e.g., a
RUNcommand requiring a directory created byWORKDIR). It uses a custom EBNF grammar to parse shell scripts and track variables, paths, and execution contexts. - Modification Frequency Evaluation: By analyzing up to 30 months of Git history, Doctor calculates the probability of future modifications for each instruction using TF-IDF and cosine similarity.
- Build Time Collection: The tool performs a clean build to capture the precise execution cost of each instruction.
- Instruction Reconstruction: A weighted topological sorting algorithm generates a new sequence that respects functional dependencies while minimizing the expected cost of future rebuilds by moving high-probability-of-change instructions to the bottom.
Evaluation and Key Results
The researchers evaluated Doctor on a diverse dataset of 2,000 popular GitHub repositories. The tool successfully optimized 92.75% of the Dockerfiles.
Performance Gains
On average, rebuild times were reduced by 26.5%. In over 12% of cases, rebuild times were cut by more than half. The optimization process itself is efficient, requiring approximately 77 seconds on average, primarily for the initial timing build.
Functional Integrity
A critical concern in automated re-orchestration is breaking the container. Doctor maintained functional integrity (bit-for-bit identical file systems) in 86.2% of cases. Manual inspection revealed semantic differences or bugs in only 0.21% of optimized images.
Predictive Accuracy
The study found that RUN and COPY instructions—the most frequent causes of cache invalidation—could be predicted with over 92% accuracy based on historical commit data. Interestingly, the researchers identified a "sweet spot" for historical analysis: looking back 30 months provided optimal results, whereas shorter windows (under 8 months) yielded negligible gains.
Discussion and Conclusion
While Doctor significantly improves build efficiency, it introduces a trade-off regarding readability. Optimized Dockerfiles may appear "scrambled" to developers who prefer semantic grouping. The study noted that enforcing human-friendly grouping reduced efficiency gains from 26.3% to 18.4%.
Future work includes bridging this readability gap through semantic constraints and extending the tool to support "on-the-fly" optimization for new projects lacking extensive commit histories. By treating Dockerfile orchestration as a data-driven optimization problem, Doctor provides a robust path toward more efficient containerized development lifecycles.
Summary updated