.net核心docker通过VS2019工作,但是映像构建出现错误且无法正常工作 [英] .net core docker is working via VS2019, but image build is getting error and not working

查看:481
本文介绍了.net核心docker通过VS2019工作,但是映像构建出现错误且无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  "Docker": {
  "commandName": "Docker",
  "launchBrowser": true,
  "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/api/values",
  "httpPort": 52706,
  "useSSL": true,
  "sslPort": 44344
}

这将在通过Visual Studio运行时提供输出

This gives the output when it is run through visual studio

但是在构建时,它会引发错误

But on build, it throws error

DockerFile:

DockerFile:

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 83
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["testdocker/testdocker.csproj", "testdocker/"]
RUN dotnet restore "testdocker/testdocker.csproj"
COPY . .
WORKDIR "/src/testdocker"
RUN dotnet build "testdocker.csproj" -c Release -o /app

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENV ASPNETCORE_URLS http://+:83
ENTRYPOINT ["dotnet", "testdocker.dll"]

构建docker映像

docker build -t testdock .

但是它给出了

COPY failed: stat /var/lib/docker/tmp/docker-builder666564019/testdocker/testdocker.csproj,: no such file or directory

请帮助重写dockerfile,以完成此构建并运行应用程序

Please help to get the dockerfile rewritten so as to complete this build and run the app

推荐答案

如果在Visual Studio中查看容器工具"输出,则会看到类似以下的行:

If you look at the Container Tools output in Visual Studio, you'll see a line like:

docker build -f "C:\Users\foo\source\MySolution\TestDocker\Dockerfile" -t testdocker:dev --target base  --label "com.microsoft.created-by=visual-studio" "C:\Users\foo\source\MySolution"

在Windows上为Linux容器构建映像时,Docker会将活动目录的内容提升到MobyLinux VM中,所有复制命令等都将在MobyLinux VM中的该路径上运行,而不是在本地文件系统上运行.因为项目通常需要访问同一解决方案中的其他项目才能进行构建,所以Visual Studio创建的Dockerfile相对于您的解决方案目录,因此整个解决方案目录在MobyLinux中被提升.

When building an image for a Linux container on Windows, Docker lifts the contents of the active directory into the MobyLinux VM and all the copy commands and such are run against that path in the MobyLinux VM, not your local filesystem. Because projects very often need access to other projects in the same solution in order to build, the Dockerfiles created by Visual Studio are relative to your solution directory, such that the entire solution directory is lifted in MobyLinux.

很有可能,您要做的是直接导航到您的项目目录并从那里运行Dockerfile,而无需传递用作根"的目录.这样,Docker只是简单地提升了当前项目,即您的项目,目录,并且MobyLinux VM中的结果路径不再与Dockerfile中的内容匹配.

Very likely, what you've done is navigate directly into your project directory and run the Dockerfile from there, without passing a directory to use as the "root". As such, Docker simply lifts the current, i.e your project, directory and the resulting paths in the MobyLinux VM no longer match what's in the Dockerfile.

简短地说,如果要手动构建映像,则需要确保解除的活动目录是您的解决方案目录,而不是您的项目目录.您只需将上面命令的最后一个字符串传递给您自己的命令即可实现,这将使它相对于您的解决方案而言.

Long and short, if you want to manually do a build of the image, then you need to ensure that the active directory that's lifted is your solution directory, not your project directory. You can achieve that simply by passing that last string of the command above to your own command, which will make it relative to your solution.

这篇关于.net核心docker通过VS2019工作,但是映像构建出现错误且无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆