1. 程式人生 > 實用技巧 ># 【Docker】解析器指令之 escape

# 【Docker】解析器指令之 escape

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

環境

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

escape

# escape=\ (backslash)

Or

# escape=` (backtick)

The escape directive sets the character used to escape characters in a Dockerfile. If not specified, the default escape character is \.

escape 指令設定用於轉義 Dockerfile

中的字元的字元。如果未指定,則預設轉義字元為 \

The escape character is used both to escape characters in a line, and to escape a newline. This allows a Dockerfile instruction to span multiple lines. Note that regardless of whether the escape parser directive is included in a Dockerfile, escaping is not performed in a RUN

command, except at the end of a line.

轉義字元用於轉義一行中的字元和轉義換行符。這允許 Dockerfile 指令跨越多行。請注意,無論 Dockerfile 中是否包含 escape 解析器指令,都不會在 RUN 命令中執行轉義,除非在行末。

Setting the escape character to ` is especially useful on Windows, where \ is the directory path separator. ` is consistent with Windows PowerShell.

在 Windows 上將轉義符設定為 `

特別有用,其中 \ 是目錄路徑分隔符。 `Windows PowerShell 一致。

Consider the following example which would fail in a non-obvious way on Windows. The second \ at the end of the second line would be interpreted as an escape for the newline, instead of a target of the escape from the first \. Similarly, the \ at the end of the third line would, assuming it was actually handled as an instruction, cause it be treated as a line continuation. The result of this dockerfile is that second and third lines are considered a single instruction:

考慮以下示例,該示例將在 Windows 上以非明顯的方式失敗。第二行末尾的第二個 \ 將被解釋為換行符的轉義,而不是第一個 \ 的轉義目標。類似地,假設實際將其作為指令處理,則第三行末尾的 \ 會導致將其視為行的延續。該 dockerfile 的結果是第二和第三行被視為一條指令:

FROM microsoft/nanoserver
COPY testfile.txt c:\\
RUN dir c:\

Results in:

PS C:\John> docker build -t cmd .
Sending build context to Docker daemon 3.072 kB
Step 1/2 : FROM microsoft/nanoserver
 ---> 22738ff49c6d
Step 2/2 : COPY testfile.txt c:\RUN dir c:
GetFileAttributesEx c:RUN: The system cannot find the file specified.
PS C:\John>

One solution to the above would be to use / as the target of both the COPY instruction, and dir. However, this syntax is, at best, confusing as it is not natural for paths on Windows, and at worst, error prone as not all commands on Windows support / as the path separator.

上面情況的一種解決方案是將 / 用作 COPY 指令和 dir 的目標。但是,這種語法充其量是令人困惑的,因為 Windows 上的路徑並不自然,更糟糕的是容易出錯,因為並非 Windows 上的所有命令都支援 / 作為路徑分隔符。

By adding the escape parser directive, the following Dockerfile succeeds as expected with the use of natural platform semantics for file paths on Windows:

通過新增 escape 解析器指令,以下 Dockerfile 可以通過在 Windows 上為檔案路徑使用自然平臺語義而成功完成:

# escape=`

FROM microsoft/nanoserver
COPY testfile.txt c:\
RUN dir c:\

Results in:

PS C:\John> docker build -t succeeds --no-cache=true .
Sending build context to Docker daemon 3.072 kB
Step 1/3 : FROM microsoft/nanoserver
 ---> 22738ff49c6d
Step 2/3 : COPY testfile.txt c:\
 ---> 96655de338de
Removing intermediate container 4db9acbb1682
Step 3/3 : RUN dir c:\
 ---> Running in a2c157f842f5
 Volume in drive C has no label.
 Volume Serial Number is 7E6D-E0F7

 Directory of c:\

10/05/2016  05:04 PM             1,894 License.txt
10/05/2016  02:22 PM    <DIR>          Program Files
10/05/2016  02:14 PM    <DIR>          Program Files (x86)
10/28/2016  11:18 AM                62 testfile.txt
10/28/2016  11:20 AM    <DIR>          Users
10/28/2016  11:20 AM    <DIR>          Windows
           2 File(s)          1,956 bytes
           4 Dir(s)  21,259,096,064 bytes free
 ---> 01c7f3bef04f
Removing intermediate container a2c157f842f5
Successfully built 01c7f3bef04f
PS C:\John>

總結

介紹了 Dockerfile 指令解析器的 escape 用法。