373

I've generated a Dockerfile with Visual Studio. It runs in Visual Studio just fine and now I'm trying to build it from Windows itself (docker build ., and I tried many combinations). Yet I get the following error:

> [build 3/7] COPY [client/client.csproj, client/]:
------
failed to compute cache key: "/client/client.csproj" not found: not found

When I change copy to ./client.csproj it does continue and then I get:

 => ERROR [build 7/7] RUN dotnet build "client.csproj" -c Release -o /app/build                3.3s
------
> [build 7/7] RUN dotnet build "client.csproj" -c Release -o /app/build:
#15 0.652 Microsoft (R) Build Engine version 16.8.3+39993d9d for .NET
#15 0.652 Copyright (C) Microsoft Corporation. All rights reserved.
#15 0.652
#15 1.169   Determining projects to restore...
#15 1.483   All projects are up-to-date for restore.
#15 3.231 CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/client/client.csproj]
#15 3.240
#15 3.240 Build FAILED.
#15 3.240
#15 3.240 CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/client/client.csproj]
#15 3.240     0 Warning (5)
#15 3.240     1 Error (5)
#15 3.240
#15 3.240 Time Elapsed 00:00:02.51
-----
executor failed running [/bin/sh -c dotnet build "client.csproj" -c Release -o /app/build]: exit code: 1

What am I doing wrong? I changed Docker Linux to Windows, changed WSL, and restarted everything.

#See https://aka.ms/containerfastmode to understand how Visua...

FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["client/client.csproj", "client/"]
RUN dotnet restore "client/client.csproj"
COPY . .
WORKDIR "/src/client"
RUN dotnet build "client.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "client.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet', "client.dll"]
2
  • 10
    It's work for me stackoverflow.com/a/63257667/15203500 You have to move your docker file One directory up Commented Feb 13, 2021 at 13:44
  • For anyone tempted to leave yet another answer saying "In my case I used the wrong path"... please don't. Commented Nov 12, 2024 at 16:09

33 Answers 33

530

Check your .dockerignore file. Possible it ignores needed files for copy command and you get failed to compute cache key error.

.dockerignore may be configured to minimize the files sent to docker for performance and security:

* 
!dist/

The first line * disallows all files. The second line !dist/ allows the dist folder

This can cause unexpected behavior:

FROM nginx:latest

# Fails because of * in .dockerignore
# failed to compute cache key: "/nginx.conf.spa" not found: not found
# Fix by adding `!nginx.conf.spa`  to .dockerignore
COPY nginx.conf.spa /etc/nginx/nginx.conf

RUN mkdir /app

# Works because of !dist/ in .dockerignore
COPY dist/spa /app

Belts and suspenders.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeeezz, this is the 3rd time in a few weeks I end up on this solution after making the same mistake. Will I ever learn?
I got caught by this when using nixpacks, I had to add !.nixpacks to my dockerignore, because the nixpack docker image was building my docker image, which used my dockerignore...
how is it so hard to make that specific error/issue more obvious... one little message saying "btw you are trying to add a file which is in your docker ignore" would have saved at least 500 people headaches...
285

The way Visual Studio does it is a little bit odd.

Instead of launching docker build in the folder with the Dockerfile, it launches in the parent folder and specifies the Dockerfile with the -f option.

I was using the demo project (trying to create a minimal solution for another question) and struck the same situation.

Setup for my demo project is

\WorkerService2  ("solution" folder)
   +- WorkerService2.sln
   +- WorkserService2  ("project" folder)
       +- DockerFile
       +- WorkerService2.csproj
       +- ... other program files

So I would expect to go

cd \Workerservice2\WorkerService2
docker build .

But I get your error message.

 => ERROR [build 3/7] COPY [WorkerService2/WorkerService2.csproj, WorkerService2/]                                                                                                                        0.0s
------
 > [build 3/7] COPY [WorkerService2/WorkerService2.csproj, WorkerService2/]:
------
failed to compute cache key: "/WorkerService2/WorkerService2.csproj" not found: not found

Instead, go to the parent directory, with the .sln file and use the docker -f option to specify the Dockerfile to use in the subfolder:

cd \Workerservice2
docker build -f WorkerService2\Dockerfile --force-rm -t worker2/try7 .

docker run -it worker2/try7    

Note the final dot on the docker build command.

For docker the final part of the command is the location of the files that Docker will work with. Usually this is the folder with the Dockerfile in, but that's what's different about how VS does it. In this case the dockerfile is specified with the -f. Any paths (such as with the COPY instruction in the dockerfile) are relative to the location specified. The . means "current directory", which in my example is \WorkerService2.

I got to this stage by inspecting the output of the build process, with verbosity set to Detailed. If you choose Tools / Options / Projects and Solutions / Build and Run you can adjust the build output verbosity, I made mine Detailed.


I think I've worked out why Visual Studio does it this way. It allows the project references in the same solution to be copied in.

If it was set up to do docker build from the project folder, docker would not be able to COPY any of the other projects in the solution in. But the way this is set up, with current directory being the solution folder, you can copy referenced projects (subfolders) into your docker build process.

8 Comments

Gah. It was that last dot that was getting me. I tried all sorts of combinations of the full file or a path without the file, but I either got the file not found issue, or the dreaded docker build requires exactly 1 argument message. Not gonna lie, learning the tiniest new thing in docker is like pulling teeth, FFS. It works great once you figure out the obscure syntax and methods, but until then it's one headdesk after another.
Yeah, what's the dot at the end for? You're specifying two docker files.
@tblev The '.' at the end is telling docker which directory to run from, in this case, the root of the solution rather than the project. Careful though, the COPY command is case sensitive and if you have your project directory named inconsistently with the command (camel case etc.) that will confuse docker
i had this problem, just check your path for typo
I love the inclusion of the debugging process in a small note after the answer itself! Noting how to get detailed verbosity with the IDE at hand is very useful to independent visitors.
|
43

I had the same issue, I set the Docker environment to Windows in when adding Docker support. Even running in Visual Studio threw error to that. I changed the environment to Linux as my Docker is running in the Windows Subsystem for Linux (WSL).

Then I moved back to the terminal to run the commands.

I was able to resolve this by moving to the Solutions folder (Root folder).

And I did docker build like this:

docker build -t containername/tag -f ProjectFolder/Dockerfile .

Then I did docker run:

docker run containername/tag

2 Comments

Using -f option in the .sln-folder had already been an answer 10 days before this answer.
Re "Linux Subsystem for Windows": Do you mean Windows Subsystem for Linux (WSL)?
23

Asking for a directory that does not exist throws this error.

In my case, I tried

 > [stage-1  7/14] COPY /.ssh/id_rsa.pub /.ssh/:
------
failed to compute cache key: "/.ssh/id_rsa.pub" not found: not found

I had forgotten to add the /.ssh folder to the project directory. In your case you should check whether /client is really a subfolder of your Dockerfile build context.

3 Comments

same here, made a typo
Or maybe you renamed/moved a file that used to be referenced in your Docker file (ADD/COPY/etc.) and now is missing for the docker build to run. Whatever makes it be missing.
Same in my case. Glad I searched this. I simply didn't think to check for a typo, lol
19

The following command was failing with failed to compute cache key: not found:

docker build -t tag-name:v1.5.1 - <Dockerfile

Upon changing the command to the following it got fixed:

docker build -t tag-name:v1.5.1 -f Dockerfile .

1 Comment

Simply doing docker build . worked for me since "Dockerfile" is the default dockerfile filename.
19
Error : failed to compute cache key: "src" not found: not found 

in my case , folder/file excluded in .dockerignore

after resolving file from dockerignore able to create image.

Comments

10

I had a similar issues: Apparently, docker roots the file system during build to the specified build directory for security reasons. As a result, COPY and ADD cannot refer to arbitrary locations on the host file system. Additionally, there are other issues with syntax peculiarities. What eventually worked was the following:

COPY ./script_file.sh  /
RUN /script_file.sh

3 Comments

Other solutions here were overkill. This was simplest and cleanest. However, you may need to run docker builder prune if you still get cache errors.
Yup this is what fixed it for me, was specifying an absolute path
Thanks. Simplest, best answer. I created a folder ./copy_files, copied my files to here, then in my Dockerfile it was COPY ./copy_files/fname folder_in_container .
7

In my case I found that docker build is case sensitive in directory name, so I was writing /bin/release/net5.0/publish in the COPY instruction and failed with the same error, I've just changed to /bin/Release/net5.0/publish and it worked

1 Comment

This was the answer for me. Windows not being case sensitive on pathing makes working with docker fun sometimes.
7

In my case, I had something like this:

FROM mcr.microsoft.com/dotnet/aspnet:5.0
COPY bin/Release/net5.0/publish/ app/
WORKDIR /app
ENTRYPOINT ["dotnet", "MyApi.dll"]

And I finally realized that I had the bin folder in my .dockerignore file.

Comments

5

It's all due to an error in the dockerignore file. If you check that the file has ignore target then leave it out

Comments

3

In my case I changed context, and path of Dockerfile within docker-compose.yml config:

services:
    server:
        # inheritance structru
        extends:
            file: ../../docker-compose.server.yml
        # I recommend you to play with this paths
        build:
            context: ../../
            dockerfile: ./apps/${APP_NAME}/Dockerfile
        ...

Comments

3

Moving the Dockerfile one folder up as suggested by m.mitin worked for me as well. The instructions in the Dockerfile assume it is outside the project folder, it has nothing to do with minimization of files. In fact, I deliberately copied the project files directly onto my server and got the same error. I think the team at Microsoft should do something about to avoid productivity loss caused by this issue.

Comments

2

Use docker build -f Dockerfile .. from the directory where the Dockerfile exists (Note the 2 dots at the end). Official docs: https://learn.microsoft.com/en-us/visualstudio/containers/container-build?view=vs-2022#use-docker-build

Comments

1

I had the same issue. In my case there was a wrong directory specified. My Dockerfile was:

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS publish
WORKDIR /app
COPY . .
RUN dotnet publish -c Release -o publish/web src/MyApp/MyApp.csproj

FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY --from=publish publish/web .
EXPOSE 80
CMD ASPNETCORE_URLS=http://*:$PORT dotnet MyApp.dll

Then I realised that in the second build stage I am trying to copy project files from directory publish/web:

COPY --from=publish publish/web .

But as I specified workdir /app in the first stage, my files are located in that directory in image filesystem, so changing path from publish/web to app/publish/web resolved my issue.

So my final working Dockerfile is:

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS publish
WORKDIR /app
COPY . .
RUN dotnet publish -c Release -o publish/web src/MyApp/MyApp.csproj

FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY --from=publish app/publish/web .
EXPOSE 80
CMD ASPNETCORE_URLS=http://*:$PORT dotnet MyApp.dll

Comments

1

In my case there was a sneaky trailing whitespace in the file name.

------
 > [3/3] COPY init.sh ./:
------
failed to compute cache key: "/init.sh" not found: not found

So the file was actually called "init.sh " instead of "init.sh".

Comments

1

The nginx.conf file must be included to be published. Right Click on the nginx.conf file and open the Properties. Change "Copy to output directory" to "Copy always"

Comments

1

I face the same issue when I am trying to include a Docker service in my existing ASP.NET Core 8 project. I run the following command in the Dockerfile directory:

Dockerfile_folder> docker build -t fapi .

The reason I found is that the Dockerfile and the .dockerignore file are in different folders. So, I navigate to the gitignore folder in my CMD and then run the following Docker command, after which my application starts running as expected:

gitignore_folder> docker build -f API\Dockerfile -t fapi .

Comments

0

I had faced the same issue.

The reason was the name of the DLL file in the Docker file is case sensitive.

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY MyFirstMicroService.csproj .
RUN dotnet restore
COPY . .
RUN dotnet publish -c release -o /app

FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "**MyFirstMicroService.dll**"]

This .dll name should match your .csproj file.

Comments

0

in my case, it was a wrong Build with PATH configuration e.g. Docker build context

  1. Simple docker script
    docker build . 
    
    where . is path to build context
  2. Gradle+Docker
    docker {
        dependsOn build
        dependsOn dockerFilesCopy
        name "${project.name}:${project.version}"
        files "build" // path to build context
    }
    
  3. Gradle+GitHub action
    name: Docker build and push
    
    on:
      push:
        branches: [ main ]
    
    # ...
    
    jobs:
      build:
        runs-on: ubuntu-latest
    
        # ...
    
        steps:
          - name: Checkout
            uses: actions/checkout@v2
    
         # ...
    
          - name: Build and export to Docker
            uses: docker/build-push-action@v2
            with:
              # ...
              file: src/main/docker/Dockerfile      
              context: ./build                      # path to build context
    

Comments

0

For Laravel users...

I got this error when running docker compose up and it was due to there being a storage folder symlink in my public folder.

After deleting the symlink, the command executed successfully.

Comments

0

Firstly, my dockerfile looked like this:

FROM node:18

WORKDIR /app
COPY ./ /app
RUN npm install
EXPOSE 3000
CMD npm run dev

then it gave me the error: npm cound not find package.json

and after adding this to dockerfile:

COPY ./package.json ./app

and running the command :

docker build -f Dockerfile . (don't forget to add the dot at the end)

and that solved it.

Comments

0

In my case the copy command was using parent directories:

COPY ../../../www /var/www/

I had to change to

COPY ./www /var/www/

and it worked for me

Comments

0

In my case it was working fine in Visual Studio, but got this issue when trying to build via Azure DevOps pipelines because it uses the location of the Dockerfile as its default build context. By explicitly specifying the build context as $(Build.SourcesDirectory) it resolved the issue.

The step in my yaml script was specified as:

  - task: Docker@2
    displayName: Build an image
    inputs:
      command: build
      buildContext: $(Build.SourcesDirectory)
      Dockerfile: '**/Dockerfile'

Comments

0

As opposed to other comments, I did not change anything specific in my file system nor build commands. The build was working fine and one day it didn't. I started getting:

failed to solve: failed to compute cache key: failed to calculate checksum of ref..

Restarting docker did not solve the issue. Couldn't find the actual reason, but restarting the computer fixed it.

Comments

-1

This also happens when you don't provide the proper path to your COPY command input. The most important clue I had is that WORKDIR command opens a folder for the container, not in the windows explorer (so it doesn't affect the path you need to specify for the COPY command).

Comments

-1

In my Case, i was doing mistake in '/' and ''. Let me explain Open your dockerfile (it should be named as dockerfile only, not DockerFile or Dockerfile). You may have something like this- FROM mcr.microsoft.com/dotnet/runtime:5.0 COPY bin\Release\net5.0\publish . ENTRYPOINT ["dotnet", "HelloDocker.dll"]

Replace COPY bin\Release\net5.0\publish . to COPY bin/Release/net5.0/publish .

Comments

-1

In my case, with Angular project, my project was in the folder called ex: My-Folder-Project and I was putting on Dockerfile COPY --from=publish app/dist/My-Folder-Project . But of course the correct thing is put the "name" in your package.json like COPY --from=publish app/dist/name-in-package.json .

Comments

-1

I run into a similar issue where I was attempting to run the docker build command. I created a folder called "Docker" and inside of it another folder called "getting-started". I kept running into the error:

"failed to compute cache key: "/public" not found: not found"

Took me a while to realize that I should run the command from the "getting-started" folder instead of "Docker". It worked on I did so..

Comments

-1

I also got this error, first it resolved by removing some files from .dockerignore.

After that, I used a variable and haven't used $ operator to resolve it. Added $ before "{variable}", it worked for me.

Comments

-1

Anyone coming here after googling struggling with this on an ARM based Mac, you can try disabling Rosetta emulation in docker desktop.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.