Automating Dockerfile Refactoring to Multi-stage Builds
Open Access
- 30 June 2026
- journal article
- Published by Association for Computing Machinery (ACM) in Proceedings of the ACM on Software Engineering
- Vol. 3 (FSE) , 1560-1581
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.Keywords
Funding Information
- National Natural Science Foundation of China (62372232)
- National Natural Science Foundation of China (62372227)
This publication has 19 references indexed in Scilit:
- Together We are Better: LLM, IDE and Semantic Embedding to Assist Move Method Refactoring2025
- Refactoring for Dockerfile Quality: A Dive into Developer Practices and Automation Potential2025
- Exploring the potential of general purpose LLMs in automated software refactoring: an empirical studyAutomated Software Engineering, 2025
- Fixing Dockerfile smells: an empirical studyEmpirical Software Engineering, 2024
- Revisiting Dockerfiles in Open Source Software Over Time2021
- Shipwright: A Human-in-the-Loop System for Dockerfile Repair2021
- Dockerfile Changes in Practice: A Large-Scale Empirical Study of 4,110 Projects on GitHub2020
- An Insight Into the Impact of Dockerfile Evolutionary Trajectories on Quality and Latency2018
- Cimplifier: automatically debloating containers2017
- A Study of Security Vulnerabilities on Docker Hub2017