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?
You can use the docker cp command to copy the file. The first path (Source) is the path in the Docker Container and the second one is the path inside your Local System (Destination).
Obtain the name or id of the Docker container. Issue the docker cp command and reference the container name or id. The first parameter of the docker copy command is the path to the file inside the container. The second parameter of the docker copy command is the location to save the file on the host.
Another way to copy files to and from Docker containers is to use a volume mount. This means we make a directory from the host system available inside the container. The command above runs a grafana container and mounts the /tmp directory from the host machine as a new directory inside the container named /transfer.
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.
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 /
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With