无法在目录名称中包含空格的Dockerfile中执行RUN mkdir

时间:2018-08-30 23:59:18

标签: docker dockerfile

我要在构建docker映像并使用以下命令时在C驱动器中创建一个文件

RUN mkdir "C:\Program Files\Microsoft Passport RPS"

但是会引发错误:

Step 6/6 : RUN mkdir "C:\Program Files\Microsoft Passport RPS"
 ---> Running in ab58c6f2948d
[91mmkdir : A positional parameter cannot be found that accepts argument 'Files\Microsoft'.
At line:1 char:76
+ ... e = 'SilentlyContinue'; mkdir C:\Program Files\Microsoft Passport RPS
[0m[91m+                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[0m[91m    + CategoryInfo          : InvalidArgument: (:) [mkdir], ParentContainsErro 
[0m[91m   rRecordException
+ FullyQualifiedErrorId : PositionalParameterNotFound,mkdir

尝试以上命令使用正斜杠和反斜杠。

我尝试了以下多个其他命令:

ENV PATH_WITH_SPACE "C:/Program Files/Microsoft Passport RPS"
RUN mkdir $[PATH_WITH_SPACE]

RUN mkdir ["C:\Program Files\Microsoft Passport RPS"]

您能通过适当的命令帮助我吗?

3 个答案:

答案 0 :(得分:0)

您必须逃脱空间,例如:

RUN mkdir "C:\Program Files\Microsoft\ Passport\ RPS"

或使用JSON format

RUN ["mkdir", "C:\\Program Files\\Microsoft\ Passport\ RPS"]
使用JSON format进行

note:才能避免反斜杠。在Windows中,反斜杠是路径分隔符。

答案 1 :(得分:0)

我们可以使用PowerShell命令创建带空格的文件夹。

尝试一下:

RUN powershell -Command New-Item -Path 'C:\Program Files\Microsoft Passport RPS' -ItemType Directory

它有效!!!

答案 2 :(得分:0)

遇到相同问题。没有答案对我有用。 我终于通过使用`

转义空格来使其工作
RUN mkdir "C:\Program` Files\Microsoft` Passport` RPS"
COPY . "C:\Program` Files\Microsoft` Passport` RPS"

另一种方法是使用Shell,并明确声明转义

While the JSON form is unambiguous and does not use the un-necessary cmd.exe, it does require more verbosity through double-quoting and escaping. The alternate mechanism is to use the SHELL instruction and the shell form, making a more natural syntax for Windows users, especially when combined with the escape parser directive

# escape=`

FROM microsoft/nanoserver
SHELL ["powershell","-command"]
RUN New-Item -ItemType Directory C:\Example
ADD Execute-MyCmdlet.ps1 c:\example\
RUN c:\example\Execute-MyCmdlet -sample 'hello world'