Docker Multi-stage
builds
By Rathneesh TM (HPE)
Bangalore Docker Meetup #33
Docker build
Builds Docker images from a Dockerfile and a “context”.
A build’s context is the files located in the specified PATH or URL.
The build process can refer to any of the files in the context.
Each directive in Dockerfile creates a layer.
Each Dockerfile generates on image.
Docker image build best practices
Avoid installing unnecessary packages
Each container should have only one concern
Minimize the number of layers
Minimize the size of the image
Packaging an application in docker
image
application
compiler
test code
build /test logs/reports
application
Docker Builder pattern
Build in multiple stages
2 Docker files
1 – Build environment
2 – Run time environment
Work flow
Doesn’t run everywhere
Docker multi stage builds
Available from Docker 17.05.
Extended FROM and COPY directives.
Supports Multiple FROM directives.
Each FROM statement is start of a new context.
COPY from any previous stage to the current context.
The last FROM directive creates the final image.
The FROM syntax also supports AS keyword
Multistage build exampleFROM node:latest AS storefront
WORKDIR /usr/src/atsea/app/react-app
COPY react-app .
RUN npm install
RUN npm run build
FROM maven:latest AS appserver
WORKDIR /usr/src/atsea
COPY pom.xml .
RUN mvn -B -f pom.xml -s /usr/share/maven/ref/settings-docker.xml dependency:resolve
COPY . .
RUN mvn -B -s /usr/share/maven/ref/settings-docker.xml package -DskipTests
FROM java:8-jdk-alpine
RUN adduser -Dh /home/gordon gordon
WORKDIR /static
COPY --from=storefront /usr/src/atsea/app/react-app/build/ .
WORKDIR /app
COPY --from=appserver /usr/src/atsea/target/AtSea-0.0.1-SNAPSHOT.jar .
ENTRYPOINT ["java", "-jar", "/app/AtSea-0.0.1-SNAPSHOT.jar"]
CMD ["--spring.profiles.active=postgres"]
https://github.com/dockersamples/atsea-sample-shop-app/blob/master/app/Dockerfile
Simplified
One Dockerfile
One syntax(Dockerfile)
one build
Create as many stages as needed
References
https://github.com/moby/moby/pull/31257
https://github.com/moby/moby/pull/32496
https://blog.alexellis.io/mutli-stage-docker-builds
http://blog.arungupta.me/smaller-java-image-docker-multi-stage-build/

Docker multi stage builds

  • 1.
    Docker Multi-stage builds By RathneeshTM (HPE) Bangalore Docker Meetup #33
  • 2.
    Docker build Builds Dockerimages from a Dockerfile and a “context”. A build’s context is the files located in the specified PATH or URL. The build process can refer to any of the files in the context. Each directive in Dockerfile creates a layer. Each Dockerfile generates on image.
  • 3.
    Docker image buildbest practices Avoid installing unnecessary packages Each container should have only one concern Minimize the number of layers Minimize the size of the image
  • 4.
    Packaging an applicationin docker image application compiler test code build /test logs/reports application
  • 5.
    Docker Builder pattern Buildin multiple stages 2 Docker files 1 – Build environment 2 – Run time environment Work flow Doesn’t run everywhere
  • 6.
    Docker multi stagebuilds Available from Docker 17.05. Extended FROM and COPY directives. Supports Multiple FROM directives. Each FROM statement is start of a new context. COPY from any previous stage to the current context. The last FROM directive creates the final image. The FROM syntax also supports AS keyword
  • 7.
    Multistage build exampleFROMnode:latest AS storefront WORKDIR /usr/src/atsea/app/react-app COPY react-app . RUN npm install RUN npm run build FROM maven:latest AS appserver WORKDIR /usr/src/atsea COPY pom.xml . RUN mvn -B -f pom.xml -s /usr/share/maven/ref/settings-docker.xml dependency:resolve COPY . . RUN mvn -B -s /usr/share/maven/ref/settings-docker.xml package -DskipTests FROM java:8-jdk-alpine RUN adduser -Dh /home/gordon gordon WORKDIR /static COPY --from=storefront /usr/src/atsea/app/react-app/build/ . WORKDIR /app COPY --from=appserver /usr/src/atsea/target/AtSea-0.0.1-SNAPSHOT.jar . ENTRYPOINT ["java", "-jar", "/app/AtSea-0.0.1-SNAPSHOT.jar"] CMD ["--spring.profiles.active=postgres"] https://github.com/dockersamples/atsea-sample-shop-app/blob/master/app/Dockerfile
  • 8.
    Simplified One Dockerfile One syntax(Dockerfile) onebuild Create as many stages as needed
  • 9.