Docker copy error fix
The Problem
The 'ERROR: cannot copy to non-directory' issue occurs when trying to copy files into the WORKDIR, which is set to '.', indicating the current directory. This error affects Docker image builds and can be frustrating for developers. The primary reason for this error is that the WORKDIR directive sets the working directory in the Dockerfile, but it does not create the directory if it does not exist.This issue can also be caused by a mismatch between the expected destination and the actual location of the files being copied. In this case, the error message indicates that the destination '/var/lib/docker/overlay2/cytt3i5m23b6ce9iciljwb3c7/merged/lib' is not a directory, which prevents Docker from copying files into it.
🛑 Root Causes of the Error
The primary reason for this error is that the WORKDIR directive sets the working directory in the Dockerfile, but it does not create the directory if it does not exist. This means that when Docker tries to copy files into the WORKDIR, it checks if the directory exists and throws an error if it does not.Another alternative reason for this error is a mismatch between the expected destination and the actual location of the files being copied. In this case, the error message indicates that the destination '/var/lib/docker/overlay2/cytt3i5m23b6ce9iciljwb3c7/merged/lib' is not a directory, which prevents Docker from copying files into it.
✅ Best Solutions to Fix It
Change the WORKDIR to a valid directory
Step 1: Replace the WORKDIR . with WORKDIR /tmp or another valid directory that exists on the host machine.Step 2: Update the Dockerfile to reflect the new WORKDIR directive.Step 3: Try rebuilding the Docker image with the updated Dockerfile.
Use a different COPY command
Step 1: Replace the second COPY command ./* ./ with a different destination directory, such as ./dest.Step 2: Update the Dockerfile to reflect the new COPY command.Step 3: Try rebuilding the Docker image with the updated Dockerfile.
💡 Conclusion
To resolve the 'ERROR: cannot copy to non-directory' issue when copying to WORKDIR, change the WORKDIR directive to a valid directory that exists on the host machine, or use a different COPY command with a valid destination directory. By following these steps, you should be able to successfully build your Docker image.
Full step-by-step guide with screenshots: Read the complete fix here
Found this helpful? Check out more verified tech fixes at TechFixDocs













