COPYing a file in a Dockerfile, no such file or directory?

Docker

Docker Problem Overview


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?

Docker Solutions


Solution 1 - Docker

Do check the .dockerignore file too.

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

Solution 2 - Docker

It is possibly caused by you are referring file1/file2/file3 as an absolute path which is not in build context, Docker only searches the path in the 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 the same name here, no file or directory error is thrown.

Refer to One of the docker issue

Solution 3 - Docker

The COPY instruction in the Dockerfile copies the files in src to the dest folder. Looks like you are either missing the file1, file2 and file3 or trying to build the Dockerfile from the wrong folder.

Refer Dockerfile Doc

Also the command for building the Dockerfile should be something like.

cd into/the/folder/
docker build -t sometagname .

Solution 4 - Docker

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.

Solution 5 - Docker

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.

Solution 6 - Docker

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.

Solution 7 - Docker

Doing a docker build - < Dockerfile or Get-Content .\Dockerfile | Docker build -on PS might not be getting the context. I would recommend you use

Docker build -f .\Path\Dockerfile . 

Solution 8 - Docker

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

Solution 9 - Docker

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.

Solution 10 - Docker

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/

Solution 11 - Docker

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.

Solution 12 - Docker

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]

Solution 13 - Docker

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.

Solution 14 - Docker

For following error,

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

I got it around by restarting docker service.

sudo service docker restart

Solution 15 - Docker

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(...)

Solution 16 - Docker

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.

Solution 17 - Docker

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.

Solution 18 - Docker

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

  3. then run this command

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

  4. after you create your container

    docker run -ti --rm cordova bash

  5. After you will get your directory copied in your container

Solution 19 - Docker

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

Solution 20 - Docker

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGreenGodotView Question on Stackoverflow
Solution 1 - DockerswateekView Answer on Stackoverflow
Solution 2 - DockerPopeyeView Answer on Stackoverflow
Solution 3 - DockeraskbView Answer on Stackoverflow
Solution 4 - DockerTallandtreeView Answer on Stackoverflow
Solution 5 - DockerA StarView Answer on Stackoverflow
Solution 6 - DockerLiHRaMView Answer on Stackoverflow
Solution 7 - DockerN Djel OkoyeView Answer on Stackoverflow
Solution 8 - DockerAnna van den AkkerView Answer on Stackoverflow
Solution 9 - DockerfufonzoView Answer on Stackoverflow
Solution 10 - DockerIsaac PakView Answer on Stackoverflow
Solution 11 - DockerTSlegaitisView Answer on Stackoverflow
Solution 12 - DockerOmrView Answer on Stackoverflow
Solution 13 - DockeralgojohnView Answer on Stackoverflow
Solution 14 - DockerVineethView Answer on Stackoverflow
Solution 15 - DockermetadataView Answer on Stackoverflow
Solution 16 - DockerlinehrrView Answer on Stackoverflow
Solution 17 - DockerraspberryView Answer on Stackoverflow
Solution 18 - DockerWalterwhitesView Answer on Stackoverflow
Solution 19 - DockerJose SolorzanoView Answer on Stackoverflow
Solution 20 - DockerSkyView Answer on Stackoverflow