Is it possible to run my development asp.net core server out of a docker container - asp.net

When developing node.js projects I will normally use docker compose to bring up an entire stack of docker containers in order to develop on my local host. I mount volumes in and use nodemon to automatically update the running server. This works well. However, it does not seem to work quite the same way with asp.net core.
Is it possible to run a development container for an asp.net core application? I would like to have the server restart when there is a change on the host machine to a file. I know you can do this with the dotnet watch run command but I'm yet to see it working inside a running docker container. How can I get this working within a docker compose stack? What docker base image should I be using? Ideally this solution will work from the command line without using visual studio as I'm using Visual Studio code on a mac.

A little bit late to the party, but maybe it will help someone.
Here is the sample project setup that will leverage dotnet watch command
in a docker container, while source code is being edited on the host system.
As far as I know dotnet watch now is a global tool so you can use it with
different versions of .netcore.
Here is the documentation for
dotnet watch command.
Sample project overview
Here is a small example project for demo purposes, made with help of minimal api.
It has one endpoint /freshbread which returns "Baked bread".
Folder structure:
Bakery
│ Dockerfile
└───src
│───Bakery.csproj
└───Program.cs
Content of Program.cs:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/freshbread", () => "Baked bread");
app.Run("http://*:80");
Content of Bakery.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
Content of Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:6.0
WORKDIR /app
ENTRYPOINT ["dotnet", "watch"]
Based on a Microsoft .net core SDK image. It just defines an /app workdir and launches there dotnet watch command.
The "trick" here is to mount into /app folder your source code from the host machine and let container handle compilation
and server restart, so:
Build an image (from Bakery folder):
docker build -t dev-container .
Run a container:
docker run \
--rm \
--detach \
--volume /path/to/the/Bakery/src:/app \
--publish 5000:80
dev-container
Publish port outside, so api will be accessible from the host machine.
Now, container is watching files in mounted folder and whenever there are chages
it will perform hot-reload.
Demo
Here is a short demo of this setup, note that top and bottom-right splits are host machine terminals and bottom-left split is log of the container.

Related

How do I add a webjob to my aspnet appservice using only the CLI

I am working on macOS with an external text editor (NOT VisualStudio).
I have an asp.net project that I push to my app service using
git push azure main:master
the remote is configured as such https://$name:pass#name.scm.azurewebsites.net:443/name.git
when I go to the web jobs section in the app service to add a web job, I get the error message:
I want to continue using my source control push method while developing...
however I would also like to add a csharp console webjob .
I have followed the tutorial here (A) https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-get-started, and have a functoining console app that consumes a message from a queue.
How can I push this extra console app to my Kudu instance?
I read here that there is a 'hidden' structure not in the main doucmentation (A) I linked above for the tutorial: https://github.com/projectkudu/kudu/wiki/WebJobs.
Do I have to manually zip the result of dotnet publish -c Release to some Kudu folder? Or is there a more elegant way by somehow including this project inside my git repo that I push normally... or using an az cli tool to push the webjob to the app?
I created the Web Job and tried to Add it from App service, got the below error.
Using Azure CLI, we can deploy our Web Jobs to App Service
Web Jobs need to be deployed to the below folder in KUDU Console, Create a folder with Web Job Name in the below path
app_data\jobs\triggered\NewFolder
Zip all the contents of the Web Job into one folder.
wwwroot folder path will be read-only mode, we need to enable it by setting WEBSITE_RUN_FROM_PACKAGE to 1
Set the WEBSITE_RUN_FROM_PACKAGE to 1 in WebApp => Configuration => Application Settings or run the CLI command to set it.
az webapp config appsettings set --resource-group YourRGName --name AppServiceName --settings WEBSITE_RUN_FROM_PACKAGE="1"
Zip deploy Web Job to the existing Azure App service
az webapp deployment source config-zip --resource-group YourRGName --name YourAppService --src YourZipFilePath
We can directly drag and drop the Web Jobs Zip folder from the KUDU Console as well
We can deploy our Web Jobs in many ways.
We can FTP / Use Build Pipelines/ Zip Deploy.
Found a way to push directly using az cli
az webapp deploy --name $APP_NAME -g $RG \
--src-path program.py \
--type static \
--target-path /home/site/Jobs/continuous/Job$RANDOM/program.py
for larger programs in .net I just dumped the project on Kudu, unzipped, and rebuilt (I'm on a mac m1 so can't build against x64 without some hoops, this was easier workaround)
# on my machine
az webapp deploy --name $APP_NAME -g $RG \
--src-path path_to_dotnet_project.zip \
--type static \
--target-path /home/site/Jobs/continuous/Job5/myproject.zip
then on the app service (using kudu cmd/powershell)
cd /home/site/Jobs/continuous/Job5
unzip myproject.zip
dotnet build -c Release
cp bin/release/net6.0/* /home/site/Jobs/continuous/MyNewJob
#job in MyNewJob automatically starts since this is in the 'continuous' folder

CrashLoopBackOff when trying to run .Net Applications in AKS cluster accross 2 pods

Apologies from the start but please bear with me, I am still rather novice at this so if you see any issues glaringly obvious, please forgive me.
I am working at a company where some devs are trying to have us deploy some .NET Core applications to containers in Azure Kubernetes Service (AKS). From my understanding, they have been written in .NET Core 3.1. The goal is to run this process using a CI/CD Azure Pipeline, using Azure Repos as repository, using a build pipeline to create the docker image, push image to our Azure Container Registry and create an artifact for the release pipeline to then deploy (using helm) contianers into the AKS.
File Structure is as follows:
Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
COPY ["AppFolder1\App.csproj", "."]
RUN dotnet restore "AppFolder1\App.csproj"
COPY . .
RUN dotnet build "AppFolder1\App.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "AppFolder1\App.csproj" -c Release -o /app/publish
FROM base AS final
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DotNet.Docker.dll"]
ERROR
Question: Could there be an issue with 6.0 sdk when trying to deploy app made with .net core 3.1?
running "kubectl get pods -n redacted-namespace"
a) retrieves two pods with CrashLoopBackOff Status showing 9 restarts
running "kubectl define pod -n redacted-namespace" retrieves information on pods
a) both pods show successful pod scheduling - Successfully assigned redacted-namespace/ to aks-nodepool1-02 AND aks-nodepool1-00
b) Both show multiple successful pull of image
c) Both show creation of container and start of container
d) End message:
Warning BackOff 58s (x117 over 26m) kubelet Back-off restarting failed container
--ATTEMPTS TO SOLVE--
It was suggested that the Dockerfile was to blame. Spent time creating and running pipeline with multiple iterations of dockerfile, including changing .net versioning to 3.1 from 6.0. No successful pipelines using these dockerfiles yet.
running kubectl logs <pod-name> -n redacted-namespace:
Could not execute because the application was not found or a compatible .NET SDK is not installed.
Possible reasons for this include:
* You intended to execute a .NET program:
The application 'DotNet.Docker.dll' does not exist.
* You intended to execute a .NET SDK command:
It was not possible to find any installed .NET SDKs.
Install a .NET SDK from:
https://aka.ms/dotnet-download
I had figured that the installation of the .NET SDK should have been handled by the dockerfile line 1, however it doesn't seem to be working properly. In the meantime, adding in pipeline release Agent Task Use .NET Core sdk 6.0 and deleting previous pods to try again.
Re-running pipeline release - No effect. Likely .NET Core SDK install agent task does not work inside of each pod and is therefore not available as an installed resource within pods and replicas.
Apparently there were TWO problems with the Dockerfile. The first and foremost, #Hans Kilian, you're absolutely right. Apparently they were using .NET 3.1. The other issue was the ENDPOINT I had set up was not pointing to the right .dll file. This I found by going to Solutions/App.sln and pulled the name from the Project line (something like Project("################################")= "Project_name"... Its working and running just fine now. Thank you!

Containerrizing Hosted Blazor WebAssembly for Raspberry Pi Docker Swarm

I’m developing a DotNet Core hosted Blazor WebAssembly, as a frontend to my backend ASPNet Core API, running on Raspberry Pi’s, containerised in a Docker Swarm.
I’m developing on a MacBook Pro, using VSCode for Mac, and it’s really a great tool. I created the solution as “dotnet new blazorwasm —hosted” and got the solution created and build.
I have installed Docker Desktop, and created a Buildx builder for arm, which works great with all my other solutions (DotNet Core API, DotNet Background solutions), but with the hosted blazorwasm solution, I run into several problems, probably caused by my lacking knowledge on setting up build options.
So I installed Visual Studio 2019 (I have used VS on Windows for 20 years) and was actually surprised over the look and feel, a really great tool.
I created the hosted Blazor WebAssembly solution, which works great om Mac, and even the Docker desktop integration work without problems.
Building the Docker Image was easy, I just used my Buildx builder, and executed below command from the command line:
“docker buildx build --file ./SnakeConsole/Server/Dockerfile --platform linux/arm/v7 -t jagdriver/wavesnake:SnakeConsole3 --push .”
On the Raspberry Swarm, I then created the Stack/Container. The Stack installed but the container refused to start, and the log was:
standard_init_linux.go:211: exec user process caused “exec format error”.
I have seen this error before, and as far as I remember I added “-r ubuntu.19.10-arm” to the dotnet publish command to ensure that the code is generated for linux-arm.
So I tried to add “-r ubuntu.19.10-arm” to the dotnet publish command in below dockerfile, and execute the Docker buildx build command again, but then the Build Engine run into below error.
project.assets.json' doesn't have a target for '.NETCoreApp,Version=v5.0/browser-wasm'. Ensure that restore has run and that you have included 'net5.0' in the TargetFrameworks for your project. You may also need to include 'browser-wasm' in your project's RuntimeIdentifiers
Can anyone out the give me a helping hand on this, thanks in advance.
FACTS:
The solution I’m mentioning is the standard Hosted Blazor
WebAssembly template without any changes.
The target framework is .Net 5.0
Visual Studio for Mac community is version 8.7.2 (build 4)
Docker file from the default hosted Blazor WebAssembly template.
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY SnakeConsole/Server/SnakeConsole.Server.csproj SnakeConsole/Server/
COPY SnakeConsole/Client/SnakeConsole.Client.csproj SnakeConsole/Client/
COPY SnakeConsole/Shared/SnakeConsole.Shared.csproj SnakeConsole/Shared/
RUN dotnet restore "SnakeConsole/Server/SnakeConsole.Server.csproj"
COPY . .
WORKDIR "/src/SnakeConsole/Server"
RUN dotnet build "SnakeConsole.Server.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "SnakeConsole.Server.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SnakeConsole.Server.dll"]
Have faced similar issue, documentation says:
Deploying a standalone Blazor WebAssembly app to Azure App Service for Linux isn't currently supported. A Linux server image to host the app isn't available at this time. Work is in progress to enable this scenario.
https://learn.microsoft.com/en-us/aspnet/core/blazor/host-and-deploy/webassembly?view=aspnetcore-5.0#standalone-deployment
Looks like this is a Docker OS compatibility and framework functionality issue. I would suggest to target your app to Server (Blazor Server Hosting model scenario).

Heroku Push rejected, failed to compile ASP.NET Core app

I exactly followed this link http://codersblock.com/blog/how-to-run-net-on-heroku/
to deploy the application on heroku but I am getting an error
Push rejected, failed to compile ASP.NET Core app.
when I execute git push heroku master.
If any body knows about the issue, it will be great help.
Thanks.
what is heroku-16?
there are different types of distros(stacks) which contains your app:
heroku-16
cedar-14 (supports dotNet-core.)
thats why we will switch to cedar-14
how to fix:
1- install heroku cli. (https://devcenter.heroku.com/articles/heroku-cli)
2- open cmd. type:
cd to/project/Root
heroku stack:set cedar-14
git push heroku master
You could create a Dockerfile to generate a Docker image runnable on Heroku.
FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app/out .
CMD ASPNETCORE_URLS=http://*:$PORT dotnet <YOUR_API_NAME>.dll
Then you can publish it to Heroku either directly using these Docker commands:
docker build -t aspnetapp <YOUR_API_NAME>
docker login --username=$HEROKU_USERNAME --password=$HEROKU_API_KEY registry.heroku.com
docker tag aspnetapp registry.heroku.com/$HEROKU_APP_NAME/web
docker push registry.heroku.com/$HEROKU_APP_NAME/web
In order these commands:
Create a Docker image and builds a release of your project in it
Connect to Heroku's docker registry using your credentials
Creates a new tag for your image
Publishes your image to your Heroku app
However, if your machine does not support Docker, you can also use CircleCI to run these commands. More details here:
https://www.codingnagger.com/2018/02/21/continuous-delivery/
I've a similar problem and spent almost the whole day debugging it.
My issue, I was using Microsoft.Net.Compilers and I'd to remove that
<PackageReference Include="Microsoft.Net.Compilers" Version="4.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
and add
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
To .csproj (My case I was using clean architecture so I was doing these modifications in Domain layer
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
</PropertyGroup>
Note, I also have upgraded some of the packages it was complaining about been downgraded.

How to publish Visual Studio ASP.NET 5 projects to Docker Hub?

I'm looking for a way to publish my website to DaoCloud, a Docker container service provider.
The provider doesn't open their API for you. They just let you build your APP from source on GitHub or pulling image from Docker Hub. If you're building from GitHub, you can specify the Dockerfile which should be used for building.
But as you can see the Dockerfile in a ASP.NET 5 project
It doesn't specify any ports. Neither does projects.json/commands/web. I have tried to add ports specification manually and then build it, but with no success. All contents in the wwwroot folder return 404 error.
How can I create a buildable repository or create a Docker Hub image by Visual Studio?
Your Dockerfile seems to be right. Here is the one I used to build:
FROM microsoft/aspnet
COPY . /app
WORKDIR /app
RUN ["dnu", "restore"]
EXPOSE 5004
ENTRYPOINT ["dnx", "-p", "project.json", "web"]
Maybe you could share your project.json. Here is the command into the project.json that matches the entrypoint declared in the dockerfile.
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel --server.urls http://*:5004"
},
The (*) allows kestrel for listening requests from all domains.
You can find some samples here: AspNet Home Samples
Hope this helps !

Resources