I have a problem with dotnet restore command used in docker container.
I am using standard Dockerfile generated by VS2022 for simple console project in .net 7. DockerFile is generated by Visual Studio.
Docker Engine v24.0.7 Docker Desktop v4.26.1
DockerFile :
FROM mcr.microsoft.com/dotnet/runtime:7.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["ConsoleApp1.csproj", "."]
#RUN curl -i -k -X GET https://api.nuget.org/v3/index.json
RUN dotnet restore "./././ConsoleApp1.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "./ConsoleApp1.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./ConsoleApp1.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ConsoleApp1.dll"]
Output :
docker build -f Dockerfile --force-rm -t consoleapp1:dev --target build .
[+] Building 7.4s (8/11) docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 898B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 464B 0.0s
=> [internal] load metadata for mcr.microsoft.com/dotnet/sdk:7.0 0.1s
=> [build 1/7] FROM mcr.microsoft.com/dotnet/sdk:7.0@sha256:4be8ff7cb847f9e7ea1ac194920b0a6d41956af89f934b61a2ce64dc8563471c 0.0s
=> => resolve mcr.microsoft.com/dotnet/sdk:7.0@sha256:4be8ff7cb847f9e7ea1ac194920b0a6d41956af89f934b61a2ce64dc8563471c 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 184B 0.0s
=> CACHED [build 2/7] WORKDIR /src 0.0s
=> CACHED [build 3/7] COPY [ConsoleApp1.csproj, .] 0.0s
=> ERROR [build 4/7] RUN dotnet restore "./././ConsoleApp1.csproj" 7.3s
------
> [build 4/7] RUN dotnet restore "./././ConsoleApp1.csproj":
0.913 Determining projects to restore...
7.194 /src/ConsoleApp1.csproj : error NU1301: Unable to load the service index for source https://api.nuget.org/v3/index.json.
7.242 Failed to restore /src/ConsoleApp1.csproj (in 6.13 sec).
------
Dockerfile:11
--------------------
9 | COPY ["ConsoleApp1.csproj", "."]
10 | #RUN curl -i -k -X GET https://api.nuget.org/v3/index.json
11 | >>> RUN dotnet restore "./././ConsoleApp1.csproj"
12 | COPY . .
13 | WORKDIR "/src/."
--------------------
ERROR: failed to solve: process "/bin/sh -c dotnet restore \"./././ConsoleApp1.csproj\"" did not complete successfully: exit code: 1
I try this request from container and I could hit nuget :
curl -v -k https://api.nuget.org/v3/index.json
I try severals solutions (link) posted in stackoverflow and the web but I have same issue...
UPDATE 1 :
I fixed the problem, my company use a proxy. We have disabled the proxy for tests and dotnet restore command not failed.
All I have to do is import my company certificate in the container.
UPDATE 2 :
Import certificates and update :
...
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["ConsoleApp1.csproj", "."]
COPY ["./Certificate/myCertificate.crt", "/usr/local/share/ca-certificates/myCertificate.crt"]
COPY ["./Certificate/myCertificate.crt", "/usr/local/share/ca-certificates/myCertificate.crt"]
RUN update-ca-certificates
RUN dotnet restore "./././ConsoleApp1.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "./ConsoleApp1.csproj" -c $BUILD_CONFIGURATION -o /app/build