Cannot connect to azure nuget feed when creating a container - asp.net

I have been trying to get docker to pull from a private nuget feed which is hosted within Azure Devops, but I consistently get a 401 unauthorized error. I have looked a numerous other answers and resources, but all of which have not helped. My dockerfile script is the following
# escape=`
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /src
EXPOSE 80
EXPOSE 443
ARG FEED_URL
ARG FEED_ACCESSTOKEN
# Install powershell via dotnet
RUN dotnet tool install --global PowerShell --version 7.0.0
SHELL ["pwsh", "-command"]
## CURRENT ERROR - "-AddNetfx" - NOT CONSIDERED AN ARGUMENT, ALTHOUGH IT IS (CAN BE SEEN IN THE SCRIPT ENTRY) ##
# Install credprovider which is required for accessing private artifact repository
RUN Invoke-WebRequest https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1 -OutFile installcredprovider.ps1; `
.\installcredprovider.ps1 -AddNetfx; `
del installcredprovider.ps1
#################################################################################################################
# Copy csproj and sln files
COPY ["Abstractions/*.csproj", "./Abstractions/"]
COPY ["AdminServicesPortal/*.csproj", "./AdminServicesPortal/"]
COPY ["DataInterface/*.csproj", "./DataInterface/"]
COPY ["Resources/*.csproj", "./Resources/"]
# Need to copy over docker NuGet packages
COPY "AdminServicesPortal/nuget.config" "AdminServicesPortal/"
# Enable session token cache
ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED true
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS `
"{`"endpointCredentials`": [{`"endpoint`":`"https://pkgs.dev.azure.com/ORG/PROJECT/_packaging/FEED/nuget/v3/index.json`", `"username`":`"USER`", `"password`":`"PAT`"}]}"
RUN echo $Env:VSS_NUGET_EXTERNAL_FEED_ENDPOINTS
# Restore dependencies before copying everything else over
RUN dotnet restore "AdminServicesPortal/Portal.csproj"
# Copy the rest of the files required for a build
# Note that this is separate, because we should stop building
# the container if we do not have all required dependencies
COPY Abstractions/. ./Abstractions/
COPY AdminServicesPortal/. ./AdminServicesPortal/
COPY DataInterface/. ./DataInterface/
COPY Resources/. ./Resources/
WORKDIR "/src/AdminServicesPortal"
RUN dotnet build "Portal.csproj" -c Release -o /app/build
# Publish application
FROM build-env AS publish
RUN dotnet publish "Portal.csproj" -c Release -o /app/publish
# Runtime environment for container
FROM build-env AS final
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "MPX.AdminServices.Portal.dll"]
The error I get from Visual Studio is the following
2> Failed to restore C:\src\Resources\Resources.csproj (in 2.99 sec).
2>C:\Program Files\dotnet\sdk\3.1.404\NuGet.targets(128,5): error : Unable to load the service index for source https://pkgs.dev.azure.com/{ORG}/{PROJECT}/_packaging/{FEED}/nuget/v3/index.json. [C:\src\AdminServicesPortal\Portal.csproj]
2>C:\Program Files\dotnet\sdk\3.1.404\NuGet.targets(128,5): error : Response status code does not indicate success: 401 (Unauthorized). [C:\src\AdminServicesPortal\Portal.csproj]
From what I can tell in the link below; my dockerfile script should work
https://github.com/dotnet/dotnet-docker/blob/master/documentation/scenarios/nuget-credentials.md#using-the-azure-artifact-credential-provider
To ensure it wasn't an issue with the PAT of the user not having access, I gave them access to everything across the whole entire organisation. Any help would be greatly appreciated!
Note #1: My current suspicion is that there are some additional parameters I might need to pass, since the private nuget feed is for a project rather than the entire organisation?
Note #2: I ran the following PowerShell command (https://learn.microsoft.com/en-us/azure/devops/artifacts/tutorials/private-powershell-library?view=azure-devops#package-and-publish-the-module) which worked fine, so I am even more confused to why my dockerfile script does not work.
nuget sources Add -Name "PowershellModules" -Source "https://pkgs.dev.azure.com/<org_name>/_packaging/<feed_name>/nuget/v3/index.json" -username "<user_name>" -password "<personal_access_token(PAT)>"

You can check out below workarounds without using NuGet credential plugin.
1, Use dotnet cli to add credentials to the nuget source in your docker file. And then pass the $PAT in the build arguments (ie. --build-arg PAT=$PAT ):
ARG PAT
COPY . .
RUN dotnet nuget add source "your-source-url" --name "source-name" --username "useless" --password "$PAT" --store-password-in-clear-text
RUN dotnet restore
2, You can also try adding the credentials to the config file using the nuget command. And include the nuget.config which has the credentials in the build context. And copy the nuget.config in your docker file . See below:
sources Add -Name "MyPackages" -Source "https://my.pkgs.visualstudio.com/_packaging/MyPackages/nuget/v3/index.json" -username any -password $PAT -ConfigFile Source/Nuget.config -StorePasswordInClearText
Copy the nuget.config in the docker file, You can delete the nuget.config file when the restore is complete:
COPY *.csproj .
COPY ./nuget.config .
RUN dotnet restore
RUN rm nuget.config
You can refer to my answer to this similar thread that using azure nuget feed in the docker container in Azure pipelines.

Related

Deploy Blazor Wasm with Docker from Visual Studio

I want to deploy my Blazor Webassambly App to a Docker Container to run it on my synology NAS.
So my problem is now, that since I want to run my Webassembly App in my VisualStudio IDE with Docker, I have an error "The library 'hostpolicy.dll' required to execute the application was not found in ..."
Has anyone an idea what i could do do solve this error?
Here is the full console output:
docker exec -i -e ASPNETCORE_HTTPS_PORT="55427" -w "C:\app" d274a895523ac47657707b86a7df5ea304df8bfe68330a64b00d9cc357ed1546 "C:\Program Files\dotnet\dotnet.exe" --additionalProbingPath c:\\.nuget\\fallbackpackages2 --additionalProbingPath c:\\.nuget\\fallbackpackages "C:\app\bin\Debug\net5.0\Jordan_Webapp_Client.dll"
Cannot use file stream for [C:\app\bin\Debug\net5.0\Jordan_Webapp_Client.deps.json]: No such file or directory
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\app\bin\Debug\net5.0\'.
Failed to run as a self-contained app.
- The application was run as a self-contained app because 'C:\app\bin\Debug\net5.0\Jordan_Webapp_Client.runtimeconfig.json' was not found.
- If this should be a framework-dependent app, add the 'C:\app\bin\Debug\net5.0\Jordan_Webapp_Client.runtimeconfig.json' file and specify the appropriate framework.
docker exec -i -e ASPNETCORE_HTTPS_PORT="55427" -w "C:\app" d274a895523ac47657707b86a7df5ea304df8bfe68330a64b00d9cc357ed1546 "C:\Program Files\dotnet\dotnet.exe" --additionalProbingPath c:\\.nuget\\fallbackpackages2 --additionalProbingPath c:\\.nuget\\fallbackpackages "C:\app\bin\Debug\net5.0\Jordan_Webapp_Client.dll"
Cannot use file stream for [C:\app\bin\Debug\net5.0\Jordan_Webapp_Client.deps.json]: No such file or directory
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\app\bin\Debug\net5.0\'.
Failed to run as a self-contained app.
- The application was run as a self-contained app because 'C:\app\bin\Debug\net5.0\Jordan_Webapp_Client.runtimeconfig.json' was not found.
- If this should be a framework-dependent app, add the 'C:\app\bin\Debug\net5.0\Jordan_Webapp_Client.runtimeconfig.json' file and specify the appropriate framework.
And here is my Docker File:
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["Jordan_Webapp_Client/Jordan_Webapp_Client.csproj", "Jordan_Webapp_Client/"]
RUN dotnet restore "Jordan_Webapp_Client/Jordan_Webapp_Client.csproj"
COPY . .
WORKDIR "/src/Jordan_Webapp_Client"
RUN dotnet build "Jordan_Webapp_Client.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Jordan_Webapp_Client.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Jordan_Webapp_Client.dll"] ```
If you're using a private nuget feed, try changing the "RUN dotnet restore..." command.
Example:
RUN dotnet restore -s "https://your-nuget-feed.example.com/nuget" -s "https://api.nuget.org/v3/index.json" "Jordan_Webapp_Client/Jordan_Webapp_Client.csproj"

Errors installing microsoft-net-sdk-blazorwebassembly-aot in Dockerfile

I'm trying to run dotnet workload install microsoft-net-sdk-blazorwebassembly-aot from my Dockerfile while building my Blazor project in order to enable AOT WASM compilation.
Here is the relevant Dockerfile code:
FROM mcr.microsoft.com/dotnet/sdk:6.0-focal-amd64 AS builder
...
RUN dotnet workload install microsoft-net-sdk-blazorwebassembly-aot
RUN dotnet publish -c Release
However, this fails with an error like:
Updated advertising manifest microsoft.net.sdk.ios.
Updated advertising manifest microsoft.net.sdk.maccatalyst.
Updated advertising manifest microsoft.net.sdk.macos.
Updated advertising manifest microsoft.net.workload.mono.toolchain.
Updated advertising manifest microsoft.net.sdk.tvos.
Updated advertising manifest microsoft.net.sdk.android.
Installing workload manifest microsoft.net.sdk.ios version 14.5.100-preview.5.894.
Workload installation failed, rolling back installed packs...
Installing workload manifest microsoft.net.sdk.ios version 14.5.100-preview.5.881.
Installation roll back failed: Failed to install manifest microsoft.net.sdk.ios version 14.5.100-preview.5.881: The transaction has aborted..
Workload installation failed: Failed to install manifest microsoft.net.sdk.ios version 14.5.100-preview.5.894: Invalid cross-device link.
I also tried with --skip-manifest, and this appears to install the workload, but then fails to perform the AOT build.
RUN dotnet workload install microsoft-net-sdk-blazorwebassembly-aot --skip-manifest-update
Installing pack Microsoft.NETCore.App.Runtime.Mono.browser-wasm version 6.0.0-preview.5.21301.5...
Writing workload pack installation record for Microsoft.NETCore.App.Runtime.Mono.browser-wasm version 6.0.0-preview.5.21301.5...
...
Garbage collecting for SDK feature bands 6.0.100...
Successfully installed workload(s) microsoft-net-sdk-blazorwebassembly-aot.
RUN dotnet publish -c Release
...
Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink
AOT'ing 50 assemblies
/usr/share/dotnet/packs/Microsoft.NET.Runtime.WebAssembly.Sdk/6.0.0-preview.5.21301.5/Sdk/WasmApp.targets(507,5): error MSB3073: The command "emcc --version" exited with code 1.
Any advice?
Morgan, you're hitting two issues:
On the skip-manifest (which is the workaround) is issue https://github.com/dotnet/sdk/issues/18450
The emcc --version error is issue https://github.com/dotnet/runtime/issues/54342 and resolved by installing Python first...see example: https://github.com/timheuer/PictureFixer/blob/docker/PictureFixer/Dockerfile#L11-L12 (on docker branch in the repo)
You will then hit a different bug (could not find cross compiler) which is issue https://github.com/dotnet/runtime/pull/54651 (which has been checked in).
Once #3 is fixed in an image you can consume, it should work ensuring you add #2 in your script. Example full Docker file:
FROM mcr.microsoft.com/dotnet/nightly/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
#FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
FROM mcr.microsoft.com/dotnet/nightly/sdk:6.0 AS build
COPY ["nuget.config", "nuget.config"]
RUN apt-get update
RUN apt-get install --no-install-recommends --yes python3
RUN dotnet workload install microsoft-net-sdk-blazorwebassembly-aot --skip-manifest-update
WORKDIR /src
COPY ["Server/PictureFixer.Server.csproj", "Server/"]
COPY ["Shared/PictureFixer.Shared.csproj", "Shared/"]
COPY ["Client/PictureFixer.Client.csproj", "Client/"]
RUN dotnet restore "Server/PictureFixer.Server.csproj"
COPY . .
WORKDIR "/src/Server"
RUN dotnet build "PictureFixer.Server.csproj" -c Release -o /app/build --nologo
FROM build AS publish
RUN dotnet publish "PictureFixer.Server.csproj" -c Release -o /app/publish --nologo
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "PictureFixer.Server.dll"]
You can temporarily work around #3 by adding this to your Blazor Wasm project csproj:
<PropertyGroup>
<_MonoAotCrossCompilerPath>#(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier','browser-wasm'))</_MonoAotCrossCompilerPath>
</PropertyGroup>

Docker Error: failed to register layer: Error processing tar file(exit status 1): "...msader15.dll.mui: no such file or directory"

I am trying to build my ASP.NET 4.7.2 WEB Services into a docker file, however when I run
docker build -t manufacturing-portal-api:latest .
I get the following error:
failed to register layer: Error processing tar file(exit status 1): link /Files/Program Files/common files/system/ado/en-US/msader15.dll.mui /Files/Program Files (x86)/common files/system/ado/en-US/msader15.dll.mui: no such file or directory=> => sha256:d9e8b01179bfc94a5bdb1810fbd76b999aa52016001ace2d3a4c4bc7065a9601 1.66GB / 1.66GB 0.0s
This is my docker file
FROM microsoft/dotnet-framework:4.7.2-sdk-windowsservercore-1803 AS build
WORKDIR /app
# copy csproj and restore as distinct layers
COPY *.sln .
COPY WebPortalAPI/*.csproj ./WebPortalAPI/
COPY WebPortalAPI/*.config ./WebPortalAPI/
COPY ./instantclient_11_2 .
ENV PATH=./instantclient_11_2
ENV TNS_ADMIN=./instantclient_11_2/network/admin
RUN nuget restore
# copy everything else and build app
COPY WebPortalAPI/. ./WebPortalAPI/
WORKDIR /app/WebPortalAPI
RUN msbuild /p:Configuration=Release
FROM microsoft/aspnet:4.7.2-windowsservercore-1803 AS runtime
WORKDIR /inetpub/wwwroot
COPY --from=build /app/WebPortalAPI/. ./
please advise
Your Docker host is likely configured to run Linux containers inside of a VM. To run Windows containers, you need to "Switch to Windows containers" in the Docker menu. Documentation on that is available here.

Install .NET Runtime on .NET Core 3.1 Nano Server Docker Image

I have a .NET Core 3.1 console app that uses the Microsoft.NET.Sdk.WindowsDesktop project SDK because it references Geometry3D in System.Windows.Media.Media3D.
I would like to run this in a Docker container on Windows Nano Server, but I receive the following error when executing docker run:
It was not possible to find any compatible framework version
The framework 'Microsoft.WindowsDesktop.App', version '3.1.0' was not found.
- No frameworks were found.
You can resolve the problem by installing the specified framework and/or SDK.
The specified framework can be found at:
- https://aka.ms/dotnet-core-applaunch?framework=Microsoft.WindowsDesktop.App&framework_version=3.1.0&arch=x64&rid=win10-x64
How do I go about installing the required sdk and runtime?
Here is my existing Dockerfile:
FROM mcr.microsoft.com/dotnet/core/runtime:3.1-nanoserver-1903 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["HelloWpfCore/HelloWpfCore.csproj", "HelloWpfCore/"]
RUN dotnet restore "HelloWpfCore/HelloWpfCore.csproj"
COPY . .
WORKDIR "/src/HelloWpfCore"
RUN dotnet build "HelloWpfCore.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "HelloWpfCore.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "HelloWpfCore.dll"]
GitHub repo is here: https://github.com/tonysneed/hello-netcore-wpf-nano
I figured it out. I created an image that installs the Windows Desktop runtime on Nano Server base image.
# escape=`
# Installer image
FROM mcr.microsoft.com/windows/servercore:1909 AS installer
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
# Retrieve .NET Core Runtime
# USER ContainerAdministrator
RUN $dotnet_version = '3.1.5'; `
Invoke-WebRequest -OutFile dotnet-installer.exe https://download.visualstudio.microsoft.com/download/pr/86835fe4-93b5-4f4e-a7ad-c0b0532e407b/f4f2b1239f1203a05b9952028d54fc13/windowsdesktop-runtime-3.1.5-win-x64.exe; `
$dotnet_sha512 = '5df17bd9fed94727ec5b151e1684bf9cdc6bfd3075f615ab546759ffca0679d23a35fcf7a8961ac014dd5a4ff0d22ef5f7434a072e23122d5c0415fcd4198831'; `
if ((Get-FileHash dotnet-installer.exe -Algorithm sha512).Hash -ne $dotnet_sha512) { `
Write-Host 'CHECKSUM VERIFICATION FAILED!'; `
exit 1; `
}; `
`
./dotnet-installer.exe /S
# Runtime image
FROM mcr.microsoft.com/windows/nanoserver:1909
ENV `
# Enable detection of running in a container
DOTNET_RUNNING_IN_CONTAINER=true
# In order to set system PATH, ContainerAdministrator must be used
USER ContainerAdministrator
RUN setx /M PATH "%PATH%;C:\Program Files\dotnet"
USER ContainerUser
COPY --from=installer ["/Program Files/dotnet", "/Program Files/dotnet"]
I pushed this to my repo on Docker Hub: https://hub.docker.com/repository/docker/tonysneed/dotnet-runtime-windowsdesktop
Then I simply used this custom image as the base image for my sample app: https://github.com/tonysneed/hello-netcore-wpf-nano

MSBUILD : error MSB1011: Specify which project or solution file to use because this folder contains more than one project or solution file

I am new to docker and trying to create a Dockerfile for ASP.NET Core application. Can someone suggest what changes do I require?
Here is my Dockerfile:
FROM microsoft/dotnet:2.1-sdk
WORKDIR /app
COPY Presentation/ECCP.Web/ *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "api.dll"]
I am facing the following error:
MSBUILD : error MSB1011: Specify which project or solution file to use
because this folder contains more than one project or solution file.
The command '/bin/sh -c dotnet publish -c Release -o out' returned a
non-zero code: 1
It looks like your work dirctory contains both .csproj and .sln files. Try to specify the .sln file in the command.
Run
dotnet publish your-solution-file.sln -c Release -o out
I had the same error message with dotnet build and this solves it.
By the way, since .NET Core 2.0 the dotnet restore command is run implicitly, so you may skip it.

Resources