Multi-stage docker builds
Multi-stage builds are a new feature requiring Docker ≥ 17.05

Why we need multi-stage build?
One of the most challenging things about building images is keeping the image size down. For that we have to be careful while moving from one environment to another environment and we needed to keep tracks of artifacts, traditionally these can be achieved using shell scripts. (shell script at the rescue 😄 ).
Apart from that, Maintaining two or more dockerfile
for application is not ideal. Multi-stage build simplifies this situation.
What is multi-stage build?
Multi-stage builds are a new feature requiring Docker 17.05 or higher on the daemon and client. Multistage builds are useful to anyone who has struggled to optimize Dockerfiles while keeping them easy to read and maintain.
With multi-stage builds, you use multiple FROM
statements in your Dockerfile. Each FROM
instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image.
COPY --from=0 /src/app .
In the above instruction, we are using stage 0 to copy artifacts and leaving everything else behind. But, numbering stage, let’s just say not easy to read.
let's create sample tomcat docker images and lets C image size
FROM tomcat AS step1 MAINTAINER Gudditi WORKDIR /usr/local/tomcat/webapps/ RUN apt update && apt install git -y RUN mkdir /home/git/ && cd /home/git/ && git clone https://github.com/GudditiNaganjaneyulu/MyResumeDocker.git RUN cp /home/git/MyResumeDocker/index/resume.war /usr/local/tomcat/webapps/ EXPOSE 8080
[node1] (local) root@192.168.0.28 ~ $ docker build -t tomcat: sample . [node1] (local) root@192.168.0.28 ~ $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat sample 8ab7f546887a About a minute ago 589MB tomcat new ae80e414a240 24 minutes ago 475MB tomcat latest 1dad8c5497f9 2 weeks ago 474MB [node1] (local) root@192.168.0.28 ~
the size of image we build 589 MB size we can reduce this size to minimal using multistage build concept
We are going to use below docker file to optimize image size : FROM tomcat AS step1 MAINTAINER Gudditi WORKDIR /usr/local/tomcat/webapps/ RUN apt update && apt install git -y RUN mkdir /home/git/ && cd /home/git/ && git clone https://github.com/GudditiNaganjaneyulu/MyResumeDocker.git RUN cp /home/git/MyResumeDocker/index/resume.war /usr/local/tomcat/webapps/ EXPOSE 8080 FROM tomcat WORKDIR /usr/local/tomcat/webapps/ COPY --from=step1 /usr/local/tomcat/webapps/ . we are going to build image [node1] (local) root@192.168.0.28 ~ $ docker build -t tomcat:multistage_build . and lets C variation in memory and image size [node1] (local) root@192.168.0.28 ~ $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat sample 8ab7f546887a 9 minutes ago 589MB tomcat multistage_build ae80e414a240 31 minutes ago 475MB tomcat new ae80e414a240 31 minutes ago 475MB tomcat latest 1dad8c5497f9 2 weeks ago 474MB
Red: Old Build docker image with size 589 MB
Green: New image build with size of 475 MB only
***
Comments
Post a Comment