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

klr1opcd  于 2023-05-06  发布在  Docker
关注(0)|答案(5)|浏览(394)

我尝试在我的构建脚本中添加(覆盖)一个私有的nuget源代码,以便添加用户/pw -并使其不受我的源代码控制。到目前为止,我尝试的是:

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

mdfafbf11#

我目前的解决方法是创建一个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
      .
hsgswve4

hsgswve42#

通过将nuget.config添加到solution/project中并将其复制到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>
carvr3hs

carvr3hs3#

1.在你的私有feed中,如果是azure工件,创建一个具有完全访问权限的个人访问令牌。

1.将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
1.将这两行添加到docker文件的copy部分

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

最后运行你的Docker build,它应该可以工作。

093gszye

093gszye4#

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

cgyqldqp

cgyqldqp5#

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

相关问题