【Docker】Dockerfile 之 FROM


參考教程:https://docs.docker.com/engine/reference/builder/

環境

  1. virtual box 6.1
  2. centos 7.8
  3. docker 19.03

FROM

FROM [--platform=<platform>] <image> [AS <name>]

Or

FROM [--platform=<platform>] <image>[:<tag>] [AS <name>]

Or

FROM [--platform=<platform>] <image>[@<digest>] [AS <name>]

The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with a FROM instruction. The image can be any valid image – it is especially easy to start by pulling an image from the Public Repositories.

FROM 指令初始化一個新的構建階段,並為后續指令設置基礎鏡像。因此,有效的 Dockerfile 必須以 FROM 指令開頭。該鏡像可以是任何有效的鏡像–很容易通過從Public Repositories 拉取一個鏡像。

  • ARG is the only instruction that may precede FROM in the Dockerfile. See Understand how ARG and FROM interact.

  • ARG 是唯一可以在 Dockerfile 中的 FROM 之前的指令。請參閱了解ARG和FROM之間的交互方式

  • FROM can appear multiple times within a single Dockerfile to create multiple images or use one build stage as a dependency for another. Simply make a note of the last image ID output by the commit before each new FROM instruction. Each FROM instruction clears any state created by previous instructions.

  • FROM 可以在單個 Dockerfile 中多次出現,以創建多個鏡像或將一個構建階段用作對另一構建階段的依賴。只需在每條新的 FROM 指令之前記錄一次提交輸出的最后一個鏡像 ID。每條 FROM 指令清除由先前指令創建的任何狀態。

  • Optionally a name can be given to a new build stage by adding AS name to the FROM instruction. The name can be used in subsequent FROM and COPY --from=<name> instructions to refer to the image built in this stage.

  • 可以選擇在 FROM 指令中添加 AS 名稱,從而為新的構建階段指定名稱。該名稱可以在后續的 FROMCOPY --from = <name> 指令中使用,以引用此階段構建的鏡像。

  • The tag or digest values are optional. If you omit either of them, the builder assumes a latest tag by default. The builder returns an error if it cannot find the tag value.

  • tagdigest 值是可選的。如果您忽略其中任何一個,則默認情況下,構建器將采用 latest 標簽。如果構建器找不到 tag 值,則返回錯誤。

The optional --platform flag can be used to specify the platform of the image in case FROM references a multi-platform image. For example, linux/amd64, linux/arm64, or windows/amd64. By default, the target platform of the build request is used. Global build arguments can be used in the value of this flag, for example automatic platform ARGs allow you to force a stage to native build platform (--platform=$BUILDPLATFORM), and use it to cross-compile to the target platform inside the stage.

可選的 --platform 標志可用於指定鏡像的平台,以防萬一 ·FROM· 引用了多平台鏡像。例如,linux/amd64linux/arm64windows/amd64。默認情況下,使用構建請求的目標平台。可以在此標志的值中使用全局構建參數,例如 automatic platform ARGs 允許您將階段強制為本機構建平台(--platform = $BUILDPLATFORM),並使用該平台將其交叉編譯到階段內部的目標平台。

Understand how ARG and FROM interac

FROM instructions support variables that are declared by any ARG instructions that occur before the first FROM.

FROM 指令支持由出現在第一個 FROM 之前的任何 ARG 指令聲明的變量。

ARG  CODE_VERSION=latest
FROM base:${CODE_VERSION}
CMD  /code/run-app

FROM extras:${CODE_VERSION}
CMD  /code/run-extras

An ARG declared before a FROM is outside of a build stage, so it can’t be used in any instruction after a FROM. To use the default value of an ARG declared before the first FROM use an ARG instruction without a value inside of a build stage:

FROM 之前聲明的 ARG 在構建階段之外,因此不能在 FROM 之后的任何指令中使用。要使用在第一個 FROM 之前聲明的 ARG 的默認值,請使用在構建階段內部不帶值的 ARG 指令:

ARG VERSION=latest
FROM busybox:$VERSION
ARG VERSION
RUN echo $VERSION > image_version

總結

介紹了 Dockerfile 中 FROM 指令的使用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM