.Net Dockerizing excluding build stage in the dockerfile - asp.net

I am new to docker and AKS, I am working on a .Net project that is migrated from .Net core 3.1 to .Net 6 , I am trying to containerize and deploy to AKS. First i wanted to test in the local machine so i had setup docker in the macbook m1 and followed this link https://learn.microsoft.com/en-us/visualstudio/mac/docker-quickstart?view=vsmac-2022 to create a dockerfile, I am able to run the dockerfile from Visual Studio if i follow the steps given in the link but if i run docker compose command from terminal i am getting issues with SDK version, however what i am trying to achieve is remove the build stage and directly run the .net application as the build stage is taken care by Jenkins and the DLL is placed in a folder
Here is the complete dockerfile with all the stages
FROM mcr.microsoft.com/dotnet/core/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:6.0 AS build
WORKDIR /src
COPY DockerDemo/DockerDemo.csproj DockerDemo/
RUN dotnet restore "DockerDemo/DockerDemo.csproj"
COPY . .
WORKDIR "/src/DockerDemo"
RUN dotnet build "DockerDemo.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DockerDemo.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerDemo.dll"]
I modified dockerfile excluding build stage, I am taking DLL file from a "lib/net6.0" that is built by jenkins and placed in this folder
FROM mcr.microsoft.com/dotnet/core/aspnet:6.0 AS base
COPY ["lib/net6.0","/src/"]
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM base as final
WORKDIR /app
COPY --from=base /src/ .
ENTRYPOINT["dotnet","DockerDemo.dll"]
If i build the dockerfile the image is getting created but when i run I am getting following error
unhandled exception system.io.filenotfoundexception could not load
file or assembly 'NLog, Version=.5.0.0.0
The project is using NLog.Web.ASPNetCore 5.1.4 version, i tried downgrading it to 5.0.0 but i am still getting the same error, i also tried adding just NLog 5.0.0 package but no luck
As i said i am new to Docker and also to the .net development on macbook
My Questions
Q1) Is it possible to exclude the build stage in dockerfile and directly run
Q2) Does the macbook m1 completely supports .Net Core and .Net 6 development so that it can be deployed to AKS via Jenkins pipeline

Related

What could cause a "TypeError: (0 , d.__decorate) is not a function" of an angular function inside a docker container?

I'm building a web application. The frontend is in angular and the backend in .Net Core.
Currently, I'm doing the following, to build my final docker image:
Build angular app
Copy the built angular files in the wwwroot of asp.net core
Building the asp.net core app
Publishing the asp.net core app
Copying the publish result inside a docker image.
Here is my current dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM node:latest as node
WORKDIR /app
COPY Frontend .
RUN npm install
RUN npm run build
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Backend/my-project.csproj", "Backend/"]
RUN dotnet restore "Backend/my-project.csproj"
COPY Backend Backend
WORKDIR "/src/Backend"
COPY --from=node /app/dist/app/ /wwwroot/
RUN dotnet build "my-project.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "my-project.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "my-project.dll"]
The whole process finish without errors, but when I'm trying to access to http://localhost/, I get the html, but there is a javascript error:
Uncaught TypeError: (0 , d.__decorate) is not a function
at Object.619 (main.3ba20fe70ead0a4b.js:1:45137)
at r (runtime.834ae414fca175dc.js:1:127)
at Object.1983 (main.3ba20fe70ead0a4b.js:1:45670)
at r (runtime.834ae414fca175dc.js:1:127)
at main.3ba20fe70ead0a4b.js:1:512504
at n (runtime.834ae414fca175dc.js:1:2649)
at main.3ba20fe70ead0a4b.js:1:57
But:
the result of ng serve work
serving with a test server the result of npm build works
Copying the result of npm build to wwwroot of my asp.net core works in debug
Publishing the asp.net core app(with the wwwroot populated) also works
When I try to access to each individual file present in my npm build folder, it seems to be accessible(not sure, because names have this number that is not exactly the same)
If I deleted nodes_modules, packages-lock.json and run npm install and npm run build, it also works
I deleted the angular app, created another one, run the whole docker build, it works.
I tried to change node to the version I'm using on my computer(node 16.14)
Edit
I started to remove everything from my app to see when it starts working again. It appears that once I remove my State(s) of NgxsModule.forFeature([]), it works.
So basically, if I've a module with:
NgxsModule.forFeature([])
It works, but NgxsModule.forFeature([UiState]) doesn't, even if UiState is almost empty:
#State<UiStateModel>({
name: 'ui',
defaults: {
isMenuOpen: true,
},
})
#Injectable()
export class UiState {}
I'm kind of desperate, any idea what could cause this error?
I'm not exactly sure what did the trick, but I finally solved it.
What I did:
I had some ngxs packages that were refered as dev dependencies AND dependencies(with different version), I kept only the one in the dependencies
In my tsconfig.json, I was having a paths specified for "tslib"(to the nodes_modules one). I remember I added it, but I don't remember why, I think at some point I was having a compilation error and I found on SO this solution. I did remove this path for tslib, which is what probably solve the issue.

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

Resources