Software⏱️ 3 min read📅 2026-06-11

How to Fix: "ERROR: cannot copy to non-directory" when copying to WORKDIR?

Docker copy error fix

Quick Answer: The issue is caused by the WORKDIR instruction being used as a destination in the COPY command. Instead, use the dot (.) as the source directory for the COPY command.

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

  1. Step 1: Replace the WORKDIR . with WORKDIR /tmp or another valid directory that exists on the host machine.
  2. Step 2: Update the Dockerfile to reflect the new WORKDIR directive.
  3. Step 3: Try rebuilding the Docker image with the updated Dockerfile.

Use a different COPY command

  1. Step 1: Replace the second COPY command ./* ./ with a different destination directory, such as ./dest.
  2. Step 2: Update the Dockerfile to reflect the new COPY command.
  3. 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.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions