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 -
- Source path is incorrect
- Incorrect file
- Incorrect docker build image name
- .dockerignore
- While working on Java project using Gradle Build Tool
- Wrong build context causing - ADD failed : No such file/Directory while building docker image
- Permission on the destination and source path
Scenario 1: Source path is incorrect
Example Docker copy command
1COPY build/lib/* /deployments/lib/
Resolution
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/
Resolution
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/
Resolution
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/
7. Permission on the destination and source path
It might be possible that your dockerfile
or docker-compose.yaml
is correct but still you are getting error docker copy failed. In such cases, it is worth checking the permission(read, write, execute) of the source directory as well as destination direction
Here are tips for checking the permission -
-
Souce Directory: Take an example you are trying to copy the content from the path ex.
/etc/home/my_source
. So go to the directorymy_source
and check the permission if the user has aread
permission or not. -
Destination Directory: Here also let us assume that you are trying to copy the content to the directory
/etc/destination/my_destination
. So go to the directorymy_destination
and check the permission. You should have awrite
permission on that directory otherwise your docker copy command will fail.
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
Posts in this Series
- (docker run -d) Why Does a Docker Container Stop Automatically?
- Attach and detach from Docker's process?
- How I Change Name of My Docker Repository and Rename Images?
- How to set-up Cron Jobs in Docker Containers?
- 4 Ways to copy file from localhost to docker container
- Multiple commands execution in Docker Compose?
- How to push Docker Images to AWS ECR(Elastic Container registry)?
- How to Copy Docker images from one host to another host?
- What is persistent storage in docker and how to manage it
- Docker Installation on MacOS, Linux and Windows
- Docker - ADD, Update, Export Environment variable
- How to fix-Docker docker failed to compute cache key not found
- How to fix docker driver failed programming external connectivity on endpoint webserver?
- How to fix docker error executable file not found in $PATH?
- How to expose port on live containers?
- How to expose multiple ports with Docker?
- How to restart single docker container within multiple docker container?
- How to edit file within Docker container or edit a file after I shell into a Docker container?
- How to fix Error starting docker service Unit not found?
- How to remove old, unused images of Docker?
- How to fix docker error invalid reference format error?
- How to fix requested access to the resource is denied?
- How to fix Docker error cannot delete docker container conflict unable to remove repository reference?
- How to fix docker error no space left on device?
- How to connect localhost from docker container?
- Docker COPY vs Docker ADD?
- 6 Ways to fix - Got permission denied while trying to connect to the Docker daemon socket?
- 6 Ways to fix – Docker COPY failed: stat no source files were specified