Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 333
ellockie Avatar asked Jun 03 '19 02:06

ellockie


People also ask

What is && in Dockerfile?

Each RUN command in a Dockerfile creates a new layer to the Docker image. In general, each layer should try to do one job and the fewer layers in an image the easier it is compress. This is why you see all these '&& 's in the RUN command, so that all the shell commands will take place in a single layer.

What does Workdir in Dockerfile do?

The WORKDIR command is used to define the working directory of a Docker container at any given time. The command is specified in the Dockerfile. Any RUN , CMD , ADD , COPY , or ENTRYPOINT command will be executed in the specified working directory.

What is difference between ADD and copy in Dockerfile?

COPY is a docker file command that copies files from a local source location to a destination in the Docker container. ADD command is used to copy files/directories into a Docker image. It only has only one assigned function. It can also copy files from a URL.

What is ADD command in Dockerfile?

Docker ADD Command The command copies files/directories to a file system of the specified container. The basic syntax for the ADD command is: ADD <src> … < dest> It includes the source you want to copy ( <src> ) followed by the destination where you want to store it ( <dest> ).


1 Answers

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]/"]
like image 60
atline Avatar answered Sep 23 '22 09:09

atline