无法在Dockerfile中添加NuGet源

后端开发 2026-07-10

我现在正在用Docker搭建TeamCity服务器。

当前的问题是,我的构建需要来自同一服务器的其他构建所产生的NuGet包。为此,我使用TeamCity服务器提供的内部NuGet源。

因为构建代理默认不会识别该源,所以需要手动添加。由于我已经创建了一个修改过的Dockerfile(用于使用多个.NET版本),我尝试把它添加进去。

构建完成,日志显示已添加该源。

但在启动容器时它并不会出现,只列出默认源nuget.org。

我已经通过 docker exec 手动添加了该源,但这些改动不会持久化,下一次重启时会还原。

#
# Dockerfile containing manifest of TeamCity Windows Agent (AMD) with custom .NET SDKs
# See: https://dotnet.microsoft.com/download/dotnet/
#
# @param teamCityAgentImage target TeamCity Agent image, e.g. 'jetbrains/teamcity-agent:2023.05.4'
# @param dotnetSdkVersion target .NET SDK version, e.g. '7.0.401'
# @param dotnetSdkChecksum checksum of .NET SDK's archive obtained from "builds.dotnet.microsoft.com/dotnet/Sdk"
# @param dotnetSdkVersion2 target .NET SDK version, e.g. '7.0.401'
# @param dotnetSdkChecksum2 checksum of .NET SDK's archive obtained from "builds.dotnet.microsoft.com/dotnet/Sdk"

ARG teamCityAgentImage

FROM ${teamCityAgentImage}

ARG dotnetSdkVersion
ARG dotnetSdkChecksum
ARG dotnetSdkVersion2
ARG dotnetSdkChecksum2

# PowerShell
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

# Modify .NET & other paths
USER ContainerAdministrator
RUN setx /M PATH ('{0};{1}\bin;C:\Program Files\Git\cmd;C:\Program Files\Mercurial' -f $env:PATH, $env:JAVA_HOME)

RUN [Net.ServicePointManager]::SecurityProtocol = 'tls12, tls11, tls' ; \
    $code = Get-Content -Path "scripts/Web.cs" -Raw ; \
    Add-Type -IgnoreWarnings -TypeDefinition "$code" -Language CSharp ; \
    $url = 'https://builds.dotnet.microsoft.com/dotnet/Sdk/' + $Env:dotnetSdkVersion + '/dotnet-sdk-' + $Env:dotnetSdkVersion + '-win-x64.zip'; \
    $downloadScript = [Scripts.Web]::DownloadFiles($url + '#SHA512#' + $Env:dotnetSdkChecksum, 'dotnet.zip') ; \
    # Remove to successfully expand archive
    Remove-Item -Force -Recurse $Env:ProgramFiles\dotnet; \
    # 2. Extract .NET version
    Expand-Archive dotnet.zip -Force -DestinationPath $Env:ProgramFiles\dotnet; \
    Remove-Item -Force dotnet.zip; \
    Get-ChildItem -Path $Env:ProgramFiles\dotnet -Include *.lzma -File -Recurse | foreach { $_.Delete()};

RUN [Net.ServicePointManager]::SecurityProtocol = 'tls12, tls11, tls' ; \
    $code = Get-Content -Path "scripts/Web.cs" -Raw ; \
    Add-Type -IgnoreWarnings -TypeDefinition "$code" -Language CSharp ; \
    $url = 'https://builds.dotnet.microsoft.com/dotnet/Sdk/' + $Env:dotnetSdkVersion2 + '/dotnet-sdk-' + $Env:dotnetSdkVersion2 + '-win-x64.zip'; \
    $downloadScript = [Scripts.Web]::DownloadFiles($url + '#SHA512#' + $Env:dotnetSdkChecksum2, 'dotnet2.zip') ; \
    # Remove to successfully expand archive
    #Remove-Item -Force -Recurse $Env:ProgramFiles\dotnet; \
    # 2. Extract .NET version
    Expand-Archive dotnet2.zip -Force -DestinationPath $Env:ProgramFiles\dotnet; \
    Remove-Item -Force dotnet2.zip; \
    Get-ChildItem -Path $Env:ProgramFiles\dotnet -Include *.lzma -File -Recurse | foreach { $_.Delete()};

# Add NuGet Source of the Server so it can be found during builds
RUN dotnet nuget add source http://<server-url>/Custom/v3/index.json --name TeamCity --allow-insecure-connections; 
#RUN nuget sources add -name TeamCity -Source http://<server-url>/Custom/v3/index.json;

# Switch back to regular user
USER ContainerUser

解决方案

问题在于添加NuGet源时,实际上只对当前活动的用户生效。

先切换用户再添加该源即可解决。

...

# Switch back to regular user
USER ContainerUser

# Add NuGet Source of the Server so it can be found during builds
RUN dotnet nuget add source http://<server-url>/Custom/v3/index.json --name TeamCity --allow-insecure-connections;
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章