Docker multi-stage build
Using Docker multi-stage build
to create advanced build pipelines
About me…
★Chief of Research
@codefresh.io
★Open Source:
#pumba #microci
★Technology Stack:
#docker #golang #k8s
★Medium & Twitter
@alexeiled
Docker Images and Layers
Docker build
• Dockerfile - imperative DSL that defines build
commands
• Each docker build command generates
SINGLE image layer
• Complete docker build execution generates
SINGLE Docker image
janloeffler/html-demo Image Layers
nginx Dockerfile
html-demo Dockerfile
Problem with Docker Build*
application
configuration
runtime
Image we want
application
configuration
runtime
code, build and test logs, …
linters, tests, profilers, …
compilers, debuggers, …
Image we build
x (4..10)
* before Docker 17.05
Docker Builder Pattern
• 2 Dockerfiles
• 1st for build tools
• 2nd for runtime
• Drawbacks
• 2+ Dockerfiles
• Orchestration needed: Bash, make, YAML, …
Docker multi-stage build
✓ Benefits
✓ Single Dockerfile
✓ One syntax to learn
✓ Same Build
✓ Local and CI
✓ Create multiple stages
curl
FROM: alpine:3.5
/hello
FROM: ubuntu
/world
FROM: debian
/hello
FROM: base
/world
Node.js multi-stage build
https://github.com/codefreshdemo/demochat/tree/multistage
Builder (Alpine) Runtime (Alpine)
Content
Node.js
App Modules
Yarn (package)
Mocha (testing)
Eslint (linter)
Newman (REST testing)
Gulp (automation)
nsp (security scanner)
Istanbul (coverage)
Python
gcc, g++, krb5-dev, …
Node.js
App Modules
Yarn (package)
Size 568 MB 112 MB
Go multi-stage build
https://github.com/gaia-adm/pumba/tree/multistage
Builder (Alpine) Runtime (Alpine)
Content
go 1.8
glide (package)
cover (coverage)
go test (testing)
git
github-release (release)
goveralls (coveralls.io)
Bats (testing)
Bash
curl, openssl
Docker CLI
pumba
Size 427 MB 15 MB
Docker multi-stage with
codefresh.io
Multi-build Use Case
service-a:v1 service-b:v2 service-c:v1 service-d:v1.1
$ docker build -t repo/service-a:v1 --target service-a .
$ docker build -t repo/service-b:v2 --target service-b .
✓Same Dockerfile
✓Multiple Docker images
✓With --target flag
https://codefresh.io/blog/node_docker_multistage/

Docker multi-stage build