[docker] COPYing a file in a Dockerfile, no such file or directory?

I have a Dockerfile set up in my root (~) folder. The first three lines of my file look like this:

COPY file1 /root/folder/
COPY file2 /root/folder/
COPY file3 /root/folder/

but it returns the following error for each line:

No such file or directory

The files are in the same directory as my Dockerfile and I am running the command docker build - < Dockerfile in the same directory in terminal as well.

What am I doing wrong here exactly?

This question is related to docker

The answer is


I just experienced this problem and none of the suggestions here solved my problem. Turns out I had the wrong line endings in my file, and had to change them to the appropriate line endings. (In this case from CRLF to LF, so Ubuntu 14.04 would recognize the script, which I had been editing on windows.)

I changed the line endings using VSCode, and most code editors should have the option of choosing line endings.

Hope this helps someone.


I ran into this. Copying some directories didn't work. Copying files did. It turned out to be because files contained in .gitignore (not just .dockerignore) are also ignored. See: https://github.com/zeit/now/issues/790


one of the way to don't use stdin and keep the context is:

1) in your Dockerfile, you should add

ADD /your_dir_to_copy /location_in_container

2) after, you should go on the parent of /your_dir_to_copy dir

2) then run this command

sudo docker build . -t (image/name) -f path_of_your_dockerfile/Dockerfile

3) after you create your container

docker run -ti --rm cordova bash

4) After you will get your directory copied in your container


Seems that the commands:

docker build -t imagename .

and:

docker build -t imagename - < Dockerfile2

are not executed the same way. If you want to build 2 docker images from within one folder with Dockerfile and Dockerfile2, the COPY command cannot be used in the second example using stdin (< Dockerfile2). Instead you have to use:

docker build -t imagename -f Dockerfile2 .

Then COPY does work as expected.


Here is the solution and the best practice:

You need to create a resources folder where you can keep all your files you want to copy.

+-- Dockerfile
+-- resources
¦     +-- file1.txt
¦     +-- file2.js

The command for copying files should be specified this way:

COPY resources /root/folder/

where

*resources - your local folder which you created in the same folder where Dockerfile is

*/root/folder/ - folder at your container


Running docker build . -f docker/development/Dockerfile worked, which allows you to run your docker file from a specified directory other than the root of your application.

Use -f or --file to specify the name and location of the Dockerfile.

This happened to me when trying to run the docker file from a different directory.

I had the COPY failed: stat /var/lib/docker/tmp/docker-builder929708051/XXXX: no such file or directory and managed to resolve this by specifying the docker file.

It was docker build docker/development/Dockerfile that caused this issue for me.

I found it strange at first because when i had the Dockerfile in the apps root directory it worked fine. This will help if you want to manage your environment docker files a little better.


I was searching for a fix on this and the folder i was ADD or COPY'ing was not in the build folder, multiple directories above or referenced from /

Moving the folder from outside the build folder into the build folder fixed my issue.


So this has happened a couple of times just recently. As a .Net dev, using VisualStudio I changed my build name from SomeThing to Something as the DLL name but this does not change the .csproj file which stays SomeThing.csproj

The Dockerfile uses linux case-sensitive filenames, so the newly autogenerated Dockerfile was trying to copy Something.csproj which it couldn't find. So manually renaming that file (making it lowercase) got it all working

But...here's a cautionary warning. This filename change on my Windows laptop does not get picked up by Git so the repo source was still SomeThing.csproj on the repo and during the CI/CD process, the Docker build failed for the same reasons...

I had to change the filename directly as a commit on the repo....nasty little workaround but got me going

tl;dr If on Windows O/S check for filename case sensitivity and be aware local file renames do not get picked up as Git change so make sure your repo is also modified if using CI/CD


It is possibly caused by you are referring file1/file2/file3 as an absolute path which is not in build context, Docker only search the path in build context.

E.g. if you use COPY /home/yourname/file1, Docker build interprets it as ${docker build working directory}/home/yourname/file1, if no file with same name here, no file or directory error is thrown.

Refer to One of the docker issue


Do check the .dockerignore file too.

I know this is a very rare case, but I had that file mentioned there.


Some great answers here already. What worked for me, was to move the comments to the next line.

BAD:

WORKDIR /tmp/app/src # set the working directory (WORKDIR), so we can reference program directly instead of providing the full path.
COPY src/ /tmp/app/src/ # copy the project source code

GOOD:

WORKDIR /tmp/app/src
  # set the working directory (WORKDIR), so we can reference program directly instead of providing the full path.
COPY src/ /tmp/app/src/
  # copy the project source code

if you are sure that you did the right thing, but docker still complains, take a look at this issue: https://github.com/moby/moby/issues/27134.
I got burnt by this, and it seems like restarting docker engine service docker restart will just fix this problem.


I know this is old, but something to point out. If you think everything is as its supposed to, check your .gitignore file :)

You might have the folder locally, but if the folder is in your git ignore then its not on the server, which means Docker cannot find that folder as it does not exist.


I feel a little stupid, but my issue was that I was running docker-compose, and my Dockerfile was in a ./deploy subdirectory. My ADD reference needed to be relative to the root of the project, not the Dockerfile.

Changed: ADD ./file.tar.gz /etc/folder/ to: ADD ./deploy/file.tar.gz /etc/folder/

Anyhow, thought I'd post in case someone ran into the same issue.


File not found error with Docker put_archive. I am using the Python API for docker. Docker version 1.12.5, build 7392c3b

docker.errors.NotFound: 404 Client Error: Not Found ("lstat /var/lib/docker/aufs/mnt/39d58e00519ba4171815ee4444f3c43d2c6a7e285102747398f6788e39ee0e87/var/lib/neo4j/certificates: no such file or directory")

I am unable to copy files to a created docker container.

con = cli.create_container(...)
cli.put_archive(...)
cli.start(con['Id'])

If I change the order of operation there is no error and the files are copied exactly to were I want them. So I know my code is working and doing what I want it to do. But its important to copy configuration files to a container before it is started. Coping the files after the start cuases the container to start with a default configuration and not the custom configuration which needs to be copied into place before the container is started. Docker claims that this issue is closed but it is still affecting my application.

This works; Same code different execution order.

con = cli.create_container(...)
cli.start(con['Id'])
cli.put_archive(...)

I had this problem even though my source directory was in the correct build context. Found the reason was that my source directory was a symbolic link to a location outside the build context.

For example my Dockerfile contains the following:

COPY dir1 /tmp

If dir1 is a symbolic link the COPY command is not working in my case.


Previous calls on COPY may be changing the directory.

COPY ./server/package.json ./server      # this passes, but the dest ./server is considered a file

COPY ./server/dist ./server/dist         # error, ./server/dist is not a directory

Add a trailing slash to the first call

COPY ./server/package.json ./server/

Similar and thanks to tslegaitis's answer, after

gcloud builds submit --config cloudbuild.yaml . 

it shows

Check the gcloud log [/home/USER/.config/gcloud/logs/2020.05.24/21.12.04.NUMBERS.log] to see which files and the contents of the
default gcloudignore file used (see `$ gcloud topic gcloudignore` to learn
more).

Checking that log, it says that docker will use .gitignore:

DATE Using default gcloudignore file:
# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
# ...

.gitignore

So I fixed my .gitignore (I use it as whitelist instead) and docker copied the file.

[I added the answer because I have not enough reputation to comment]


For following error,

COPY failed: stat /<**path**> :no such file or directory

I got it around by restarting docker service.

sudo service docker restart