如何在dotnet的Dockerfile中添加私有NuGet源?

19

我试图在构建脚本中添加(覆盖)私有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

如果您升级到3.1版本,就支持此功能。请参考https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-nuget-add-source。 - red888
5个回答

14

通过向解决方案/项目添加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
这个可以运行,但是在构建管道中出现了未授权的错误。 - Rod Talingting
1
关于 activePackageSource 的注意事项(仅适用于2.x版本;在3.x及以上版本中已弃用) - Remy

12

我的当前解决方案是创建一个包含用户名和密码占位符的packageSourceCredentials部分的nuget.config副本。然后,我将现有的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
      .

1
使用这种方法,您的用户名和令牌将出现在镜像中。您应该使用docker secret来实现此目的。https://dev59.com/9FwX5IYBdhLWcg3wlwaQ - Kamushek
你不能在独立容器中使用Docker Secrets。 - db2
@Kamushek 我们使用分阶段构建来防止这种情况:https://docs.docker.com/build/building/multi-stage/ - jwillmer
@jwillmer 如果我没记错的话,在分阶段构建中,你的数据将可以在构建机器或其他地方的历史记录中访问到。 - Kamushek

6
  1. In your private feed if it's azure artifacts create a personal access token with full access.

    enter image description here

  2. Add a NuGet.Config file to the root of your project

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

    the ClearTextPassword key is your PAT

  3. Add these two-line to the copy section of your docker file

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

    and finally run your docker build it should be work.


2
这样做是可行的,但你会把凭证提交到你的代码库中。 - David Clarke
当你拥有私有仓库时,你别无选择。 - javidasd
1
你至少可以将PAT限制为仅读取包。 - David Clarke
这并不完全准确。您可以通过参数将凭据传递给Dockerfile。 ARG password - Fábio
1
最后两个Docker指令可以合并成一个(直接将COPY到正确的目录,不需要使用cp),否则会创建一个不必要的层。 - Alex Povel

4

2023年的答案

没有安全措施

这段代码针对WebApplication3运行得很好。我们使用BaGet NuGet服务器作为Nuget.org和我们的构建服务器之间的代理,以便更快地加载我们使用的常见包。

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["WebApplication3/WebApplication3.csproj", "WebApplication3/"]

# !!!IMPORTANT PART HERE !!!

# Add your NuGet server here
RUN dotnet nuget add source https://nuget.yourdomain.com/v3/index.json
# For our purposes, to hide nuget.org behind a NuGet proxy we disable its source, you can skip that
RUN dotnet nuget disable source "nuget.org"

# Just to see if two lines above work
RUN dotnet nuget list source

RUN dotnet restore "WebApplication3/WebApplication3.csproj"

COPY . .
WORKDIR "/src/WebApplication3"
RUN dotnet build "WebApplication3.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApplication3.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication3.dll"]

使用基本身份验证

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build

# !!!IMPORTANT PART HERE !!!

ARG NUGET_USERNAME
ARG NUGET_PASSWORD

ENV NUGET_USERNAME=${NUGET_USERNAME}
ENV NUGET_PASSWORD=${NUGET_PASSWORD}

# Adds this source with basic authentication, other authentication types exist but I'm not sure if they are applicable here in Linux based container
RUN dotnet nuget add source https://nuget.yourdomain.com/v3/index.json --name="Your source name" --username ${NUGET_USERNAME} --valid-authentication-types basic --store-password-in-clear-text --password ${NUGET_PASSWORD}


WORKDIR /src
COPY ["WebApplication3/WebApplication3.csproj", "WebApplication3/"]

RUN dotnet nuget disable source "nuget.org"

# Just to see if two lines above work
RUN dotnet nuget list source

RUN dotnet restore "WebApplication3/WebApplication3.csproj"

COPY . .
WORKDIR "/src/WebApplication3"
RUN dotnet build "WebApplication3.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApplication3.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication3.dll"]

1
这太棒了!谢谢分享! - Mr. Pumpkin
1
太棒了!谢谢分享! - undefined
1
男人,你真是救命恩人。我一次又一次地尝试只使用ARG NUGET_USERNAME,但它没有起作用。我不知道我必须先将arg放入env中。谢谢! - Aleksandar Angelov
1
哥们,你真是个救命稻草。我一直尝试着只用 ARG NUGET_USERNAME,但是不管怎么试都不行。我完全不知道我得先把这个参数放到环境变量里。谢谢! - undefined

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


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