Dotnet restore Docker fail

时间秒杀一切 提交于 2021-01-29 09:05:36

问题


There is problem with Docker and command dotnet restore. I have the docker file:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["AuthAPI/AuthAPI.csproj", "AuthAPI/"]
COPY ["CommonCoreLibrary/CommonCoreLibrary.csproj", "CommonCoreLibrary/"]
RUN dotnet restore "AuthAPI/AuthAPI.csproj"
COPY . .
WORKDIR "/src/AuthAPI"
RUN dotnet build "AuthAPI.csproj" -c Release -o /app/build

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
COPY wait /wait
RUN chmod +x /wait
COPY /cert /root/.dotnet/https
ENTRYPOINT /wait && dotnet AuthAPI.dll

and docker compose file:

  authapi:
    image: ${DOCKER_REGISTRY-}authapi
    build:
      context: .
      dockerfile: AuthAPI/Dockerfile
    networks:
      - peopleapp
    environment:
      WAIT_HOSTS: db:5432
      WAIT_HOSTS_TIMEOUT: 300
    ports:
      - "8002:443"

When I run command

docker-compose -f "docker-compose.yml" build

I have a error

Step 10/23 : RUN dotnet restore "AnalyticAPI/AnalyticAPI.csproj"
 ---> Running in 2d29e1367d51
  Determining projects to restore...
/usr/share/dotnet/sdk/3.1.300/NuGet.targets(128,5): error : Unable to load the service index for source https://api.nuget.org/v3/index.json. [/src/AnalyticAPI/AnalyticAPI.csproj]
/usr/share/dotnet/sdk/3.1.300/NuGet.targets(128,5): error :   Connection refused [/src/AnalyticAPI/AnalyticAPI.csproj]
ERROR: Service 'analyticapi' failed to build: The command '/bin/sh -c dotnet restore "AnalyticAPI/AnalyticAPI.csproj"' returned a non-zero code: 1

Sometimes it works fine. There are warning about Connection refused, but restoring is success. I use Windows 10 + Hyper-V + Docker Desktop + Linux Containers + NET Core 3.1.


回答1:


I had a similar problem. It's works for me:

  1. Add curl command to dockerfile before restore:
  • RUN curl https://api.nuget.org/v3/index.json -k
  • RUN dotnet restore "AuthAPI/AuthAPI.csproj"
  1. Build image
  2. Remove curl command from dockerfile
  3. Build image again


来源:https://stackoverflow.com/questions/61920258/dotnet-restore-docker-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!