【Docker】Dockerfile 之 ONBUILD
參考教程:https://docs.docker.com/engine/reference/builder/
環境
- virtual box 6.1
- centos 7.8
- docker 19.03
ONBUILD
ONBUILD <INSTRUCTION>
The ONBUILD
instruction adds to the image a trigger instruction to be executed at a later time, when the image is used as the base for another build. The trigger will be executed in the context of the downstream build, as if it had been inserted immediately after the FROM
Dockerfile
.
當映象被用作另一個構建的基礎映象時,ONBUILD
指令會在映象上新增 trigger 指令,以便稍後執行。觸發器將在下游構建的上下文中執行,就好像它是在下游 Dockerfile
中的 FROM
指令之後立即插入的。
Any build instruction can be registered as a trigger.
任何構建指令都可以註冊為觸發器。
This is useful if you are building an image which will be used as a base to build other images, for example an application build environment or a daemon which may be customized with user-specific configuration.
如果您要構建的映象將用作構建其他映象的基礎,例如,可以使用使用者特定的配置自定義的應用程式構建環境或守護程式,則此功能很有用。
For example, if your image is a reusable Python application builder, it will require application source code to be added in a particular directory, and it might require a build script to be called after that. You can’t just call ADD
and RUN
Dockerfile
to copy-paste into their application, but that is inefficient, error-prone and difficult to update because it mixes with application-specific code.
例如,如果您的映象是可重用的 Python 應用程式構建器,則將需要在特定目錄中新增應用程式原始碼,並且在那之後可能需要呼叫構建指令碼。您不能立即呼叫 ADD
和 RUN
,因為您還沒有訪問應用程式原始碼的許可權,並且每個應用程式構建版本的程式碼都不同。您可以簡單地為應用程式開發人員提供模板檔案 Dockerfile
,以將其複製貼上到他們的應用程式中,但這效率低下,容易出錯且難以更新,因為它與特定於應用程式的程式碼混合在一起。
The solution is to use ONBUILD
to register advance instructions to run later, during the next build stage.
解決方案是使用 ONBUILD
註冊預先的指令,以便在下一個構建階段稍後執行。
Here’s how it works:
運作方式如下:
-
When it encounters an
ONBUILD
instruction, the builder adds a trigger to the metadata of the image being built. The instruction does not otherwise affect the current build. -
構建器在遇到
ONBUILD
指令時,將觸發器新增到正在構建的影象的元資料中。該指令不會影響當前版本。 -
At the end of the build, a list of all triggers is stored in the image manifest, under the key
OnBuild
. They can be inspected with thedocker inspect
command. -
在構建結束時,所有觸發器的列表都儲存在映象清單中的
OnBuild
鍵下。可以使用docker inspect
命令來檢查它們。 -
Later the image may be used as a base for a new build, using the
FROM
instruction. As part of processing theFROM
instruction, the downstream builder looks forONBUILD
triggers, and executes them in the same order they were registered. If any of the triggers fail, theFROM
instruction is aborted which in turn causes the build to fail. If all triggers succeed, theFROM
instruction completes and the build continues as usual. -
之後,可以使用
FROM
指令將該映象用作新版本的基礎映象。作為處理FROM
指令的一部分,下游構建器將查詢ONBUILD
觸發器,並以與註冊時相同的順序執行它們。如果任何觸發器失敗,則FROM
指令將中止,從而導致構建失敗。如果所有觸發均成功,則FROM
指令完成,並且構建照常繼續。 -
Triggers are cleared from the final image after being executed. In other words they are not inherited by “grand-children” builds.
4.觸發器在執行後從最終映象中清除。換句話說,它們不是“孫子代”版本所繼承的。
For example you might add something like this:
例如,您可以新增以下內容:
ONBUILD ADD . /app/src
ONBUILD RUN /usr/local/bin/python-build --dir /app/src
Warning
Chaining
ONBUILD
instructions usingONBUILD ONBUILD
isn’t allowed.
警告
不允許使用
ONBUILD ONBUILD
連結ONBUILD
指令。
Warning
The
ONBUILD
instruction may not triggerFROM
orMAINTAINER
instructions.
警告
ONBUILD
指令可能不會觸發FROM
或MAINTAINER
指令。
總結
介紹了 Dockerfile 中 ONBUILD 指令的用法和注意事項。