Foreword

Automated build is an indispensable part of the application release process. Commonly used build tools are jenkins, walle and so on. These tools often have the following problems when building an app:

  1. Need to write a shell command for construction, etc., directly or indirectly, difficult to manage, poor compatibility
  2. The above point may be easier to solve, but the most deadly is: heavily dependent on the software environment such as jenkins host or baler, such as git, maven, java, etc.

The ideal situation is: Different applications, such as java applications, go applications, php applications, etc., can perform parallel and non-interfering execution operations on a host that is responsible for the build, and the software environment and build process depend on the build. Can be controlled by the developer.

So far, the mission can be done well, it may not be [docker] (https://zhouzhipeng.com/category/docker)!

In the world of docker, the build is delivered with a mirror, and the one that can be mirrored is Dockerfile (manual use of docker commit).

After docker ce 17.05, there is a very important feature Multi-Stage Build (multi-stage build), which will significantly increase your operational productivity!

The following will use the actual case to explain the feature of Multi-Stage Build in detail.

Before Multi-Stage Build

The following demo takes the example of java hello world, the complete code is: [https://github.com/zhouzhipeng/docker-multi-stage-demo](https://github.com/zhouzhipeng/docker-multi-stage -demo)

This is a standard maven project with only one HelloWorld main class. The general construction idea is:

  1. Compile and package the project in the maven image
  2. Copy the jar generated in step 1
  3. Using the jar from step 2, build and run the main class in the jar in the jre image.

Dockerfile.build is used to compile and package jars

Dockerfile.old is used to run the main class in the jar

Note that the docker-multi-stage-demo-1.0-SNAPSHOT.jar file associated with the two dockerfiles requires another build.sh script to string.

Build.sh

Friends who know about Dockerfile and shell believe that they should understand it. Don’t repeat it here.

After Multi-Stage Build

After reading the previous section, you might feel a little trouble? Yes, the trouble is not only to write multiple dockerfiles, but also a build.sh script for extra execution. Undoubtedly increased the complexity of building applications!

Combine the above Dockerfile.build with Dockerfile.old and slightly modify it to get the following new Dockerfile:

Then, still the familiar docker build command

Just fine.

Careful, you should not find that there are two different places in the Dockerfile above.

  1. Multiple FROM statements have appeared
  2. --from=builder after the COPY command

This is today's main coffee Multi-Stage Build, first come through a picture to intuitively feel what is called 'Multi-Stage Build (multi-stage build):

Through multi-stage build, you can keep the Dockerfile simple and easy to read, and make the final product image "clean".

Simple understanding

Or the Dockerfile in the above example, as shown below:

The parts in the red box can be thought of as separate "stages", and you can roughly imagine that the achievement is a separate Dockerfile content.

Everyone knows that the mirror construction is superimposed layer by layer. According to the command line order of the Dockerfile, the overlay is performed from top to bottom. Therefore, the lower stage can be referenced to the upper stage. In order to facilitate reference to the upper stage, you need to give it a name, using the as operator.

The full format of the FROM command is as follows:

The interaction between the stages is a file, so the COPY command needs to be extended. Use --from= to specify which "stage" to copy the file from above. The complete command format is as follows:

It’s worth mentioning that by default, using the docker build command to build a dockerfile with multiple stages, the final product is the image generated by the bottom stage.

Of course, if for debugging reasons or other requirements, docker also supports building to the specified stage, using --target builder to build only the builder image.

last step

So far, we have a Dockerfile that can be built with one click, and then it’s only allowed to build automatically!

You can use the familiar jenkins with github’s webhook to implement the code once and execute the docker build command.

Of course, if I recommend a personal experience, use the official docker hub, because the images you build can be shared with others.

The specific use of the Docker hub’s automated build function is not explained in detail. Below is a quick demonstration with a gif diagram. Interested friends can explore it on their own.

![](https://zhouzhipeng.com/wp-content/uploads/2018/02/gif5new file-2.gif)

to sum up

The Multi-Stage Build feature is ideal for building pipeline flows, and is ideal for applications that rely on complex environments and complex processes.

You can try the above source code under clone: ​​https://github.com/zhouzhipeng/docker-multi-stage-demo

references

[https://docs.docker.com/v17.09/engine/userguide/eng-image/multistage-build/](https://docs.docker.com/v17.09/engine/userguide/eng– Image/multistage-build/)

https://blog.alexellis.io/mutli-stage-docker-builds/

Last modified: 2019年3月29日

Author

Comments

开发者头条 

感谢分享!已推荐到《开发者头条》:https://toutiao.io/posts/li2d7t 欢迎点赞支持!
欢迎订阅《CodingAir》https://toutiao.io/subjects/185910

Write a Reply or Comment

Your email address will not be published.