【问题标题】:asp.net core docker issueasp.net核心码头问题
【发布时间】:2018-01-14 14:28:32
【问题描述】:

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

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

有人知道我的配置和 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 目录
  • @PavelAgarkov ,是的,这是我最初的猜测,但通过在解决方案中提供 nuget.config 应该可以解决。

标签: docker asp.net-core nuget-package dotnet-restore


【解决方案1】:

好像有两个问题

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

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

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

为了解决这个问题,我推荐以下方法。

  1. 要么将 Dockerfile 移至解决方案目录,要么将 NuGet.config 移至项目目录。
  2. 添加第二个名为 NuGet.linux.config 的文件,并在非 Windows 上构建时指向该文件。在 Linux 上构建时,使用 RestoreConfigFile 属性指向此文件。如果您已将 NuGet.config 移至项目目录,则将这些行添加到您的 aspnetcoretssl.csproj 文件中即可:

    <PropertyGroup>
      <RestoreConfigFile Condition="'$(OS)' != 'Windows_NT'">NuGet.linux.config</RestoreConfigFile>
      <RestoreConfigFile Condition="'$(OS)' == 'Windows_NT'">NuGet.config</RestoreConfigFile>
    </PropertyGroup>
    
  3. 创建从\\path\to\the\nuget_packages_folderZ:\ 的网络挂载

  4. NuGet.linux.config 中,将“myrepo”更改为&lt;add key="myrepo" value="/nuget/myrepo" /&gt;
  5. 将此驱动器安装到容器中。这是通过 docker-run 上的 --volume 参数完成的。 docker run --volume Z:/:/nuget/myrepo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-09
    • 2019-04-24
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 2017-08-25
    相关资源
    最近更新 更多