ellockie
ellockie

Reputation: 4268

Dockerfile: COPY / ADD with space character in path (Windows)

In Windows 10 (cmd) I'm trying to copy a file from a subfolder containing a space character in its name.

  1. First I tried to use quotation marks:

    FROM jfloff/alpine-python:2.7
    COPY "Folder 1/File.txt" "Dir 1"
    

    Error message:

    failed to process "\"Folder": unexpected end of statement while looking for matching double-quote

  2. "JSON" format (skipped the first line):

    COPY ["Folder 1/File.txt" "Dir 1"]
    

    Error message:

    failed to process "[\"Folder": unexpected end of statement while looking for matching double-quote

  3. Trying to escape with a single backslash:

    COPY "Folder\ 1/File.txt" "Dir\ 1"
    

    Error message:

    failed to process "\"Folder\\": unexpected end of statement while looking for matching double-quote

  4. Trying to escape with a double backslash:

    COPY "Folder\\ 1/File.txt" "Dir\\ 1"
    

    Error message:

    failed to process "\"Folder\\\\": unexpected end of statement while looking for matching double-quote

  5. Also tried a suggestion to use %20 instead of space:

    COPY ["Folder%201/File.txt" "Dir%201"]
    

    Error message:

    COPY failed: no source File

  6. Escape character replacement:

    # escape=`
    COPY "Folder` 1/File.txt" "Dir 1"
    

    Error message:

    failed to process "\"Folder`": unexpected end of statement while looking for matching double-quote

  7. The same, but without quotes:

    #escape=`
    COPY Folder` 1/File.txt Dir` 1
    

    Error message:

    COPY failed: stat /var/lib/docker/tmp/docker-builder082039614/Folder: no such file or directory

  8. Method with packing / unpacking using a tar archive (I'm not happy with that idea).

It should be possible, shouldn't it?

Upvotes: 41

Views: 31776

Answers (4)

Dipayan
Dipayan

Reputation: 1

Using Square Brackets should solve this problem. Example:

COPY ["[[]File Name]/Folder/Project.csproj", "[File Name]/Folder/"]

Upvotes: 0

Flo
Flo

Reputation: 3127

The solution is the follow: enter image description here

Docker has updated it's COPY command. So you can use brackets and your filenames can have whitespaces. Read more here (official docs)

Upvotes: 9

Stephen Kamau
Stephen Kamau

Reputation: 21

agree it's annoying to get files from docker that have special characters, had similar experience and after hours of try and errors, I came up with this simple solution that I now use since then.

tar/zip the files and then "docker cp" them easily without having to worry about dozens of []'"..

tar files in your container machine:

#zipped
tar czf myfiles.tar.gz file1.txt dir24 file_xyz

#or unzipped
tar -cvf myfiles.tar file1.txt dir24 file_xyz

Then copy them to local directories in your windows machine.

open cmd in windows run

# docker cp ABCD_CONTAINER_NAME:PATH/myfiles.tar.gz d:/TARGETDIR/

check if files have been transferred in d:/TARGETDIR/

Upvotes: 2

atline
atline

Reputation: 31584

Maybe you can use ARG to help you, like this:

Dockerfile:

FROM jfloff/alpine-python:2.7
ARG src="Folder 1/File.txt"
ARG target="Dir 1/"
COPY ${src} ${target}

BTW, a / has to be add at the end of Dir 1 if you treat really want to treat it as a folder.

And, JSON format is also ok, just you miss ,, it should be:

FROM jfloff/alpine-python:2.7
COPY ["Folder 1/File.txt", "Dir 1/"]

Update for your comments:

In official guide, it said:

When copying files or directories that contain special characters (such as [ and ]), you need to escape those paths following the Golang rules to prevent them from being treated as a matching pattern.

So, for your case, it should be:

FROM jfloff/alpine-python:2.7
ARG src="[[]Folder 1]/__SLIM_TEMPLATE.mm"
ARG target="[Folder 1]/"
COPY ${src} ${target}

Or:

FROM jfloff/alpine-python:2.7
COPY ["[[]Folder 1]/__SLIM_TEMPLATE.mm", "[Folder 1]/"]

Upvotes: 48

Related Questions