In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to build a multi-stage mirror image of Docker, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
Since the Docker version of 17.05.0-ce, a new way to build images has been supported, called Multi-stage builds, which is designed to address some of the pain points in Docker building application containers. In the daily scenario of building a container, it is often encountered that the source code is obtained, compiled and generated in the same container, and finally built as an image. The disadvantage of doing so is:
Have to install the runtime environment necessary for the builder in the container
Have to get the source code of the program and some ecological tools needed to build in the same container
The constructed image even contains the program source code and some unnecessary files, resulting in the large size of the container image.
Of course, there is also a slightly more elegant way, that is, we package the project and its dependent library compilation tests externally in advance, and then copy them into the build directory, although this can well avoid the risk points that exist in the first way. However, you also need to consider the differences in the compatibility of different mirror runs.
In fact, these pain points, Docker also thought of, the official provides a simple multi-stage build (multi-stage build) solution. The so-called multi-stage construction, that is, the construction process is divided into multiple stages, in the same Dockerfile, through different stages to build and generate the required application files, and finally add these application files to a release image. Doing so can completely avoid a series of problems encountered above. Implementing a multi-stage build mainly depends on the newly provided keywords: from and as.
Here's a chestnut:
FROM muninn/glide:alpine AS build-envADD. / go/src/my-projWORKDIR / go/src/my-projRUN go get-vRUN go build- o / go/src/my-proj/my-serverFROM alpineRUN apk add-U tzdataRUN ln-sf / usr/share/zoneinfo/Asia/Shanghai / etc/localtimeCOPY-- from=build-env / go/src/my-proj/my-server / my-serverEXPOSE 80CMD ["my-server"]
A multi-phase Dockerfile looks like it merges two or more Dockerfile together, which means multi-phase. The as keyword is used to give the build phase an alias so that in another build phase, the build output of the corresponding keyword phase can be referenced and used by the from keyword and packaged into a container.
After the completion of the multi-stage build, the output image only contains the final output of the my-server application, there are no other source files and third-party source packages, very clean and concise. Because the build-env phase is just an intermediate process of building.
Even, we can use more build phases to build different applications, and eventually merge these built-out applications into a mirror that eventually needs to be released. We can look at a more complicated chestnut:
From debian as build-essentialarg APT_MIRRORrun apt-get updaterun apt-get install-y make gccworkdir / srcfrom build-essential as foocopy src1. Run makefrom build-essential as barcopy src2. Run makefrom alpinecopy-- from=foo bin1. Copy-- from=bar bin2. Cmd.
The benefits of multi-stage construction are self-evident. You can easily build the desired container image through a single Dockerfile for multiple projects that depend on each other, without having to worry about risks such as too large image and source code disclosure. I have to say, this is a very good improvement.
Thank you for reading this article carefully. I hope the article "how to build a Multi-stage Image of Docker" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.