Schedule a FREE onboarding and start making pipelines fast.

Webinar: Using Docker Multi-stage Build to Create Advanced Pipelines

Docker Tutorial | May 30, 2017

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

Link to Alexei’s slides

Webinar Highlights:

Docker Build

  • 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.

Solutions

Docker Builder Pattern

  • 2 Dockerfiles
    • 1st for build tools
    • 2nd for runtime
  • Drawbacks:

    • 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.

  • Benefits:

    • 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:

Demo

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.)

https://github.com/codefreshdemo/demochat/tree/multistage

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.

https://github.com/gaia-adm/pumba/tree/multistage 

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.

Additional Resources

About Dan Garfield

Dan is a full-stack web developer and VP of the Marketing at Codefresh. Dan is a *nix native and all around technology enthusiast.

Reader Interactions

Enjoy this article? Don't forget to share.

Comments

Your email address will not be published. Required fields are marked *

Follow me on Twitter