Search code examples
c#azuredockerdocker-composenuget

Unable to publish the .net 5.0 and 6.0 to Azure Container Registry due to docker error


I have created a Web API project and added the docker file. When I am publishing it to the Azure Container Registry I am getting the below error.

It is happening for 5.0 and 6.0 both. I have tried with VS2019 and VS2022.

Error: C:\Program Files\dotnet\sdk\5.0.408\NuGet.targets(131,5): error : Unable to load the service index for source https://api.nuget.org/v3/index.json. [C:\src\learndoc\learndoc.csproj] C:\Program Files\dotnet\sdk\5.0.408\NuGet.targets(131,5): error : No such host is known. (api.nuget.org:443) [C:\src\learndoc\learndoc.csproj] C:\Program Files\dotnet\sdk\5.0.408\NuGet.targets(131,5): error : No such host is known. [C:\src\learndoc\learndoc.csproj] Removing intermediate container d8f9a4f724d7 The command 'cmd /S /C dotnet restore "learndoc/learndoc.csproj"' returned a non-zero code: 1 C:\Users\source\repos\learndocker\learndoc\Dockerfile(0,0): Error CTC1014: Docker command failed with exit code 1.

I am able to access the nuget path directly with the browser. I have tried deleting the nuget cache from appdata as well as localdata location but still not success. I have also tried restarting Visual Studio and recreating the Docker file.

Docker File Content :

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

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

FROM build AS publish
RUN dotnet publish "learndoc.csproj" -c Release -o /app/publish

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

Solution

  • I have tried with both .net 5.0 and 6.0 and able to publish in docker.

    In Visual studio we have chosen the Operating System as Linux while creating the docker file.

    The below NuGets need to installed.

    Microsoft.VisualStudio.Azure.Containers.Tools.Targets
    Swashbuckle.AspNetCore 
    
    

    enter image description here

    Used below Docker file for version 5.0.

    #See https://aka.ms/customizecontainer to learn how to customize your debug container and 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 ["WebApplication7.csproj", "."]
    RUN dotnet restore "./WebApplication7.csproj"
    COPY . .
    WORKDIR "/src/."
    RUN dotnet build "WebApplication7.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "WebApplication7.csproj" -c Release -o /app/publish /p:UseAppHost=false
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "WebApplication7.dll"]
    
    

    Same steps have been followed for version 6.0. and able to publish in docker.
    For both the Ports 80 and 443.

    After deploying to docker.

    enter image description here

    After deploying from Visual studio 2022 to docker hub.

    enter image description here

    To avoid the error, check below points.

    1. Try to access the NuGet package source https://api.nuget.org/v3/index.json in your web browser to see if it is accessible.
    2. If the NuGet package source is not accessible, try to use a different package source or try again later.
    3. If the NuGet package source is accessible, try to clear the NuGet cache and restore the packages again.
    4. If the issue persists, you can try to reinstall the .NET SDK or update it to the latest version.