Unable to load the service index for source error for a CodeArtifact NuGet feed using Dockerfile


Hi! We’re using AWS CodeArtifact for storing our packages and when we try to build a Docker image from our Dockerfile it fails because it's unable to load the source during the restore process.

We have a web API in .Net that we want to deploy using AWS Fargate. This project runs smoothly from Visual Studio 2022 using Docker but we can’t build the image from PowerShell after adding our packages from CodeArtifact.

Our approach to include the credentials in the build is to pass the NuGet.Config stored on the host using Buildkit.

This’s our Dockerfile:

#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
ENV ASPNETCORE_URLS=http://+:49151

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

COPY ["Src/Presentation/Project.API/Project.API.csproj", "Src/Presentation/Project.API/"]
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
	--mount=type=secret,id=nugetconfig \
	dotnet restore "Src/Presentation/Project.API/Project.API.csproj" \
	--configfile /run/secrets/nugetconfig

COPY . .
WORKDIR "/src/Src/Presentation/Project.API"
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
	dotnet build "Project.API.csproj" -c Release -o /app/build \

FROM build AS publish
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
	dotnet publish "Project.API.csproj" -c Release -o /app/publish \

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

Our script in PowerShell:

docker buildx build --secret id=nugetconfig,src=$HOME\AppData\Roaming\NuGet\NuGet.Config -f "Src\Presentation\Project.API\Dockerfile" -t my-dotnet-image .


#14 [build 6/9] RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages 	--mount=type=secret,id=nugetconfig 	dotnet restore "Src/Presentation/Project.API/Project.API.csproj" 	
--configfile /run/secrets/nugetconfig
#14 1.175   Determining projects to restore...
#14 2.504 /src/Src/Presentation/Project.API/Project.API.csproj : error NU1301: Unable to load the service index for source 

What are we missing here? Once we have the Dockerfile working we want to use CDK for deploying the Docker image alongside our infrastructure.

We’re using aws codeartifact login command to authenticate with the service.


profile picture
asked 2 years ago5940 views
3 Answers

Did you ever get an answer to this? I'm having the same issue. Thanks

answered a year ago

Having this same issue -- did anyone find a solution?

answered a year ago

Try to update a source credentials for sourceName


<?xml version="1.0" encoding="utf-8"?>
    <!--To inherit the global NuGet package sources remove the <clear/> line below -->
    <clear />
    <add key="nuget" value="https://api.nuget.org/v3/index.json" />
    <add key="sourceName" value="https://mydomain-01005003.d.codeartifact.eu-west-1.amazonaws.com/nuget/nugets/v3/index.json" />

dotnet nuget update source sourceName


FROM mcr.microsoft.com/dotnet/sdk:6.0
COPY ./app .
COPY ./.aws /root/.aws
RUN apt-get update; apt-get install -y  python3-pip curl unzip

RUN curl https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip -o awscliv2.zip \
  && unzip awscliv2.zip \
  && ./aws/install \
  && rm -rf aws awscliv2.zip
RUN \    
    CODEARTIFACT_AUTH_TOKEN=`aws codeartifact get-authorization-token --domain mydomain --domain-owner 01005003 --region eu-west-1 --query authorizationToken --output text --profile default` 	 && \
    dotnet nuget update source sourceName -s "https://mydomain-01005003.d.codeartifact.eu-west-1.amazonaws.com/nuget/nugets/v3/index.json"  -u "aws" -p "${CODEARTIFACT_AUTH_TOKEN}" --store-password-in-clear-text --configfile "nuget.config"  

RUN \ 
	  dotnet publish  

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions