VS2019 Docker 支持和 Dockerfile 失败

Sha*_*eKm 4 .net c# docker dockerfile visual-studio-2019

VS2019,创建了一个全新的 mvc 应用程序,支持 Windows Docker。

Dockerfile 内容(从模板创建):

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-nanoserver-1809 AS base
WORKDIR /app
EXPOSE 80

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

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "mvc1.dll"]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

当我执行时:

docker build -t mvc1 .
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

C:\Program Files\dotnet\sdk\2.2.401\NuGet.targets(123,5): 错误:无法加载源https://api.nuget.org/v3/index.json的服务索引 。[C:\src\mvc1\mvc1.csproj] C:\Program Files\dotnet\sdk\2.2.401\NuGet.targets(123,5): 错误:不知道这样的主机 [C:\src\mvc1\ mvc1.csproj] 命令“cmd /S /C dotnet Restore“mvc1/mvc1.csproj””返回非零代码:1

编辑:我已将此行添加到 Dockerfile 中:

RUN ping google.com
Run Code Online (Sandbox Code Playgroud)

并得到:

Step 4/17 : RUN ping google.com
 ---> Running in 6633175b21a8
Ping request could not find host google.com. Please check the name and try again.
Run Code Online (Sandbox Code Playgroud)

编辑2:

因此,当我编辑 .csproj 文件并删除这一行时,结果是:

        <Project Sdk="Microsoft.NET.Sdk.Web">

          <PropertyGroup>
            <TargetFramework>netcoreapp2.2</TargetFramework>
            <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
          </PropertyGroup>


          <ItemGroup>
            <PackageReference Include="Microsoft.AspNetCore.App" />
            <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    <!-- REMOVED -->
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.7.12" />
  <!-- REMOVED -->
          </ItemGroup>

        </Project>
Run Code Online (Sandbox Code Playgroud)

它确实有效。这是为什么?

小智 5

我有同样的问题。此链接解决了我的问题: https://improveandrepeat.com/2019/09/how-to-fix-network-errors-with-docker-and-windows-containers/

我的默认以太网适配器没有最低指标检查:

Get-NetIPInterface -AddressFamily IPv4 | Sort-Object -Property InterfaceMetric -Descending
Run Code Online (Sandbox Code Playgroud)

设置为:

Set-NetIPInterface -InterfaceAlias 'Ethernet' -InterfaceMetric 4
Run Code Online (Sandbox Code Playgroud)