Home > Mobile >  dotnet restore in docker build not using all nuget sources (ignoring private github source)
dotnet restore in docker build not using all nuget sources (ignoring private github source)

Time:06-18

Took me a few hours to work out but I finally got our private github nuget package repo to accept uploads and allow me to add those packages to a local project (which then happily builds). I had to mess about with the %AppData%\Roaming\NuGet\nuget.config and add the required credentials in there.

However, I cannot get the dotnet build step of the dockerfile to recognise that there is more than one source, it seems to only look for nuget.org

nuget.config:

<?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="[myCompany]" value="https://nuget.pkg.github.com/[myCompany]/index.json" />
  </packageSources>
  <packageRestore>
    <add key="enabled" value="True" />
    <add key="automatic" value="True" />
  </packageRestore>
  <bindingRedirects>
    <add key="skip" value="False" />
  </bindingRedirects>
  <packageManagement>
    <add key="format" value="0" />
    <add key="disabled" value="False" />
  </packageManagement>
  <packageSourceCredentials>
    <[myCompany]>
        <add key="Username" value="[myUserName]" />
        <add key="ClearTextPassword" value="[PAT Token Key]" />
      </[myCompany]>
  </packageSourceCredentials>
</configuration>

DockerFile:

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

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["[myApp]/[myApp].csproj", "[myApp]/"]
RUN dotnet restore "[myApp]/[myApp].csproj"
COPY . .
WORKDIR "/src/[myApp]"
RUN dotnet build "[myApp].csproj" -c Release -o /app/build

:
:

Output from build:

PS G:\[myCompany]\[myApp]> docker build -t [myApp] .
[ ] Building 6.3s (12/18)
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 32B                                                                                0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load metadata for mcr.microsoft.com/dotnet/sdk:6.0                                                  0.4s
 => [internal] load metadata for mcr.microsoft.com/dotnet/runtime:6.0                                              0.3s
 => [build 1/7] FROM mcr.microsoft.com/dotnet/sdk:6.0@sha256:09c76ca233d3a413fd72bd0bc8c342ed12a8b55f1cc29ac455c0  0.0s
 => [internal] load build context                                                                                  0.1s
 => => transferring context: 37.90kB                                                                               0.0s
 => [base 1/2] FROM mcr.microsoft.com/dotnet/runtime:6.0@sha256:20c5363575c40c5a22993698d4f62291bfa564709c7677cbf  0.0s
 => CACHED [base 2/2] WORKDIR /app                                                                                 0.0s
 => CACHED [final 1/2] WORKDIR /app                                                                                0.0s
 => CACHED [build 2/7] WORKDIR /src                                                                                0.0s
 => CACHED [build 3/7] COPY [[myApp]/[myApp].csproj, [myApp]  0.0s
 => ERROR [build 4/7] RUN dotnet restore "[myApp]/[myApp].csproj"          5.8s
------
 > [build 4/7] RUN dotnet restore "[myApp]/[myApp].csproj":
#12 1.007   Determining projects to restore...
#12 5.536 /src/[myApp]/[myApp].csproj : error NU1101: Unable to find package [myNugetPackage]. No packages exist with this id in source(s): nuget.org
#12 5.636   Failed to restore /src/[myApp]/[myApp].csproj (in 4.46 sec).
------
executor failed running [/bin/sh -c dotnet restore "[myApp]/[myApp].csproj"]: exit code: 1
PS G:\[myCompany]\[myApp]>

I've tried adding the nuget.config to various folders and the solution/project - no change.

I also tried to add the path to the nuget.config to the DockerFile with the --configfile parameter - unfortunately the "/src" was prepended to it and didn't work.

Help, please?

CodePudding user response:

At the time you do dotnet restore in your Dockerfile, the nuget.config file hasen't been copied into the image. You've only copied the .csproj file.

  • Related