makeporngreatagain.pro
yeahporn.top
hd xxx

Reduce Docker Image size by Using Multi stage build

1,526

In Docker, one of the main issues is the size of the final image. It’s not uncommon to end up with images over 1 GB even for simple Java applications.

Since version 17.05 of Docker, it’s possible to have multiple builds in a single Dockerfile, and to access the output of the previous build into the current one.

Those are called multi-stage builds. The final image will be based on the last build stage.

Now, we will create two docker images: one without multi-stage builds and another with multi-stage builds and compare the size of both.

Let’s consider one maven project, which is hosted on GitHub and we have to build that project and deploy it on Tomcat server.

1.So, First we will install JAVA as below
Run apt-get update
Run apt-get install -y openjdk-8-jdk wget
2. then Install Git and clone repository
#Install git
RUN apt-get install git -y
RUN git clone https://github.com/webmagicinformatica/SimpleMavenProject.git
RUN cp -r $PWD/SimpleMavenProject/* $PWD
3. Now, install maven and build maven project to create WAR file i.e webapp.war
#Install maven
RUN apt-get install maven -y
RUN mvn clean install package
4. Finally, we have to install tomcat and after configuration, copy WAR file into webapps folder and start service.
#Install apache tomcat
Run groupadd tomcat
Run useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Run cd /tmp
RUN wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-8.5.35.tar.gz
Run mkdir /opt/tomcat
Run tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat –strip-components=1
Run cd /opt/tomcat
Run chgrp -R tomcat /opt/tomcat
Run chmod -R g+r /opt/tomcat/conf
Run chmod g+x /opt/tomcat/conf
Run chown -R tomcat /opt/tomcat/webapps /opt/tomcat/work /opt/tomcat/temp
Run cd /opt/tomcat/bin
COPY ./webapp.war /opt/tomcat/webapps
Expose 8080
CMD /opt/tomcat/bin/catalina.sh run && tail -f /opt/tomcat/logs/catalina.out
Complete dockerfile will look like this:
# Dockerfile
FROM ubuntu:latest
RUN mkdir /app
WORKDIR /app
Run apt-get update
Run apt-get install -y openjdk-8-jdk wget
#Install git
RUN apt-get install git -y
RUN git clone https://github.com/webmagicinformatica/SimpleMavenProject.git
RUN cp -r $PWD/SimpleMavenProject/* $PWD
#Install maven
RUN apt-get install maven -y
RUN mvn clean install package
#Install apache tomcat
Run groupadd tomcat
Run useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Run cd /tmp
RUN wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-8.5.35.tar.gz
Run mkdir /opt/tomcat
Run tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat –strip-components=1
Run cd /opt/tomcat
Run chgrp -R tomcat /opt/tomcat
Run chmod -R g+r /opt/tomcat/conf
Run chmod g+x /opt/tomcat/conf
Run chown -R tomcat /opt/tomcat/webapps /opt/tomcat/work /opt/tomcat/temp
Run cd /opt/tomcat/bin
COPY ./webapp.war /opt/tomcat/webapps
Expose 8080
CMD /opt/tomcat/bin/catalina.sh run && tail -f /opt/tomcat/logs/catalina.out

To create multi stage build image, follow these steps:

1. First, create a directory by name “with_MSB” and a dockerfile in it and add the above content.
“mkdir /without_MSB”
“cd /without_MSB”
“vi Dockerfile”
2. Build image by using below command
“docker build -t app_image .”
3. To create Container, run below command
“docker run -it -p 8080:8080 -d –name maven_app app_image”
4. Now, you can check your application on browser
“http://<IP_ADDRESS>:8080/webapp”
5. Check image size by using below command
“docker images | grep app_image”

 

Image size will be more than 600MB

6. Stop container and remove it by using below command
“docker stop maven_app”
“docker rm maven_app”

Now create docker image with multi-stage builds for same application

So build stages would be as follows:

1. Clone the Maven project repository from Git.
FROM alpine/git as clone
WORKDIR /app
RUN git clone https://github.com/webmagicinformatica/SimpleMavenProject.git
2. Copy the files from the previous stage and build the app with Maven.
FROM maven:3.5-jdk-8-alpine as build
WORKDIR /app
COPY –from=clone /app/SimpleMavenProject /app
RUN mvn clean install package
3. Copy the WAR file from the previous stage deploys it on Tomcat i.e in webapp folder.
FROM tomcat:8-jre8-alpine
COPY –from=build /app/webapp/target/webapp.war $CATALINA_HOME/webapps
EXPOSE 8080
Complete dockerfile will look like this

# Dockerfile

FROM alpine/git as clone
WORKDIR /app
RUN git clone https://github.com/webmagicinformatica/SimpleMavenProject.git
FROM maven:3.5-jdk-8-alpine as build
WORKDIR /app
COPY –from=clone /app/SimpleMavenProject /app
RUN mvn clean install package
FROM tomcat:8-jre8-alpine
#WORKDIR /app
COPY –from=build /app/webapp/target/webapp.war $CATALINA_HOME/webapps
EXPOSE 8080

To create multi-stage builds image,

1. First, create directory “with_MSB” and dockerfile in it and add the above content.
“mkdir /with_MSB”
“cd /with_MSB”
“vi Dockerfile”
2. Build image by using below command
“docker build -t app_image_msb .”
3. To create Container run below command
“docker run -it -p 8080:8080 -d –name maven_app_msb app_image_msb”
4. Now you can check your application on browser
“http://<IP_ADDRESS>:8080/webapp”
5. Check image size by using below command
“docker images | grep app_image_msb”

Image size will be 6 times less than earlier size (without multi-stage build image)
6. Stop container and remove it by using below command
“docker stop maven_app”
“docker rm maven_app”

Conclusion:

We have created two images here, one without multi-stage i.e app_image and another one by using multi-stage i.e app_image_msb.

We have noticed that app_image_msb size is 6 times lesser than app_image.

So by using multi-stage build we can reduce the size of the image.

Comments are closed, but trackbacks and pingbacks are open.

baseofporn.com https://www.opoptube.com
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.