1. 程式人生 > 實用技巧 >【Docker】Dockerfile 格式

【Docker】Dockerfile 格式

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

環境

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

格式

Here is the format of the Dockerfile:

這是 Dockerfile 的格式:

# Comment
INSTRUCTION arguments

The instruction is not case-sensitive. However, convention is for them to be UPPERCASE to distinguish them from arguments more easily.

指令不區分大小寫。但是,按約定將它們大寫,以便更輕鬆地將它們與引數區分開。

Docker runs instructions in a Dockerfile in order. A Dockerfile must begin with a FROM instruction. This may be after parser directives, comments, and globally scoped ARGs. The FROM instruction specifies the Parent Image from which you are building. FROM may only be preceded by one or more ARG

instructions, which declare arguments that are used in FROM lines in the Dockerfile.

Docker 依次執行在 Dockerfile 中的指令。Dockerfile 必須以 FROM 指令開頭。這可能在parser directives註釋和全域性範圍內的 ARG 之後。FROM 指令指定基礎映象。在 FROM 之前只能有一個或多個 ARG 指令,這些指令宣告在 Dockerfile 的 FROM 行中使用的引數。

Docker treats lines that begin with # as a comment, unless the line is a valid

parser directive. A # marker anywhere else in a line is treated as an argument. This allows statements like:

除非該行是有效的 parser directives,否則 Docker 會將以 號開頭的行作為註釋。一行中其他任何地方的 標記都被視為引數。這允許如下語句:

# Comment
RUN echo 'we are running some # of cool things'

Comment lines are removed before the Dockerfile instructions are executed, which means that the comment in the following example is not handled by the shell executing the echo command, and both examples below are equivalent:

在執行 Dockerfile 指令之前會刪除註釋行,這意味著以下示例中的註釋不會由執行echo 命令的 shell 處理,並且以下兩個示例是等效的:

RUN echo hello \
# comment
world
RUN echo hello \
world

Line continuation characters are not supported in comments.

註釋中不支援換行符。

空格注意事項

For backward compatibility, leading whitespace before comments (#) and instructions (such as RUN) are ignored, but discouraged. Leading whitespace is not preserved in these cases, and the following examples are therefore equivalent:

為了向後相容,註釋()和指令(例如 RUN)之前的空格將被忽略,但不鼓勵使用。在這些情況下,不保留前導空格,因此以下示例是等效的:

        # this is a comment-line
    RUN echo hello
RUN echo world
# this is a comment-line
RUN echo hello
RUN echo world

Note however, that whitespace in instruction arguments, such as the commands following RUN, are preserved, so the following example prints ` hello world` with leading whitespace as specified:

但是請注意,指令引數中的空格(例如 RUN 命令之後的空格)被保留,因此以下示例使用指定的前導空格列印` hello world`:

RUN echo "\
     hello\
     world

總結

介紹了 Dockerfile 的格式。