dotnet restore not working from inside a dockerfile - c#

I am trying to run RUN dotnet restore --configfile NuGet.config Ithought.Web.sln from a dockefile which looks like:-
FROM www.image.from:5000/a/private.docker-registry:1 AS build
WORKDIR /delivery
# Copy csproj and sln and restore as distinct layers
COPY NuGet.config .
COPY Ithought.Web/Ithought.Web.csproj Ithought.Web/
COPY Ithought.Web.Delivery/Ithought.Web.Delivery.csproj Ithought.Web.Delivery/
COPY Ithought.Web.sln .
RUN dotnet restore --configfile NuGet.config Ithought.Web.sln //it fails here
but docker build fails with the following error:-
ERROR [build 7/10] RUN dotnet restore --configfile NuGet.config Ithought.Web.sln 1.5s
------
> [build 7/10] RUN dotnet restore --configfile NuGet.config Ithought.Web.sln:
#14 1.190 /usr/share/dotnet/sdk/2.2.207/NuGet.targets(246,5): error MSB3202: The project file "/Smidge/src/Smidge/Smidge.csproj" was not found. [/delivery/Ithought.Web.sln]
However when I try dotnet restore --configfile NuGet.config Ithought.Web.sln from outside of the dockerfile it works fine
Please note that along with the public nuget feed I am also trying to restore packages from a private nuget feed and I am wondering if docker is able to authenticate with the private nuget although I have the private nuget's credentials added to the nuget.config file. I have seen a lot of similar questions on the internet but I have not come across anyone who is trying to do a dotnet restore from within a dockerfile from a private nuget

Related

WARNING: Could not resolve this reference. Could not locate the assembly "PackageName". Check to make sure the assembly exists on disk

need a little help resolving this issue I'm facing when building my ASP.NET project that references a local class library inside a docker container.
I'm using:
ASP.NET 6.0
Docker version 20.10.21, build baeda1f
dotnet sdk 6.0
When I try to run
docker build .
I get the following error:
=> ERROR [build 7/7] RUN dotnet build "ProjectName.csproj" -c Debug -o /app/build 1.9s
------
> [build 7/7] RUN dotnet build "ProjectName.csproj" -c Debug -o /app/build:
#15 0.434 MSBuild version 17.3.2+561848881 for .NET
#15 0.655 Determining projects to restore...
#15 0.885 All projects are up-to-date for restore.
#15 1.042 /usr/share/dotnet/sdk/6.0.404/Microsoft.Common.CurrentVersion.targets(2302,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "PackageName". Check to make sure the assembly exists on disk. If this reference is required
by your code, you may get compilation errors. [/src/ProjectName/ProjectName.csproj]
#15 1.847 CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/ProjectName/ProjectName.csproj]
#15 1.852
#15 1.852 Build FAILED.
#15 1.852
#15 1.852 /usr/share/dotnet/sdk/6.0.404/Microsoft.Common.CurrentVersion.targets(2302,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "PackageName". Check to make sure the assembly exists on disk. If this reference is required
by your code, you may get compilation errors. [/src/ProjectName/ProjectName.csproj]
#15 1.852 CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/ProjectName/ProjectName.csproj]
#15 1.852 1 Warning(s)
#15 1.852 1 Error(s)
#15 1.852
#15 1.852 Time Elapsed 00:00:01.38
------
executor failed running [/bin/sh -c dotnet build "ProjectName.csproj" -c Debug -o /app/build]: exit code: 1
The dockerfile I'm using
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 ["ProjectName.csproj", "ProjectName/"]
RUN dotnet restore "ProjectName/ProjectName.csproj"
COPY .. .
WORKDIR "/src/ProjectName"
RUN dotnet build "ProjectName.csproj" -c Publish -o /app/build
FROM build AS debug
RUN dotnet publish "ProjectName.csproj" -c Publish -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProjectName.dll"]
The .csproj file
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
....
</ItemGroup>
<ItemGroup>
<Reference Include="PackageName">
<HintPath>..\..\PackageName\PackageName\bin\Debug\netstandard2.1\PackageName.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
Any thoughts on me getting the dockerfile to fetch the package .dll on build? or is it the wrong way to go about getting what I want?
I am open to hosting the package on nuget but for the time being this dockerfile is for dev purposes only.
Keep in mind that the package is at the right location locally, as building the project using:
dotnet build ProjectName.csproj
works fine
P.S. I replaced the package name with "PackageName" and the project name with "ProjectName".
I tried building the dockerfile for ASP.NET 6.0 and I expected it to pass, but I got an Error instead.
So I solved this almost immediately, I made the class library build to the location with my asp.net project, cleared the docker cache, and built again.
Id project located on your computer, You need to copy xxx.dll after define
SWORKDIR /src
COPY ["Xxx.csproj", "."]
COPY ["output/Debug/net6.0/xxx.dll", "."]
Otherwise you have to create nuget package the dll then you install from nuget

Building docker image with package from private nuget feed fails

I'm using the default Dockerfile generated by Visual Studio 2022 for my .NET Core 6 razor pages app. The app is using DevExpress ASP.NET Core Controls. I downloaded the necessary packages using the private Nuget feed URL supplied by the package vendor.
This is how the Dockerfile looks like:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["NuGet.Config", "."]
COPY ["DMS.Monitor/DMS.Monitor.csproj", "DMS.Monitor/"]
RUN dotnet restore "DMS.Monitor/DMS.Monitor.csproj"
COPY . .
WORKDIR "/src/DMS.Monitor"
RUN dotnet build "DMS.Monitor.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DMS.Monitor.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DMS.Monitor.dll"]
When I try to build the image I get the following error:
#13 [build 5/8] RUN dotnet restore "DMS.Monitor/DMS.Monitor.csproj"
#13 sha256:f1029f328e1221cc75d3cbca3603f770dbb670711688ac30334449923e9ceb3f
#13 0.893 Determining projects to restore... #13 106.8 /src/DMS.Monitor/DMS.Monitor.csproj : error NU1102: Unable to find
package DevExtreme.AspNet.Core with version (>= 21.2.8) #13 106.8
/src/DMS.Monitor/DMS.Monitor.csproj : error NU1102: - Found 3
version(s) in nuget.org [ Nearest version: 20.1.3 ] #13 107.1
Failed to restore /src/DMS.Monitor/DMS.Monitor.csproj (in 1.77 min).
#13 ERROR: executor failed running [/bin/sh -c dotnet restore "DMS.Monitor/DMS.Monitor.csproj"]: exit code: 1
My project uses DevExtreme.AspNet.Core v.21.2.8 which is obviously not available in nuget.org. Is there a way I can edit the NuGet.Config file to tell Docker where it can find the correct version of the package?
You should be able to add your private feed using the command
dotnet nuget add source <package source>
Place it before the dotnet restore command in your Dockerfile.

Azure devops private nuget takes a long time to find package

We are using Azure DevOps pipelines. We have a step that publishes the our private nuget package to the Azure Artifacts. However, the build breaks at other steps because the nuget package (that was published on previous steps) is not found. The strange thing is that after the package is published, I can see it in the package-manager console or on Visual Studio and even in the Artifacts in Azure DevOps. But for some reason, the pipeline doesn't find the package. After 30-50min, I re-run the pipeline and then it finds the package.
What could be happening to take so long time for the pipeline to find the my package?
Edit 1:
This is my yaml for the step with error
- script: |
pwd && ls -la
dotnet restore "$(solution_path)" $(nuget_args)
dotnet publish -c Release -o $(System.DefaultWorkingDirectory)/bin "$(main_project_path)"
mkdir artifact
cp -r $(System.DefaultWorkingDirectory)/bin artifact/bin
displayName: Build Application
The error is:
/data/vstsagent/user/389/s/src/MyProject.csproj
: error NU1102: Unable to find package MyPackage with
version (>= 2.1.0) [/data/vstsagent/user/389/s/src/MySolution.sln]
/data/vstsagent/user/389/s/src/MyProject.csproj
: error NU1102: - Found 28 version(s) in MyPrivateRepository [ Nearest
version: 2.1.0-preview.6 ]
[/data/vstsagent/user/389/s/src/MySolution.sln]
It is not clear what feed is used by dotnet restore command.
Confirm that you have configured your custom nuget feed in nuget.config and using it.
Try adding "--verbose" switch in dotnet restore command and check if your feed is used for restoring packages.
Use the --no-cache option so dotnet restore doesn't use the local cache.

AspNetCore project gives System.IO.FileNotFoundException only on linux container

I've got a dotnet core project running AspNetCore webserver. I use a couple of other DLLs which is are fairly simple class libaries.
I can download the repository from git onto my windows PC, go in and run:
dotnet restore
dotnet run
And everything works fine.
However if I do the same thing in a docker container based on microsoft/aspnetcore-build:1.0.7, I get the following error on an HTTP PUT:
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HLBKHRVH7OND": An unhandled exception was thrown by the application.
System.IO.FileNotFoundException: Could not load file or assembly 'KolData, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
Now the file Koldata.dll does exist in the git repository and it's there in the bin/Debug/netcoreapp1.1 folder.
I can re-create the error in Windows by deleting the KolData.dll file in the build directory. So it seems to be that dotnet core on Linux cannot see that file, and I'm unsure why.
I've even tried replacing the DLL with a version built on the machine from source, and it still brings the same error.
One solution
I managed to get it working by changing the csproj file's target framework from:
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
</PropertyGroup>
to:
<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
</PropertyGroup>
This feels a bit strange since KolData.dll is running on 1.1
But now it runs without the error.
You have to create Dockerfile and build docker image.
EDIT:
An example of what Dockerfile should look like. The following file builds Visual Studio project and creates docker image.
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY WebApplication/WebApplication.csproj WebApplication/
RUN dotnet restore
COPY . .
WORKDIR /src/WebApplication
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApplication.dll"]
If you have already built your application include .dll in container with Dockerfile:
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "application.dll"]
Build and run your app:
docker build -t application
docker run -d -p 8000:80 application
Building Docker Images for .NET Core Applications

An assembly specified in the application dependencies manifest was not found:

I developed application in asp.net-core 2.0 preview1.
I developed on windows with Visual Studio 2017.
Now I want to deploy it to Linux server using Docker.
I created Docker file:
FROM microsoft/aspnetcore:2.0
ARG source
WORKDIR /app
EXPOSE 44305
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "Aplication.dll"]
After that running commands:
dotnet build -o obj/Docker/publish -c Release
dotnet publish -o obj/Docker/publish -c Release
docker build -t testapi-api .
docker run -p 44305:80 --name api testapi-api
Afer the last command run I am getting next error:
An assembly specified in the application dependencies manifest (Aplication.deps.json) was not found:
package: 'Microsoft.AspNetCore.Antiforgery', version: '2.0.0-preview1-final'
path: 'lib/netcoreapp2.0/Microsoft.AspNetCore.Antiforgery.dll'
This assembly was expected to be in the local runtime store as the application was published using the following target manifest files:
manifest.win7-x64.xml;manifest.win7-x86.xml;manifest.osx-x64.xml;manifest.linux-x64.xml
I am new with asp.net-core and especially with Docker. So any help with this is great.
You need to specify -r linux-x64 parameter in dotnet publish command like that:
dotnet publish -o obj/Docker/publish -c Release -r linux-x64
This will make a standalone deployment.
Try to use this image "2.0.0-preview1". Basically change the first line to FROM microsoft/aspnetcore:2.0.0-preview1, if your local has preview 1 dotnet core.
If it not works, check your local dotnet core version, it it points to 2.0.0-preview2-final, then change all your references pointing to 2.0.0-preview2-final in csproj file, then use the 2.0.0-preview2 image. It would help you I hope.

Categories

Resources