How to fix "failed to solve: cannot copy to non-directory" when using Turborepo with Docker

I encountered a weird issue when using Turborepo with Docker. I had a pretty common Dockerfile setup with the RUN turbo prune --scope=sth --docker command.

The prune command generates a "partial monorepo", which means a version of your monorepo that contains only the packages used by the project you're building.

This is useful for reducing the size of the Docker image. During that phase, Turborepo will also copy node_modules to that output directory. If you're using pnpm, you might have noticed that pnpm creates symbolic links in node_modules. Pnpm uses this approach to reuse already installed packages, reducing the size of your app.

However, it seems that Docker doesn't like COPYing a symbolic link and throws an error similar to this:

failed to solve: cannot copy to non-directory: /path/to/node_modules/something/something

To fix this, you just need to ignore the **/node_modules path in your .dockerignore file.

For example:

*.log
*.md
.git
.env
.gitignore
.dockerignore
Dockerfile
 
**/node_modules

Published on January 27, 2025 1 min read