.net core docker is working via VS2019, but image build is getting error and not working

青春壹個敷衍的年華 提交于 2019-12-11 09:02:27

问题


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

This gives the output when it is run through visual studio

But on build, it throws error

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"]

to build the docker image

docker build -t testdock .

but it gives

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

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


回答1:


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"

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.

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.



来源:https://stackoverflow.com/questions/55475041/net-core-docker-is-working-via-vs2019-but-image-build-is-getting-error-and-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!