Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Write Dockerfile best practices

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Although there are hundreds of thousands of free images in the official repository, most of them cannot directly meet the business needs of the company, which requires us to customize the images ourselves.

Docker automatically builds images through Dockerfile, a text file containing instructions for building images.

Here are 4 tips to help you write an efficient and easy-to-use Dockerfile.

1. Reduce mirror layers

A RUN command forms a new layer, try to write Shell commands in one line, reduce the number of mirror layers.

For example:

FROM centos:7MAINTAINER www.ctnrs.comRUN yum install epel-release -y RUN yum install -y gcc gcc-c++ make -yRUN wget http://docs.php.net/distributions/php-5.6.36.tar.gzRUN tar zxf php-5.6.36.tar.gzRUN cd php-5.6.36RUN ./ configure --prefix=/usr/local/php RUN make -j 4 RUN make installEXPOSE 9000CMD ["php-fpm"]

It should read:

FROM centos:7MAINTAINER www.ctnrs.comRUN yum install epel-release -y && \ yum install -y gcc gcc-c++ makeRUN wget http://docs.php.net/distributions/php-5.6.36.tar.gz && \ tar zxf php-5.6.36.tar.gz && \ cd php-5.6.36 && \ ./ configure --prefix=/usr/local/php && \ make -j 4 && make installEXPOSE 9000CMD ["php-fpm"]

Results: 12-> 6

2. Optimizing mirror size: cleaning up garbage

A RUN forms a new layer. If it is not deleted at the same layer, it will be brought to the next layer regardless of whether the file is deleted at the end. Therefore, it is necessary to clean up the corresponding residual data at each layer and reduce the mirror size.

FROM centos:7MAINTAINER www.ctnrs.comRUN yum install epel-release -y && \ yum install -y gcc gcc-c++ make gd-devel libxml2-devel \ libcurl-devel libjpeg-devel libpng-devel openssl-devel \ libmcrypt-devel libxslt-devel libtidy-devel autoconf \ iproute net-tools telnet wget curl && \ yum clean all && \ rm -rf /var/cache/yum/*RUN wget http://docs.php.net/distributions/php-5.6.36.tar.gz && \ tar zxf php-5.6.36.tar.gz && \ cd php-5.6.36 && \ ./ configure --prefix=/usr/local/php \ make -j 4 && make install && \ cd / && rm -rf php*

At least tens of M, or even hundreds of M can be saved.

3. Reduce network transit time

It is best to have a place to store software packages internally, similar to the PHP official download address above: docs.php.net/distributions/php-5.6.36.tar.gz, if you use maven to build such operations, but also change to a private maven repository, reduce network transmission time, improve the speed of image construction.

4. Multi-stage mirror build

If we run a project, we copy the code directly into the base image, according to our above practice. What if it is a project that needs to compile the code beforehand? JAVA language, for example, how to compile code, deployment together to complete it!

The above approach requires building a basic image in a Dockerfile in advance, including the project runtime environment and dependent libraries, and then writing a Dockerfile to copy the project to the runtime environment, which is a bit complicated.

For languages like JAVA, if the code compilation is operated in Dockerfile, the source code needs to be built in, but only the package needs to be built in actual operation. This kind of source code has certain security risks and also increases the image volume.

To address these issues, Docker 17.05 started supporting multi-stage builds, which simplify Dockerfiles and reduce image size.

For example, building JAVA project images:

# git clone https://github.com/lizhenliang/tomcat-java-demo# cd tomcat-java-demo# vi DockerfileFROM maven AS buildADD ./ pom.xml pom.xmlADD ./ src src/RUN mvn clean packageFROM lizhenliang/tomcatRUN rm -rf /usr/local/tomcat/webapps/ROOTCOPY --from=build target/*.war /usr/local/tomcat/webapps/ROOT.war# docker build -t demo:v1 .# docker container run -d -v demo:v1

First of all, the first FROM followed by an AS keyword, you can give this stage a name.

Then, the second part FROM uses the Tomcat image we built above, with the-from parameter added to the COPY keyword, which copies files from a certain stage to the current stage. A Dockerfile is done.

Summary: Mirrors have many benefits, such as fast deployment and fast rollback. Reduce downtime and use less disk space for mirrored repositories.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report