转义
# escape=\ (backslash)
或者
# escape=` (backtick)
在Dockerfile
中escape
指令用来设置转义字符。如果没有指定的话,默认的转义字符是\
。
转义字符被用来转义字符和转义换行符,这样可以允许一条Dockerfile
的指令放在几行里。请注意,无论在Dockerfile是否包含转义指令,在RUN
指令当中转义是不会生效的,除非在行的结尾。
因为在Windows
当中\
被当做目录分隔符在Windows
当中把转义字符设为是很有用的。在[Windows PowerShell](https://technet.microsoft.com/en-us/library/hh847755.aspx)中也使用
。
考虑下面的例子,在Windows
当中他会失败但不容易被看出来。在第二行最后的第二个\
会被当做新一行的转义字符,而不是第一个\
的转义目标。类似的,在第三行末尾的\
,假设他被当做指令的一部分,但其实他被当做了换行转义。这个dockerfile的结果就是第二行的指令和第三行的指令合起来被当做了一条指令。
FROM microsoft/nanoserver
COPY testfile.txt c:\\
RUN dir c:\
结果
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>