參考教程:https://docs.docker.com/engine/reference/builder/
環境
- virtual box 6.1
- centos 7.8
- 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 precedeFROM
in theDockerfile
. See Understand how ARG and FROM interact. -
ARG
是唯一可以在 Dockerfile 中的 FROM 之前的指令。請參閱了解ARG和FROM之間的交互方式。 -
FROM
can appear multiple times within a singleDockerfile
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 newFROM
instruction. EachFROM
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 theFROM
instruction. The name can be used in subsequentFROM
andCOPY --from=<name>
instructions to refer to the image built in this stage. -
可以選擇在
FROM
指令中添加 AS 名稱,從而為新的構建階段指定名稱。該名稱可以在后續的FROM
和COPY --from = <name>
指令中使用,以引用此階段構建的鏡像。 -
The
tag
ordigest
values are optional. If you omit either of them, the builder assumes alatest
tag by default. The builder returns an error if it cannot find thetag
value. -
tag
或digest
值是可選的。如果您忽略其中任何一個,則默認情況下,構建器將采用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/amd64
,linux/arm64
或 windows/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 指令的使用。