How to Fix: "ERROR: cannot copy to non-directory" when copying to WORKDIR?
Docker copy error fix
📋 Table of Contents
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.
❓ Frequently Asked Questions
🛠️ Related Fixes
How to Fix: Pc crashes shortly after launching game (rainbow
Fix Pc crashes shortly after launching game (rainbow six siege). Compl
How to Fix: Installing an APK on a locked down phone
Installing an APK on a locked down phone: Try using a rooted device, e
How to Fix: New PC build- no signal and no clue
Fix New PC build- no signal and no clue. Complete troubleshooting guid