In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Docker can be said to have occupied most of the container market at present. Docker has revolutionized containers and changed the way modern cloud platforms are built. Although Docker is powerful, it also encounters some problems in its use. For example, if I want to build a compiled language image, I need to compile in one Dockerfile and then use another Dockerfile to put the compiled files in the image. This increases the complexity of CI/CD invisibly.
Docker multi-phase build is a new feature introduced after 17.05, which is designed to solve the complex problems of compilation and build. Reduce the size of the mirror. Therefore, to use the multi-phase build feature, you must use a Docker greater than or equal to 17.05.
Before the emergence of multi-phase construction
The most challenging thing about building an image is to keep the image size as small as possible. Each instruction in Dockerfile adds a layer to the image, and you need to remember to clean up any unwanted artifacts before moving to the next layer.
In order to write a truly efficient Dockerfile, it is traditionally necessary to use shell techniques and other logic to keep the layers as small as possible and to ensure that each layer has the artifacts needed by the previous layer rather than anything else.
In fact, there is a Dockerfile for development (which contains everything you need to build an application), and a stripped-down version of Dockerfile for a production environment that contains only your application and what you need to run it. This is called the "builder mode". Maintaining two Dockerfiles is not ideal.
This is an example of Dockerfile.build and Dockerfile, which follows the above pattern:
Dockerfile.build:
FROM golang:1.7.3
WORKDIR / go/src/github.com/alexellis/href-counter/
COPY app.go.
RUN go get-d-v golang.org/x/net/html\
& & CGO_ENABLED= GOOS=linux go build-a-installsuffix cgo-o app.
Note that this example uses the Bash & & operator to artificially compress two RUN commands to avoid creating other layers in image. This is error-prone and difficult to maintain.
Dockerfile:
FROM alpine:latest
RUN apk-no-cache add ca-certificates
WORKDIR / root/
COPY app.
CMD [". / app"]
Build.sh:
#! / bin/sh
Echo Building alexellis2/href-counter:build
Docker build--build-arg https_proxy=$https_proxy-- build-arg http_proxy=$http_proxy\
-t alexellis2/href-counter:build. -f Dockerfile.build
Docker container create-name extract alexellis2/href-counter:build
Docker container cp extract:/go/src/github.com/alexellis/href-counter/app. / app
Docker container rm-f extract
Echo Building alexellis2/href-counter:latest
Docker build-no-cache-t alexellis2/href-counter:latest.
Rm. / app
When you run the build.sh script, it needs to build the first image, create the container from which to copy the artifacts, and then build the second image.
Multi-stage build greatly simplifies this situation!
Use multi-phase build
For multi-phase builds, you can use multiple FROM statements in Dockerfile. Each FROM instruction can use a different basis, and each instruction starts a new build. You can optionally copy artifacts from one stage to another, leaving only what you want in the final image.
To illustrate how this works, let's adjust the Dockerfile of the above example to use a multi-phase build.
Dockerfile:
FROM golang:1.7.3
WORKDIR / go/src/github.com/alexellis/href-counter/
RUN go get-d-v golang.org/x/net/html
COPY app.go.
RUN CGO_ENABLED= GOOS=linux go build-a-installsuffix cgo-o app.
FROM alpine:latest
RUN apk-no-cache add ca-certificates
WORKDIR / root/
COPY-from= / go/src/github.com/alexellis/href-counter/app.
CMD [". / app"]
All you need is a single Dockerfile. Nor do you need a separate build script. Just run docker build:
$docker build-t app:latest.
The end result is an image of the same size as before, with significantly reduced complexity. You do not need to create any intermediate image, nor do you need to extract any artifacts to the local system.
How does it work? The second FROM instruction starts a new construction phase based on alpine:latest image.
The line COPY-from = 0 copies only the build file from the previous phase to this new phase. Go SDK and any middle tier are forgotten rather than saved in the final image.
Name multiple build phases
By default, phases are not named, and you can refer to them as integers, starting with the 0th FROM instruction.
However, you can name your phase by adding as NAME to the FROM directive. This example improves on the previous example by naming the phase and using the name in the COPY directive.
This means that even if the instructions in the Dockerfile are reordered later, the COPY will not break.
FROM golang:1.7.3 as builder
WORKDIR / go/src/github.com/alexellis/href-counter/
RUN go get-d-v golang.org/x/net/html
COPY app.go.
RUN CGO_ENABLED= GOOS=linux go build-a-installsuffix cgo-o app.
FROM alpine:latest
RUN apk-no-cache add ca-certificates
WORKDIR / root/
COPY-from=builder / go/src/github.com/alexellis/href-counter/app.
CMD [". / app"]
Stop at a specific construction phase
When building an image, you do not necessarily need to build every phase of the entire Dockerfile.
You can specify the target build phase. The following command assumes that you are using the previous Dockerfile, but stops at a phase called builder:
$docker build-target builder-t alexellis2/href-counter:latest.
Some of the most appropriate scenarios that may be appropriate to use this feature are:
◾ debugs specific build phases
◾ enables all debugging or tools in the debug phase, but as concise as possible in the production phase
◾ in the testing phase, your application will be populated with test data, but in the production phase, production data will be used
Use an external mirror as a stage
When using a multi-phase build, you can not only copy from the mirror created in Dockerfile.
You can also use the COPY-from directive to copy from a separate image, using the local image name, local or Docker registry available tags or tags ID.
If necessary, Docker will extract the image and start copying from there.
The syntax is:
COPY-- from=nginx:latest / etc/nginx/nginx.conf / nginx.conf
Original text link: multi-stage construction of https://wilhelmguo.tk/blog/post/william/Docker construction
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.