cannot execute RUN mkdir in a Dockerfile with space in directory name

Viewed 6683

I want to create a file in C drive while building docker image and using command as below

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

but it throws error:

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

Tried above command with forward as well as back slash.

I have tried multiple other command as below:

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

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

Can you please help me with appropriate command?

3 Answers

Came across same problem. None of the answers worked for me. I finally got it working by escaping space with `

RUN mkdir "C:\Program` Files\Microsoft` Passport` RPS"
COPY . "C:\Program` Files\Microsoft` Passport` RPS"

Another approach is to use Shell, and declare escape explictly

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'

You have to escape the space, some like:

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

Or using the JSON format:

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

note: using JSON format is necessary to escape backslashes. This is particularly relevant on Windows where the backslash is the path separator.

We can use PowerShell command to create folder with spaces.

Try this:

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

It Works !!!

Related