Reducing Docker image size with multi-stage builds
There's been some posts appearing as of lately on medium.com and other blogs extolling the virtues of multi-stage builds in Docker, a feature that was not available as of 3 years ago (a long time in development).
So since I spend a lot of time rebuilding my own images, I decided to give this staged build methodology a try. I'm happy to report that the results are stunning:
docker images REPOSITORY TAG IMAGE ID CREATED SIZE alexanderallen/ghost latest 27c381f1a7c7 3 minutes ago 322MB alexanderallen/ghost stable 0720120629a0 3 days ago 809MB
My Ghost Docker image went from 809MB to a paltry 322MB !
As an added side-effect, now instead of having to jump between a build and a runtime image, a common image build pattern, everything is in a single
Dockerfile. But the Dockerfile is also larger. I do find it easier to debug rather than having two editor windows open for each build file.