Docker - cannot copy to non-directory: /var/lib/docker/overlay2/xw77p2bxfkhhnwqs5umpl7cbi/merged/app/.git

Viewed 5441

I am trying to build a docker image on my windows machine and I keep getting this error:

[+] Building 2.1s (12/15)
 => [internal] load build definition from Dockerfile                                                                                                                                                       0.0s 
 => => transferring dockerfile: 538B                                                                                                                                                                       0.0s 
 => [internal] load .dockerignore                                                                                                                                                                          0.0s 
 => => transferring context: 35B                                                                                                                                                                           0.0s 
 => [internal] load metadata for docker.io/library/node:alpine                                                                                                                                             1.0s 
 => [ 1/11] FROM docker.io/library/node:alpine@sha256:6b56197d33a56cd45d1d1214292b8851fa1b91b2ccc678cee7e5fd4260bd8fae                                                                                     0.0s 
 => [internal] load build context                                                                                                                                                                          1.0s 
 => => transferring context: 15.72kB                                                                                                                                                                       1.0s 
 => CACHED [ 2/11] WORKDIR /app                                                                                                                                                                            0.0s 
 => CACHED [ 3/11] COPY package.json .                                                                                                                                                                     0.0s 
 => CACHED [ 4/11] COPY tsconfig.json .                                                                                                                                                                    0.0s 
 => CACHED [ 5/11] COPY swagger.yaml .                                                                                                                                                                     0.0s 
 => CACHED [ 6/11] COPY services .                                                                                                                                                                         0.0s 
 => CACHED [ 7/11] RUN yarn install                                                                                                                                                                        0.0s 
 => ERROR [ 8/11] ADD . /app                                                                                                                                                                               0.0s 
------
 > [ 8/11] ADD . /app:
------
cannot copy to non-directory: /var/lib/docker/overlay2/xw77p2bxfkhhnwqs5umpl7cbi/merged/app/.git

My Dockerfile is the following:

FROM node:alpine

#Create Directory for the Container
WORKDIR /app

#Copy the package.json and tsconfig.json to work directory
COPY package.json .
COPY tsconfig.json .
COPY swagger.yaml .
COPY services . 

#Install all packages
RUN yarn install

#Copy all other source code to work directory
ADD . /app

#Build sources
RUN yarn run build

#Clean src directory
RUN rm -rf ./src/
RUN rm -rf ./services/src/

#Expose Ports
EXPOSE 3000

#Entry
CMD ["yarn", "start"]

This Dockerfile works on my colleagues' Linux machines but fails on my windows machine.

This is my Docker version

Docker version 20.10.7, build f0df350

Running on windows and using the wsl 2 to interact with it.

But the build also fails using the Windows command prompt.

Thank you in advance for the help!

4 Answers

Just verify the .git is actually a file or folder. Or check for any name conflict between a folder and file.

Seems like you are trying to copy a folder to a file(non-directory).

I have faced a similar issue error: failed to solve: cannot replace to directory /var/lib/docker/overlay2/*/*/folder_a with file. Turns out i have a binary file with a name 'folder_a'.

Deleting the file which matches the folder_name solved the issue for me.

My case Destination directory not created before copy command

RUN mkdir <destination directory>

I also faced a similar issue, when trying to copy only the necessary files of my monorepo. This occurs when your are trying to copy files to a non-existent directory.

COPY package.json .

COPY ./packages/shared-ui/package.json packages/shared-ui
COPY ./packages/shared-ui packages/shared-ui

COPY ./apps/frontend/package.json apps/frontend
COPY ./apps/frontend apps/frontend

To fix this, you first need to create the apps/frontend directory & packages/shared-ui directory before copying over the source files.

COPY package.json .

/* Create these directories first */
RUN mkdir -p packages/shared-ui
RUN mkdir -p apps/frontend

COPY ./packages/shared-ui/package.json packages/shared-ui
COPY ./packages/shared-ui packages/shared-ui

COPY ./apps/frontend/package.json apps/frontend
COPY ./apps/frontend apps/frontend

The -p flag is important to include with mkdir else the command won't work.

Related