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

6

18

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!

Pape answered 21/6, 2021 at 22:49 Comment(6)
Are you using docker for windows ?Gausman
@abhishekphukan Yes, I am using Docker Desktop for windowsPape
There is a WSL setting that needs to be enabled. Did you check if that is okay? learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-containersGausman
@abhishekphukan I also tried to build another project that used an identical Dockerfile and it worked. Docker and wsl were working well, just not for this one thing...Pape
Did the other dockerfile also had a .git folder being copied ?Gausman
Also you may try replacing the ADD command with COPY command if you don’t have anything that needs to be extractedGausman
H
6

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.

Hathorn answered 14/7, 2022 at 20:56 Comment(0)
M
1

My case Destination directory not created before copy command

RUN mkdir <destination directory>
Memorialize answered 19/5, 2022 at 9:57 Comment(0)
H
1

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.

Herrle answered 15/8, 2022 at 0:55 Comment(0)
E
0

I needed to dockerize the frontend project and I had to "COPY . ." in the Dockerfile. In my case this error was thrown while copying the node_modules directory into image. I created the .dockerignore file at the root project level. Then I added node_modules to it. Problem solved.

Escape answered 10/8, 2023 at 7:28 Comment(0)
H
0

I had this same error from doing COPY * . and it was not fixed by switching COPY to ADD like this: ADD * .

I found out that by changing the * to a ., the error was fixed. My new command was:

COPY . .

Which copied everything from the parent context to my current workdir.

Heeling answered 8/7 at 14:53 Comment(0)
S
-1

I had a similar issue with the COPY command and resolved it by adding the directory name in the destination path as suggested in https://sumito.jp/2021/08/04/cannot-copy-to-non-directory-var-lib-docker-overlay2/

Sutler answered 19/10, 2021 at 4:50 Comment(1)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewSculpt

© 2022 - 2024 — McMap. All rights reserved.