asp.net core 在 dotnet restore 上的 docker 问题

5

我是docker的新手,我开发了一个简单的asp.net core webapi解决方案,它使用了私有存储库中的NuGet包。

dotnet restore命令返回一个错误,因为nuget.config文件中引入了位于私有存储库中的缺失NuGet包。

有人知道我的配置和dockerfile有什么问题吗?

Dockerfile

FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# build runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetcoretssl.dll"]

.dockerignore

bin\
obj\

此外,我在解决方案的根目录下有一个nuget.config文件,如下所示

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="myrepo" value="\\path\to\the\nuget_packages_folder" />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
</configuration> 

但是我从以下位置收到消息:

docker build -t aspnetcoretssl .

错误 NU1101:无法找到 TestContracts 包。在源中不存在此 ID 的包:nuget.org 生成 MSBuild 文件 /app/obj/aspnetcoretssl.csproj.nuget.g.props。

命令 '/bin/sh -c dotnet restore' 返回非零代码:1


很可能你在 Linux 容器内运行了 dotnet restore,但它无法访问位于某个 Windows 共享文件夹上的自定义 NuGet 目录。 - Pavel Agarkov
@PavelAgarkov,是的,这是我的最初猜测,但是通过在解决方案中提供nuget.config应该可以解决问题。 - DjBuddy
1个回答

1

看起来有两个问题

  1. NuGet.config文件在您的解决方案目录中,但Dockerfile在项目文件夹中。这意味着COPY . ./不会将NuGet.config复制到Docker容器中

  2. 即使您有NuGet.config文件,“myrepo”源也是Docker容器中无效的文件路径。

这是无效的,因为这是Windows网络文件路径,但您的容器正在Linux上运行。

要解决此问题,我建议执行以下操作。

  1. Either move your Dockerfile to the solution directory, or move NuGet.config into the project directory.
  2. Add a second file named NuGet.linux.config, and point to this file when building on non-windows. Use the RestoreConfigFile property to point to this file when building on Linux. If you have moved NuGet.config into the project directory, adding these lines to your aspnetcoretssl.csproj files would work:

    <PropertyGroup>
      <RestoreConfigFile Condition="'$(OS)' != 'Windows_NT'">NuGet.linux.config</RestoreConfigFile>
      <RestoreConfigFile Condition="'$(OS)' == 'Windows_NT'">NuGet.config</RestoreConfigFile>
    </PropertyGroup>
    
  3. Creating a network mount from \\path\to\the\nuget_packages_folder to Z:\

  4. In NuGet.linux.config, change "myrepo" to <add key="myrepo" value="/nuget/myrepo" />
  5. Mounting this drive into the container. This is done with the --volume parameter on docker-run. docker run --volume Z:/:/nuget/myrepo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接