42

I am trying to copy a single file from the root of the build context into a newly created directory in a docker image.

The Dockerfile that I am using is as follows:

FROM debian:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY test.txt /usr/src/app

The test.txt file is a simple ASCII text file as follows:

$ cat test.txt 
This is a test

However, when I build this image I get an image stating that the destination path is not a directory.

$ docker build .
Sending build context to Docker daemon 4.608 kB
Sending build context to Docker daemon 
Step 0 : FROM debian:latest
 ---> 9a61b6b1315e
Step 1 : RUN mkdir -p /usr/src/app
 ---> Using cache
 ---> 86bd44a8776e
Step 2 : WORKDIR /usr/src/app
 ---> Using cache
 ---> ed6771adc681
Step 3 : ADD test.txt /usr/src/app
stat /var/lib/docker/devicemapper/mnt/ee5b9b7029f2adf27d332cbb0d98d6ad9927629a7569fd2d9574cb767b23547b/rootfs/usr/src/app/test.txt: not a directory

I have tried using multiple combinations of docker versions, base images, and distributions with docker installed. I have also tried using ADD instead of COPY but the result is the same.

I am currently trying this on CentOS 7 with the following docker version installed:

Client version: 1.7.1
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 786b29d
OS/Arch (client): linux/amd64
Server version: 1.7.1
Server API version: 1.19
Go version (server): go1.4.2
Git commit (server): 786b29d
OS/Arch (server): linux/amd64

What I have noticed is that I can copy a directory, but not a file. For example, if I create a "test" directory in the build context root and put test.txt inside the test directory then I can copy the directory successfully:

FROM debian:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY test /usr/src/app

Note that in the above Dockerfile I copy the entire ./test directory rather than just ./test.txt. This build successfully.

The Docker documentation has the following sample use case for the COPY instruction which is similar to what I am trying to do:

COPY hom?.txt /mydir/

Can anyone point out what I am doing wrong? How can I go about copying a single file?

2 Answers 2

50

As stated in the Dockerfile documentation:

  • If <src> is any other kind of file, it is copied individually along with its metadata. In this case, if <dest> ends with a trailing slash /, it will be considered a directory and the contents of <src> will be written at <dest>/base(<src>).

  • If <dest> does not end with a trailing slash, it will be considered a regular file and the contents of <src> will be written at <dest>.

Thus, you have to write COPY test.txt /usr/src/app/ with a trailing /.

2
  • That smells like rsync. I always have to double check that. Jul 4, 2018 at 10:27
  • For me, it worked also without the trailing line (Docker 18.09.5). COPY still is something tricky (have a look on all issues on SO and GitHub).
    – ivanleoncz
    Apr 28, 2019 at 19:31
0

For me, I was adding a path based on the docker-compose.yml instead of the Dockerfile:

File structure

.
..
/docker-compose.yml
/docker/
/docker/php/
/docker/php/Dockerfile
/docker/php/my_file

and in / I was running docker-compose up --build.

instead of COPY docker/php/my_file /some/location I needed to run COPY my_file /some/location

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.