SoFunction
Updated on 2025-04-14

About the use of COPY files in Dockerfile

question

Usually we will execute the docker build command to create an image in the folder where the Dockerfile file is located.

as follows:

docker build -t ${image_name} .

When creating images in this way, it is important to note that if you use the COPY command in the Dockerfile, you can only have files or folders in the same directory as the Dockerfile file.

If we want COPY files or folders that are not in the same directory as Dockerfile file, an error will be reported.

as follows:

COPY failed: stat xxxx: no such file or directory

Solution 1

The easiest solution is to place all the involved files or folders in the same directory as the Dockerfile, so that the COPY command can find the files or directories.

Solution 2

You do not need to enter the directory where the Docerfile file is located, and use "-f" in "docker build -f xxx/xxx/xxx." to specify the full path to the Dockerfile to solve this problem.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.