Docker Learning Note 17:dockerfile Instruction Onbuild Introduction

Source: Internet
Author: User

The ONBUILD directive can add triggers for mirroring. The parameter is any one of the dockerfile directives.

When we add the Onbuild directive to a dockerfile file, the directive does not have a substantial impact on the use of the dockerfile to build the image (for example, a mirror).

But when we write a new Dockerfile file to build a mirror based on a mirror (such as a B-mirror), the onbuild instruction in the Dockerfile file that constructs the A-mirror takes effect, and in the process of building B-mirroring, The instructions specified by the onbuild instruction are executed first before other instructions are executed.

It is important to note that if you are using B-mirror to construct a new mirror, the onbuild instruction is invalid, that is, it can only be executed in the sub-image, and is not valid for the grandchild image construction. In fact, it is reasonable to think, because in the construction of the sub-image has been executed, if the Sun Zi like the construction to execute, the equivalent of repeated execution, there is a problem.

With the Onbuild directive, it is actually equivalent to creating a template image, which can then be used to create a specific sub-image based on the template image, and some common operations that need to be performed during the sub-image build process can be specified in the Dockerfile file corresponding to the template image with the onbuild directive. This reduces the duplication of dockerfile files.

Let's look at a simple example.

1, first write a dockerfile file, the contents are as follows:

mkdir Mydir

Use the Dockerfile file above to build the image: Docker build-t Imagea.
Creating Containers with Imagea images: Docker run--name test1-it Imagea/bin/bash

We found that the root directory of the Test1 container does not have a mydir directory. The instructions specified by the Onbuild directive are not executed in their own build.

2, and then write a new Dockerfile file, the content is as follows

#testFROM Imageamaintainer Hello1

Note that the base image that this build prepares to use is the image that is constructed above Imagea
Use the Dockerfile file above to build the image: Docker build-t Imageb.
Creating Containers with Imagea images: Docker run--name test2-it Imageb/bin/bash

We found the Mydir directory under the root directory of the Test2 container, indicating that the trigger was executed. This is actually the output log from the construction of the IMAGEB can be seen. The logs are as follows:

[Email protected]:~/myimage$ Docker Build-T Imageb. Sending build context to Docker daemon15.87Kbstep1: from imagea# executing1Build Trigger ... Step1: RUNmkdirMydir---> Runninginche16c35c94b03--->4b393d1610a6removing Intermediate Container e16c35c94b03step2: Maintainer Hello1---> RunninginchC7b0312516ea--->0f63b8e04d82removing Intermediate container c7b0312516easuccessfully built 0f63b8e04d82

As we can see, a trigger is executed immediately after the from instruction is executed (the instruction specified by the ONBUILD Directive)

Docker Learning Note 17:dockerfile Instruction Onbuild Introduction

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.