如何在dotnet dockerfile中添加私有nuget源?

时间:2019-08-12 13:32:24

标签: .net docker .net-core nuget dockerfile

我尝试将私有nuget源添加(覆盖)到我的构建脚本中,以添加用户/密码-并将其保留在我的源代码控制之外。到目前为止,我尝试过的事情:

  • nuget未被识别为图像内的命令
  • dotnet nuget没有添加其他来源的命令
  • 安装nuget不会影响dotnet restore
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 
RUN apt-get update && apt-get install -y nuget 
RUN nuget source Add -Name "Private Feed" -Source ".." -UserName "UserVAR" -Password "PassVAR"
RUN dotnet restore

4 个答案:

答案 0 :(得分:5)

通过将nuget.config添加到解决方案/项目并将其复制到Docker项目中:

WORKDIR /src
COPY ["nuget.config", ""]

您可以添加源,然后成功完成docker构建。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="Nellson Nuget Repo" value="http://private.source.local:123/v3/index.json" />
  </packageSources>
  <activePackageSource>
    <add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" />
    <add key="Nellson Nuget Repo" value="http://private.source.local:123/v3/index.json" />
  </activePackageSource>
</configuration>

答案 1 :(得分:0)

我当前的解决方法是创建一个nuget.config部分的packageSourceCredentials副本,其中包含用户名和密码的占位符。然后,我用此文件替换现有的nuget.config,并用环境变量替换用户名和密码。

唯一的缺点是我需要使两个配置文件保持同步。如果我在项目中修改了nuget.config,我还需要记住也要更新副本。

nuget.config.template

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="GitHub private registry" value="https://nuget.pkg.github.com/your_orga/index.json" />
  </packageSources>
  <packageSourceCredentials>
    <GitHub_x0020_private_x0020_registry>
        <add key="Username" value="USER" />
        <add key="ClearTextPassword" value="PW" />
    </GitHub_x0020_nuget_x0020_registry>
</packageSourceCredentials>
</configuration>

Dockerfile

FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-image
ARG NUGET_AUTH_TOKEN
ARG NUGET_USER_NAME

WORKDIR /app
COPY ./My.Project .

# Replace nuget.config
RUN rm nuget.config
COPY ./gitlab-ci/nuget.config.template ./nuget.config
RUN sed -i -e "s/USER/$NUGET_USER_NAME/g" -e "s/PW/$NUGET_AUTH_TOKEN/g" nuget.config

RUN dotnet restore

.gitlab-ci.yml

docker build
      --build-arg NUGET_USER_NAME=$NUGET_USER_NAME
      --build-arg NUGET_AUTH_TOKEN=$NUGET_AUTH_TOKEN
      --tag $CI_REGISTRY/organization/application:$CI_COMMIT_SHA
      --file gitlab-ci/Dockerfile
      .

答案 2 :(得分:0)

nuget source命令是nuget.exe命令行界面的一部分,该命令只能在Linux上与Mono结合使用。因为mcr.microsoft.com/dotnet/core/sdk映像是Linux容器(准确地说是Debian),所以您需要自己安装Mono和Nuget才能使用该命令。您的另一个选择是获取official Mono docker image并在其中安装.NET Core和NuGet.exe。

答案 3 :(得分:0)

1.在您的私人提要中,如果它是天蓝色的工件,请创建一个具有完全访问权限的个人访问令牌。

enter image description here

2. 将 NuGet.Config 文件添加到项目的根目录

<configuration>
  <packageSources>
    <add key="MyPrivateFeed" value="https://myfeed.url/index.json" />
  </packageSources>
  <packageSourceCredentials>
    <MyPrivateFeed>
      <add key="Username" value="myusername" />
      <add key="ClearTextPassword" value="xq6n4lvnayeo2f467osasdasdgj4nat7xw2mkm7qwowvqeutjdueq" />
      <add key="ValidAuthenticationTypes" value="basic" />
    </MyPrivateFeed>
  </packageSourceCredentials>
</configuration>

ClearTextPassword 密钥是您的 PAT

3.将这两行添加到你的docker文件的copy部分

COPY "NuGet.Config" "/"
RUN ["cp", "/NuGet.Config", "/root/.nuget/NuGet/NuGet.Config"]

最后运行你的 docker build 它应该可以工作了。