6 Ways to fix – Docker COPY failed: stat no source files were specified
This error I would consider one of the most basic errors which any developer can face during the initial days of Docker learning and the error Docker COPY failed: stat no source files were specified or docker copy failed no such file or directory is caused when docker copy command is not able to copy files from the build context into your image.
But in case if you are using DOCKER ADD instead of DOCKER COPY command then you might see little different error message .i.e. ADD failed : No such file/Directory while building docker image
Both error messages points to the same problem. Here in this article I will explain 6 possible scenario where you can face this issue -
Scenario 1: Source path is incorrect
Example Docker copy command
1COPY build/lib/* /deployments/lib/
Look carefully at Source path - “build/lib/*", it might be possible this path does not exist.
Fix the source path and the re-run the docker build command
Scenario 2: Incorrect file
Example Docker copy command
1COPY build/lib/hello-world.txt /deployments/lib/
It might be possible you do not have “hello-world.txt” available at the source path “build/lib/"
To fix the issue place “hello-world.txt” at path “build/lib/"
Scenario 3: Incorrect docker build image name
Here is the sample docker where we didn’t specify the build image name
1FROM registry.access.redhat.com/ubi8/ubi-minimal:8.1 2COPY build/lib/hello-world.txt /deployments/lib/
Here we didn’t specify the build image name. To fix this we need to assign the “build” as image name -
1FROM registry.access.redhat.com/ubi8/ubi-minimal:8.1 as build 2COPY build/lib/hello-world.txt /deployments/lib/
Scenario 4: .dockerignore
One more possible cause of COPY failed: no source files were specified is .dockerignore file present in your workspace
Look for .dockerignore file because of docker’s CLI (command line interface) it sends context to the docker daemon to load the .dockerignore file.
If the .dockerignore file exist in the context than it might be possible there is exclude entry to ignore the build directory
1# ignore or exclude build directory 2*/build* 3*/*/build*
In both the above cases build directory will be excluded during the docker-compose or docker run.
Scenario 5: While working on Java project using Gradle Build Tool
Coming from java background I most of the time work with Gradle as my preferred build tool. I faced docker copy failed exception many times during my day to day development.
After spending some hefty amount of time in troubleshooting I noticed the root cause and I personally felt its my bad habits on how I used gradle build tools with my Java development.
So how does this issue happens ?
Running Clean task - If you have ran gradle->clean task and immediately trying to run docker-compose build after that, then I am very confident that you will face this exception because gradle->clean task will delete/remove build directory from your workspace and eventually you will get this issue.
Here is my suggestions to avoid this exception -
Run Clean - If you want to clean the previous build then go ahead and run the gradle->clean task. But do not forget to run the Build tasks after the clean task.
I hope the above 5 solutions will help you to fix the error COPY failed: no source files were specified
Scenario 6: Wrong build context causing - ADD failed : No such file/Directory while building docker image
In this scenario you are using DOCKER ADD instead of DOCKER COPY but you end up getting an error ADD failed : No such file/Directory while building docker image
Let’s take an example to understand this issue with a very simple directory structure with two files first.txt and second.txt -
1Project/ 2 |--/home 3 |--src/first.txt 4 |--data/second.txt 5Dockerfile
Here is our sample dockerfile which is causing this error
1 ADD first.txt /destination/ 2 ADD second.txt /destination/
So I am assuming you will build your docker image as follow -
1 docker build -t myimage .
Problem : To locate this issue first look how the docker build command is ending, its ending with dot(.) which means docker build command will look for the first.txt and second.txt inside the directory /Project instead of /Project/home/src, /Project/home/data
To fix this issue we need to update the Dockerfile to pick first.txt and second.txt from correct source directory.
Here is the correct version of Dockerfile where we have mentioned the exact source location to pick first.txt and second.txt
1 ADD /home/src/first.txt /destination/ 2 ADD /home/src/second.txt /destination/
I hope this helps you to fix your issue with
Docker ADD command and do not forget to check your project directory structure.
Learn more about c- Docker COPY vs Docker ADD
If you want to learn more about cloud framework then follow my article - Getting Started with Quarkus | Quarkus Tutorial