One of the biggest announcements at DockerCon 2017 was the launch of Multi-stage Builds. It’s available in Docker version 17.05 onwards and is one of most exciting features to come out. In our last webinar, we demonstrated how to use the new Docker Multi-stage Build feature to create advanced pipelines using a single Dockerfile.
View the webinar on-demand to learn:
- What’s need for a Docker build pipeline
- Existing solutions
- New Dockerfile syntax for Multi-stage Builds
- Demo: How to use the Multi-stage Build feature
- Dockerfile – imperative DSL that defines build commands
- Each docker build command generates ONE image layer
- Complete docker build execution generates ONE Docker image
Problem with Docker Build
When we create a new image we want to include the basic runtime, some configuration, and the application itself. However, we often build our image with additional tools/ packages, such as compilers, debuggers, test frameworks, profilers, build artifacts, etc. We end up with an image that’s 4-10x bigger than we need to run our application.
Docker Builder Pattern
- 2 Dockerfiles
- 1st for build tools
- 2nd for runtime
- Need 2+ Dockerfiles
- Orchestration needed (need to learn additional tools like BASH, Make, YAML and it’s not always easy to reproduce on local machine)
Docker Version 17.05
Docker introduced multi-stage builds in their latest release. It allows you to build smaller images, and separate build into stages. For example, in the first stage you can create the complete build environment and in the second stage, create the minimal runtime environment using a single Dockerfile.
- One Dockerfile
- One syntax to learn
- Same build
- Works on Local machine and CI server
- Can create multiple stages
Here is an example to illustrate how this is done:
Node.js Multistage Build
(See demo – 14 minutes 20 seconds of the webinar)
In the demo, Alexei showed how to create a lean Node.js image using the following demo chat application. (You’ll need to get Docker v17.05 to try Docker multi-stage build.)
Go Multistage Build
(See demo – 22 minutes 20 seconds of the webinar)
In another example, Alexei showed how to create a Go Multi-stage build using the following open-source project called Pumba.
Use Multi-stage builds Codefresh
Currently, Codefresh is the only CI that supports Docker Multi-stage builds. You can try it for free using your private or public repo. For more information on how to use this feature, check out this blog post.
- Check out Alexei’s blog post – Create lean Node.js image with Docker multi-stage build
- Join our next in-person or online event- https://codefresh.io/meetups/
- Request a one-on-one onboarding session with our support engineers to configure your automated pipeline in Codefresh.
- Email us with questions, feedback, or webinar topic requests at firstname.lastname@example.org