Optimize Docker image size scheme

Source: Internet
Author: User
If we use Dockerfile to build Docker mirrors, it would be very scary to accidentally cause the mirror to be larger than 1G. Are generally good hundreds of trillion. Larger mirrors tend to cause porting and migration is slow. Dockerfile, like code, needs to be optimized continuously. Using the following 4 optimization schemes, you can significantly reduce the size of the mirror.
The most important factor is to reduce the number of mirrored layers, which can greatly reduce the size of the mirror, and use chain code "&&" to combine multiline instructions into one line.
The mirrored size created by this method is: 837.6M from centos:6.7 maintainer Chenyufeng "yufengcode@gmail.com"
# Set the current tool directory # This command does not add a mirrored layer Workdir/home
# Install the necessary tools RUN Yum install-y wget && rpm--rebuilddb && yum install-y tar
# download Zip pack RUN wget--no-check-certificate--no-cookies--header "Cookie:oraclelicense=accept-securebackup-cookie" from JDK website http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/ jdk-8u131-linux-x64.tar.gz && Tar-xvzf jdk-8u131-linux-x64.tar.gz
# Download the zip pack RUN wget http://mirrors.shuosc.org/apache/tomcat/tomcat-8/v8.0.47/bin/apache-tomcat-8.0.47.tar.gz from Tomcat website && tar-xvzf apache-tomcat-8.0.47.tar.gz && mv Apache-tomcat-8.0.47/tomcat
# SET environment variable env java_home/home/jdk1.8.0_131 env catalina_home/home/tomcat env PATH $PATH: $JAVA _home/bin: $CATALINA _home/ Bin
# Exposing Tomcat 8080 port expose 8080
entrypoint/home/tomcat/bin/startup.sh && tail-f/home/tomcat/logs/catalina.out
# Create a container to start Tomcat CMD ['/home/tomcat/bin/startup.sh ', ' run ']

In fact, without affecting the reading of the case, you can write a line of run code, with "&&", "\" to connect. But the build process is slower than multiple run times.
This method creates a mirror size of: 837.6M "The result is the same" from centos:6.7 maintainer Chenyufeng "yufengcode@gmail.com"
# Set the current tool directory # This command does not add a mirrored layer Workdir/home
# Install the necessary tools RUN Yum install-y wget && rpm--rebuilddb && \ Yum install-y tar && WG ET--no-check-certificate--no-cookies--header "Cookie:oraclelicense=accept-securebackup-cookie" http:// Download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz && \ tar-xvzf jdk-8u131-linux-x64.tar.gz && wget http://mirrors.shuosc.org/apache/tomcat/tomcat -8/v8.0.47/bin/apache-tomcat-8.0.47.tar.gz && \ tar-xvzf apache-tomcat-8.0.47.tar.gz && MV APAC He-tomcat-8.0.47/tomcat
# SET environment variable env java_home/home/jdk1.8.0_131 env catalina_home/home/tomcat env PATH $PATH: $JAVA _home/bin: $CATALINA _home/ Bin
# Exposing Tomcat 8080 port expose 8080
entrypoint/home/tomcat/bin/startup.sh && tail-f/home/tomcat/logs/catalina.out
# Create a container to start Tomcat CMD ['/home/tomcat/bin/startup.sh ', ' run ']
Depending on the actual requirements and test results, you can choose to need a few run.

Clear Yum Cache in Dockerfile we might use the Yum command to download something, but Yum will store the downloaded packages and headers in the cache without automatically deleting them. This step can be performed at each level, that is, each layer can clear the Yum cache, instead of saying that a dockerfile file is a Yun clean all, and that a mirrored layer needs a Yun cleaning all.
# Clear Header Yum Clean headers

# clear the downloaded RPM package Yum clean Packages

# Clear All Yun cleaning all

Optimized Dockerfile, the constructed mirror size is 788.1m:from centos:6.7 maintainer Chenyufeng "yufengcode@gmail.com"
# Set the current tool directory # This command does not add a mirrored layer Workdir/home
# Install the necessary tools RUN Yum install-y wget && rpm--rebuilddb && \ Yum install-y tar && WG ET--no-check-certificate--no-cookies--header "Cookie:oraclelicense=accept-securebackup-cookie" http:// Download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz && \ tar-xvzf jdk-8u131-linux-x64.tar.gz && wget http://mirrors.shuosc.org/apache/tomcat/tomcat -8/v8.0.47/bin/apache-tomcat-8.0.47.tar.gz && \ tar-xvzf apache-tomcat-8.0.47.tar.gz && MV APAC He-tomcat-8.0.47/tomcat && \ Yum Clean all
# SET environment variable env java_home/home/jdk1.8.0_131 env catalina_home/home/tomcat env PATH $PATH: $JAVA _home/bin: $CATALINA _home/ Bin
# Exposing Tomcat 8080 port expose 8080
entrypoint/home/tomcat/bin/startup.sh && tail-f/home/tomcat/logs/catalina.out
# Create a container to start Tomcat CMD ['/home/tomcat/bin/startup.sh ', ' run ']

removing unwanted tar.gz installation packages in Dockerfile may use commands such as wget to download multiple tar.gz, tar packets, which should be removed after use. It is also best to combine "yum clean with all".
The mirrored size of the build is 593.2M at this time. The effect of thin body is very obvious. From centos:6.7 maintainer Chenyufeng "yufengcode@gmail.com"
# Set the current tool directory # This command does not add a mirrored layer Workdir/home
# Install the necessary tools RUN Yum install-y wget && rpm--rebuilddb && \ Yum install-y tar && WG ET--no-check-certificate--no-cookies--header "Cookie:oraclelicense=accept-securebackup-cookie" http:// Download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz && \ tar-xvzf jdk-8u131-linux-x64.tar.gz && wget http://mirrors.shuosc.org/apache/tomcat/tomcat -8/v8.0.47/bin/apache-tomcat-8.0.47.tar.gz && \ tar-xvzf apache-tomcat-8.0.47.tar.gz && MV APAC He-tomcat-8.0.47/tomcat && \ rm-f jdk-8u131-linux-x64.tar.gz && \ rm-f Apache-tomcat-8.0.47.tar . gz && \ Yum Clean all
# SET environment variable env java_home/home/jdk1.8.0_131 env catalina_home/home/tomcat env PATH $PATH: $JAVA _home/bin: $CATALINA _home/ Bin
# Exposing Tomcat 8080 port expose 8080
entrypoint/home/tomcat/bin/startup.sh && tail-f/home/tomcat/logs/catalina.out
# Create a container to start Tomcat CMD ['/home/tomcat/bin/startup.sh ', ' run ']



Select a smaller base image
Reduce the size of the custom mirror from the source.

This is a ubuntu16.04 to compare the centos6.7,
The following mirrors are built: 558.6M. Less than the base image of CentOS. From ubuntu:16.04 maintainer Chenyufeng "yufengcode@gmail.com"
# Set the current tool directory # This command does not add a mirrored layer Workdir/home
# Install the necessary tools
Run Apt-get update run apt-get install wget--assume-yes #RUN rpm--rebuilddb RUN apt-get Install tar--assume-yes
RUN wget--no-check-certificate--no-cookies--header "Cookie:oraclelicense=accept-securebackup-cookie" http:// Download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz && \ tar-xvzf jdk-8u131-linux-x64.tar.gz && wget http://mirrors.shuosc.org/apache/tomcat/tomcat -8/v8.0.47/bin/apache-tomcat-8.0.47.tar.gz && \ tar-xvzf apache-tomcat-8.0.47.tar.gz && MV APAC He-tomcat-8.0.47/tomcat && \ rm-f jdk-8u131-linux-x64.tar.gz && \ rm-f Apache-tomcat-8.0.47.tar . gz
# SET environment variable env java_home/home/jdk1.8.0_131 env catalina_home/home/tomcat env PATH $PATH: $JAVA _home/bin: $CATALINA _home/ Bin
# Exposing Tomcat 8080 port expose 8080
entrypoint/home/tomcat/bin/startup.sh && tail-f/home/tomcat/logs/catalina.out
# Create a container to start Tomcat CMD ['/home/tomcat/bin/startup.sh ', ' run ']

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.