Docker 出现错误“无法计算缓存密钥:未找到” - 在 Visual Studio 中运行良好
我使用 Visual Studio 生成了一个 Dockerfile。它在 Visual Studio 中运行得很好,现在我尝试从 Windows 本身构建它(docker build .
,我尝试了很多组合)。但我收到以下错误:
> [build 3/7] COPY [client/client.csproj, client/]:
------
failed to compute cache key: "/client/client.csproj" not found: not found
当我将副本更改为 ./client.csproj
时,它会继续,然后我得到:
=> 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
我做错了什么?我将 Docker Linux 更改为 Windows,更改了 WSL,然后重新启动了所有内容。
#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"]
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"]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
检查您的
.dockerignore
文件。它可能会忽略复制命令所需的文件,并且您无法计算缓存键错误。.dockerignore
可以配置以最大程度地减少发送给Docker的文件以获得性能和安全性:第一行
*
删除所有文件。第二行!dist/
允许dist
文件夹,这可能会导致意外行为:
皮带和悬架器。
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:The first line
*
disallows all files. The second line!dist/
allows thedist
folderThis can cause unexpected behavior:
Belts and suspenders.
Visual Studio的方式有些奇怪。
它没有在dockerfile的文件夹中启动
docker build
,而是在父文件夹中启动,并使用-f
选项指定Dockerfile。我正在使用演示项目(试图为另一个问题创建最小的解决方案),并遇到了同样的情况。
我的演示项目的设置是
这样,所以我会期望能够完成,
但我会收到您的错误消息。
相反,使用
.sln
文件,然后使用docker-f
选项指定要在子文件夹中使用的dockerfile:请注意:注意
Docker build
命令。对于Docker而言,命令的最后一部分是Docker将使用的文件的位置。 通常这是带有Dockerfile的文件夹,但这与VS的方式有所不同。在这种情况下,使用
-f
指定Dockerfile。与指定的位置相对于任何路径(例如复制
指令)都是相对于指定位置的。。
的意思是“当前目录”,在我的示例中是\ workerService2
。我通过检查构建过程的输出,到达了这个阶段,并将详细的详细信息设置为详细。
如果您选择工具 /选项 /项目和解决方案 /构建和运行,您可以调整构建输出的冗长,我将我的详细信息详细介绍。
我认为我已经弄清楚了为什么视觉工作室这样做。
它允许在同一解决方案中复制项目引用。
如果将其设置为
docker build
从项目文件夹中进行,docker将无法复制
任何在解决方案中的其他项目中。但是设置此方法的方式,当前目录是解决方案文件夹,您可以将引用的项目(子文件夹)复制到Docker构建过程中。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
So I would expect to go
But I get your error message.
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: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 theCOPY
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 toCOPY
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.我遇到了同样的问题,我在添加 Docker 支持时将 Docker 环境设置为 Windows。即使在 Visual Studio 中运行也会出现错误。我将环境更改为 Linux,因为我的 Docker 在 Windows Linux 子系统 (WSL) 中运行。
然后我回到终端运行命令。
我可以通过移动到解决方案文件夹(根文件夹)来解决此问题。
我像这样做了
docker build
:然后我做了
docker run
: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:Then I did
docker run
:请求不存在的目录会引发此错误。
就我而言,我尝试
忘记将
/.ssh
文件夹添加到项目目录中。在您的情况下,您应该检查/client
是否确实是 Dockerfile 构建上下文的子文件夹。Asking for a directory that does not exist throws this error.
In my case, I tried
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.以下命令是
未能计算缓存键的失败:找不到
:将命令更改为以下命令时,它已修复:
The following command was failing with
failed to compute cache key: not found
:Upon changing the command to the following it got fixed:
就我而言,
文件夹/文件
在.dockerignore中排除了
从Dockerignore解析文件后能够创建图像。
in my case ,
folder/file
excluded in.dockerignore
after resolving file from dockerignore able to create image.
我遇到了类似的问题:显然,出于安全原因, docker 在构建期间将文件系统根到指定的构建目录。因此,
COPY
和ADD
无法引用主机文件系统上的任意位置。此外,语法特性还存在其他问题。最终起作用的是以下内容: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
andADD
cannot refer to arbitrary locations on the host file system. Additionally, there are other issues with syntax peculiarities. What eventually worked was the following:就我而言,我发现 docker build 在目录名称中区分大小写,因此我在 COPY 指令中写入 /bin/release/net5.0/publish 并因相同的错误而失败,我刚刚更改为 /bin/Release /net5.0/publish 并成功
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
这都是由于Dockerignore文件中的错误。
如果您检查文件是否忽略目标,请将其排除在外
It's all due to an error in the dockerignore file.
If you check that the file has ignore target then leave it out
就我而言,我有类似的东西:
我终于意识到我的.dockerignore文件中有 bin 文件夹。
In my case, I had something like this:
And I finally realized that I had the bin folder in my .dockerignore file.
按照 m.mitin 的建议将 Dockerfile 上移一个文件夹也对我有用。 Dockerfile 中的说明假设它位于项目文件夹之外,与文件最小化无关。事实上,我故意将项目文件直接复制到我的服务器上,并得到了同样的错误。我认为微软的团队应该采取一些措施来避免这个问题造成的生产力损失。
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.
就我而言,我更改了 docker-compose.yml 配置中的上下文和 Dockerfile 路径:
In my case I changed context, and path of Dockerfile within docker-compose.yml config:
从 Dockerfile 所在的目录使用 docker build -f Dockerfile .. (注意末尾的 2 个点)。官方文档: https ://learn.microsoft.com/en-us/visualstudio/containers/container-build?view=vs-2022#use-docker-build
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我有同样的问题。就我而言,指定了错误的目录。
我的 Dockerfile 是:
然后我意识到在第二个构建阶段我试图从目录
publish/web
复制项目文件:但是当我在第一阶段指定 workdir /app 时,我的文件位于该目录位于图像文件系统中,因此将路径从
publish/web
更改为app/publish/web
解决了我的问题。所以我最终的工作 Dockerfile 是:
I had the same issue. In my case there was a wrong directory specified.
My Dockerfile was:
Then I realised that in the second build stage I am trying to copy project files from directory
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
toapp/publish/web
resolved my issue.So my final working Dockerfile is:
就我而言,文件名中有一个偷偷摸摸的尾随空格。
所以该文件实际上被称为“init.sh”而不是“init.sh”。
In my case there was a sneaky trailing whitespace in the file name.
So the file was actually called "init.sh " instead of "init.sh".
必须包含 nginx.conf 文件才能发布。右键单击 nginx.conf 文件并打开“属性”。将“复制到输出目录”更改为“始终复制”
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"
我也遇到过同样的问题。
原因是Docker文件中的DLL文件名区分大小写。
此 .dll 名称应与您的 .csproj 文件匹配。
I had faced the same issue.
The reason was the name of the DLL file in the Docker file is case sensitive.
This .dll name should match your .csproj file.
就我而言,这是错误的 使用 PATH 构建 配置,例如 Docker
build context
其中
.
是构建上下文的路径<前><代码>码头工人{
取决于构建
取决于 dockerFilesCopy
名称“${项目.名称}:${项目.版本}”
files "build" // 构建上下文的路径
}
in my case, it was a wrong Build with PATH configuration e.g. Docker
build context
where
.
is path to build context对于 Laravel 用户...
我在运行 docker compose up 时遇到此错误,这是由于我的公共文件夹中存在存储文件夹符号链接。
删除符号链接后,命令执行成功。
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.
首先,我的 dockerfile 看起来像这样:
然后它给了我错误: npm cound not find package.json
并将其添加到 dockerfile 并
运行命令:
docker build -f Dockerfile 。 (不要忘记在末尾添加点)
并解决了这个问题。
Firstly, my dockerfile looked like this:
then it gave me the error:
npm cound not find package.json
and after adding this to dockerfile:
and running the command :
docker build -f Dockerfile .
(don't forget to add the dot at the end)and that solved it.
当我试图在现有的 ASP.NET Core 8项目中包括Docker服务时,我会面临同样的问题。我在dockerfile目录中运行以下命令:
我发现的原因是dockerfile和.dockerignore文件在不同的文件夹中。 ,我导航到cmd中的gitignore文件夹,然后运行。遵循Docker命令,此后我的应用程序开始按预期运行:
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:
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:
就我而言,它在 Visual Studio 中运行良好,但在尝试通过 Azure DevOps 管道构建时遇到了此问题,因为它使用 Dockerfile 的位置作为其默认构建上下文。通过将构建上下文显式指定为
$(Build.SourcesDirectory)
它解决了该问题。我的 yaml 脚本中的步骤指定为:
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:
当您没有提供 COPY 命令输入的正确路径时,也会发生这种情况。我得到的最重要的线索是 WORKDIR 命令打开容器的文件夹,而不是在 Windows 资源管理器中(因此它不会影响您需要为 COPY 命令指定的路径)。
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).
就我而言,
我在“/”和“”中犯了错误。让我解释一下
打开您的 dockerfile(它应该仅命名为 dockerfile,而不是 DockerFile 或 Dockerfile)。
你可能有这样的事情——
来自 mcr.microsoft.com/dotnet/runtime:5.0
复制 bin\Release\net5.0\publish 。
ENTRYPOINT ["dotnet", "HelloDocker.dll"]
将 COPY bin\Release\net5.0\publish . 替换为 COPY bin/Release/net5.0/publish 。
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 .
就我而言,使用Angular Project,我的项目位于名为Ex:My Folder-Project的文件夹中,我正在使用Dockerfile副本-from = Publish App/Dist/My-Folder-Project。
但是,当然,正确的东西是在您的软件包中放置的“名称”。
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 .
我在尝试运行 docker build 命令时遇到了类似的问题。我创建了一个名为“Docker”的文件夹,并在其中创建了另一个名为“getting-started”的文件夹。我不断遇到错误:
“无法计算缓存密钥:“/public”未找到:未找到”
我花了一段时间才意识到我应该从“入门”运行命令文件夹而不是“Docker”。它有效我这样做了..
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..
我也遇到了这个错误,首先通过从 .dockerignore 中删除一些文件来解决它。
之后,我使用了一个变量,并没有使用 $ 运算符来解决它。
在“{variable}”之前添加$,它对我有用。
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.
任何人在使用基于 ARM 的 Mac 上谷歌搜索后遇到此问题后来到这里,您可以尝试在 docker 桌面中禁用 Rosetta 模拟。
Anyone coming here after googling struggling with this on an ARM based Mac, you can try disabling Rosetta emulation in docker desktop.
就我而言,复制命令使用父目录:
我必须更改为
并且它对我有用
In my case the copy command was using parent directories:
I had to change to
and it worked for me
您可能直接在项目目录中创建Dockerfile,将其移入Main中,有一些权限不允许您这样做
You probably created Dockerfile directly in your project directory, move it inside of main, there are permissions that don't let you to do that