Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

docker

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?

like image 866
GreenGodot Avatar asked Sep 26 '22 15:09

GreenGodot


People also ask

Does copy in Dockerfile create directory?

Docker Dockerfiles COPY InstructionThe COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest> . Multiple <src> resource may be specified but they must be relative to the source directory that is being built (the context of the build).

How does copy work in Dockerfile?

COPY and ADD are both Dockerfile instructions that serve similar purposes. They let you copy files from a specific location into a Docker image. COPY takes in a src and destination. It only lets you copy in a local or directory from your host (the machine-building the Docker image) into the Docker image itself.


2 Answers

Do check the .dockerignore file too.

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

like image 363
swateek Avatar answered Oct 17 '22 07:10

swateek


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

like image 59
Popeye Avatar answered Oct 17 '22 07:10

Popeye