Automating Dockerfile Refactoring to Multi-stage Builds
Dongjin Chen, Wenhua Yang, Minxue Pan, Yu Zhou
Abstract
Containerization has become a cornerstone of modern software deployment, yet many projects still ship single-stage Dockerfiles that bundle compilers, build tools, and temporary files into production images, thereby hurting performance and security. Multi-stage builds are recommended, yet uptake appears uneven, plausibly because refactoring legacy Dockerfiles demands nontrivial reasoning about build lifecycles and dependency separation. This paper presents StageCraft, an automated refactoring approach that converts single-stage Dockerfiles into optimized multi-stage builds. StageCraft first performs static analysis to identify the technology stack and to infer build-time and runtime dependencies. It then applies a lightweight gate that estimates the refactoring benefit from a composite of image bloat, structural inefficiency, and security risk, proceeding only when refactoring is warranted. Finally, it synthesizes a multi-stage Dockerfile that isolates build tooling, copies only the artifacts needed at runtime, and applies production hardening. Evaluated on 521 real-world single-stage projects, StageCraft successfully produced working multi-stage Dockerfiles for 60.3% of targets. The resulting images were, on average, 52.2% smaller and contained 50.0% fewer high-risk vulnerabilities than the originals, outperforming baselines. StageCraft lowers the barrier to multi-stage adoption at scale, yielding leaner images with a reduced attack surface and improved maintainability. We release the tool, its knowledge assets, and the evaluation dataset to support reproducibility and future research.
Ask about this paper
Ask your agent about it.
Lune has read the top-tier papers around this one, so every answer names the papers it rests on.
Your agent calls
Lunesearch_papers
Free to start. No credit card required.
Terminal
Install the CLIlune papers get 60018033-e4b9-4cbd-aee2-b5f12c5faa20Related papers
- Refactorings and Technical Debt in Docker Projects: An Empirical StudyEmna Ksontini, Marouane Kessentini, Thiago do Nascimento Ferreira, Foyzul HassanASE 2021 · 17 citations
- Shipwright: A Human-in-the-Loop System for Dockerfile RepairJordan Henkel, Denini Silva, Leopoldo Teixeira, Marcelo d'Amorim et al.ICSE 2021 · 31 citations
- Dr. Docker: A Large-Scale Security Measurement of Docker Image EcosystemHequan Shi, Lingyun Ying, Libo Chen, Haixin Duan et al.WWW 2025 · 1 citation
- Empirical Study of the Docker Smells Impact on the Image SizeThomas DurieuxICSE 2024 · 11 citations
- Automatic Dockerfile Generation with Large Language ModelsJun Lyu, He Zhang, Yusong Yuan, Lanxin Yang et al.ICSE 2026
