Skip to content
Latchkey

Docker "failed to solve: cannot copy to non-directory" in CI

A COPY tried to write into a destination that is a file, not a directory. When copying multiple sources or a directory, the destination must be (or end with / to become) a directory - copying onto an existing file fails.

What this error means

A COPY step fails with failed to solve: cannot copy to non-directory. The destination path already exists as a regular file, or the trailing slash that would make it a directory is missing.

docker
ERROR: failed to solve: cannot copy to non-directory: /app/config
# COPY config/ /app/config   but /app/config already exists as a file

Common causes

Destination exists as a file

An earlier step created /app/config as a file; a later COPY of a directory into the same path cannot turn a file into a directory.

Multiple sources into a non-directory destination

When a COPY has multiple source files, the destination must be a directory (end it with /). A bare filename destination is treated as a file.

How to fix it

Make the destination a directory

End the destination with a slash so COPY treats it as a directory, and avoid an earlier step creating it as a file.

Dockerfile
# multiple sources -> directory destination (trailing slash)
COPY config/ /app/config/
COPY a.txt b.txt /app/data/

Remove or rename the conflicting file first

If a prior step made the path a file, drop it before copying a directory there.

Dockerfile
RUN rm -f /app/config
COPY config/ /app/config/

How to prevent it

  • End directory-destination COPYs with a trailing slash.
  • Do not let an earlier step create a file where a later COPY wants a directory.
  • Copy multiple sources only into directory destinations.

Frequently asked questions

What causes ""cannot copy to non-directory""?
An earlier step created /app/config as a file; a later COPY of a directory into the same path cannot turn a file into a directory.
How do I fix "cannot copy to non-directory"?
End the destination with a slash so COPY treats it as a directory, and avoid an earlier step creating it as a file.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card