Dotnet restore Docker fail - c#

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.

I had a similar problem.
It's works for me:
Add curl command to dockerfile before restore:
RUN curl https://api.nuget.org/v3/index.json -k
RUN dotnet restore "AuthAPI/AuthAPI.csproj"
Build image
Remove curl command from dockerfile
Build image again

Related

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

It's my first question here so let me know if I have to change something or add more information.
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 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
WORKDIR /app
EXPOSE 80
ENV ASPNETCORE_URLS=http://+:49151
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY . .
WORKDIR /src
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 \
--no-restore
FROM build AS publish
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
dotnet publish "Project.API.csproj" -c Release -o /app/publish \
--no-restore
FROM base AS final
WORKDIR /app
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 .
Output:
#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
https://domain-123456789012.d.codeartifact.us-east-2.amazonaws.com/nuget/repository/v3/index.json.
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. We work on windows with a Linux container

Getting SSL error while trying to create docker image for AWS Lambda locally

I am trying to create simple Hello World AWS Lambda function in .Net core 3.1 using Visual studio and trying to build docker image locally using command prompt.
When I execute docker build -t Dockerfile . command, its giving following error:
------
> [build 4/7] RUN dotnet restore "base/AWSDockerLambda.csproj":
#12 1.555 Determining projects to restore...
#12 3.592 /usr/share/dotnet/sdk/3.1.421/NuGet.targets(128,5): error : Unable to load the service index for source https://api.nuget.org/v3/index.json. [/src/base/AWSDockerLambda.csproj]
#12 3.592 /usr/share/dotnet/sdk/3.1.421/NuGet.targets(128,5): error : The SSL connection could not be established, see inner exception. [/src/base/AWSDockerLambda.csproj]
#12 3.592 /usr/share/dotnet/sdk/3.1.421/NuGet.targets(128,5): error : The remote certificate is invalid according to the validation procedure. [/src/base/AWSDockerLambda.csproj]
------
Can anyone please help me here? Below is my Dockerfile:
FROM amazon/aws-lambda-dotnet:core3.1 AS base
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["AWSDockerLambda.csproj", "base/"]
RUN dotnet restore "base/AWSDockerLambda.csproj"
WORKDIR "/src"
COPY . .
RUN dotnet build "AWSDockerLambda.csproj" --configuration Release --output /app/build
FROM build AS publish
RUN dotnet publish "AWSLambdas3.csproj" \
--configuration Release \
--framework netcoreapp3.1 \
--self-contained false \
--output /app/publish
FROM base AS final
WORKDIR /var/task
COPY --from=publish /app/publish .
CMD ["AWSDockerLambda::AWSDockerLambda.Function::FunctionHandler"]

Docker dotnet restore: Unable to load the service index for source https://api.nuget.org/v3/index.json

Objective -> I am currently trying to integrate Dapr with a small sample project provided by the .Net project templates. At the end of my test cases, I would like to use Docker Compose to launch multiple containers for a web API and a web app to communicate with each other using the Dapr sidecars.
Problem -> Whenever I run docker compose up on the sample application, the container fails to be created and is stopped by this error:
> [build 4/7] RUN dotnet restore "MyBackEnd/MyBackEnd.csproj":
#0 0.945 Determining projects to restore...
#0 7.179 /src/MyBackEnd/MyBackEnd.csproj : error NU1301: Unable to load the service index for source https://api.nuget.org/v3/index.json.
#0 7.202 Failed to restore /src/MyBackEnd/MyBackEnd.csproj (in 6.09 sec).
Steps to Reproduce ->
The following are the steps I took with this project:
Ran mkdir sample_project && cd sample_project
Ran dotnet new webapi -o MyBackEnd followed by cd MyBackEnd
Ran touch Dockerfile && cd .. && touch docker-compose.yml
I then put this in my Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base
WORKDIR /app
RUN apt-get update && apt-get install -y libcurl4
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
COPY ["MyBackEnd/MyBackEnd.csproj", "MyBackEnd/"]
RUN dotnet restore "MyBackEnd/MyBackEnd.csproj"
COPY . .
WORKDIR "/src/MyBackEnd"
RUN dotnet build "MyBackEnd.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyBackEnd.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyBackEnd.dll"]
And this was what I did with my Docker Compose YAML file:
version: '3.4'
services:
mybackend:
image: ${DOCKER_REGISTRY-}mybackend
build:
context: .
dockerfile: MyBackEnd/Dockerfile
ports:
- "3000:80"
Finally, I ran docker compose up to arrive at the issue
Additional Information -> I am not running this on a Windows system. I am using Ubuntu 20, and thus, I do not have access to Visual Studio to handle the launching and configuration for me. I am aware that this is a heavily replicated question, but I have yet to see anything that specifically applies to users that do not have access to Visual Studio or on Ubuntu. Thanks!

Kafka with .net core docker file config

My app is .net core 3.1 based. Trying to connect to kafka , publish and consume topics. Locally the code is working fine no error .Running the code in wsl2 ubuntu env using vs code. Now I want to create a docker image for the same code and publish in azure. The docker image is getting created but while trying to run the image getting error
No provider for SASL mechanism GSSAPI:Recompile librdkafka with libsasal2 or openssl support. Current build options:Plain Sasl_scram ouathbearer.
Please help with the docker file as what exactly to install and how to do so
Docker file :
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
RUN apt-get update
RUN apt-get install -y sasl2-bin \
libsasl2-modules-gssapi-mit\
krb5-user \
libkrb5-dev \
libsasl2-dev\
libzstd-dev
RUN echo "Done"
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["abc.csproj", "./"]
RUN dotnet restore "./abc.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "./abc.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "abc.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "abc.dll"]

.net core Failed to run as a self-contained app. on a docker

I have this Dockerfile for .net core
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
WORKDIR /app
EXPOSE 8080
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["./WebApplication1.csproj", "WebApplication1/"]
RUN dotnet restore "WebApplication1/WebApplication1.csproj"
COPY . .
WORKDIR "/src/WebApplication1"
RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish --self-contained
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]
When I run the docker I get the error:
A fatal error was encountered. The library 'libhostpolicy.so' required to execute the application was not found in '/app/'.
I read this, and it is not related to my problem
--self-contained require a -r (--runtime)
-r, --runtime The target runtime to publish for. This is used when creating a self-contained deployment.
The default is to publish a framework-dependent application.
I'd start with changing the publish command in Dockerfile to this:
RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish --self-contained --runtime linux-64
New Part:
I run the below command
$ RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish --self-contained
and I got below error:
/usr/local/share/dotnet/sdk/3.0.100/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.RuntimeIdentifierInference.targets(127,5): error NETSDK1031: It is not supported to build or publish a self-contained application without specifying a RuntimeIdentifier. Please either specify a RuntimeIdentifier or set SelfContained to false. [/Users/ziaullahkhan/Code/SelfContainedApp/SelfContainedApp.csproj]
I changed the command to this:
$ dotnet publish -c Release --self-contained --runtime linux-x64 -o out
and it published everything successfully.
I then built the docker image. Started shell on the container
$ docker run -it khanziaullah/sca bash
root#eb5dc8083aa3:/app/out# ls -l libhostpolicy.so
-rwxr--r-- 1 root root 327384 Sep 13 15:21 libhostpolicy.so
root#eb5dc8083aa3:/app/out# dotnet SelfContainedApp.dll
Hello World!
so I see the libhostpolicy.so file.
Try starting a shell and look for your build and copy location for all the dependencies. COPY command might have gone wrong.
Try to add the following code to the csproj
<PackageReference Include="Microsoft.NETCore.App" Version="1.0.1" />

Categories

Resources