Running .NET Console Docker image in Ubuntu failing - c#

Create a docker image for a .NET Console application. But when trying to run the image, its throwing error.
Following section contains the Dockerfile content,
#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/runtime:6.0
COPY bin/Release/net6.0/publish .
ENTRYPOINT ["dotnet", "MySampleApplication.dll"]`
Following is the error that I get while trying to run the docker image,
docker run collector
You must install or update .NET to run this application.
App: /MySampleApplication.dll
Architecture: x64
Framework: 'Microsoft.AspNetCore.App', version '6.0.0' (x64)
.NET location: /usr/share/dotnet/
No frameworks were found.
Learn about framework resolution:
https://aka.ms/dotnet/app-launch-failed
To install missing framework, download:
https://aka.ms/dotnet-core-applaunch?framework=Microsoft.AspNetCore.App&framework_version=6.0.0&arch=x64&rid=debian.11-x64`
Tried installing .NET sdk and runtime environment in Ubuntu, but still the same issue.

Your app might run from the console, but in MS terminology, it's not a console app. You get a message saying Framework: 'Microsoft.AspNetCore.App', version '6.0.0' (x64) and that tells us that it's an ASP.NET application. ASP.NET is not included in the console image, but it is included in the aspnet image.
So change your Dockerfile to
#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
COPY bin/Release/net6.0/publish .
ENTRYPOINT ["dotnet", "MySampleApplication.dll"]
When you run it, you'll need to map port 80 to a host port to be able to talk to the application. So your docker run command will look something like
docker run -d -p 8080:80 collector
You should then be able to reach the application on http://localhost:8080/ from the host.

Related

How to Compile a C# Project for Use on Linux?

I downloaded a C# project from github and compiled it with Visual Studio using the dotnet build command and apparently everything went well. But I can't use it on Centos, not even using Mono . Which command should I use to compile a C# project in VisualStudio for use on Linux? I used dotnet build which generated me a project with .exe file and I tried to run it with Mono and I got this error File does not contain a valid CIL image.
sorry if this is the wrong area to ask
There are different ways to do it and they depend on what you're trying to achieve.
From .NET application publishing overview
Type
Command
framework-dependent executable for the current platform.
dotnet publish
framework-dependent executable for a specific platform.
dotnet publish -r --self-contained false
framework-dependent cross-platform binary.
dotnet publish
self-contained executable.
dotnet publish -r
From the Examples section:
Publish an app cross-platform framework-dependent. A Linux 64-bit executable is created along with the dll file. This command doesn't work with .NET Core SDK 2.1.
dotnet publish -r linux-x64 --self-contained false
You may be interested in Single-file deployment and executable:
Bundling all application-dependent files into a single binary provides an application developer with the attractive option to deploy and distribute the application as a single file. Single-file deployment is available for both the framework-dependent deployment model and self-contained applications.

How to run Avalonia app on Ubuntu 22.04? Some libhostfxr.so: invalid ELF header error

I'm trying to run Avalonia App on Ubuntu 22.04 and get this error:
Failed to load /home/nikita/Downloads/Project/Pro/libhostfxr.so, error: /home/nikita/Downloads/Project/Pro/libhostfxr.so: invalid ELF header
The library libhostfxr.so was found, but loading it from /home/nikita/Downloads/Project/Pro/libhostfxr.so failed
- Installing .NET prerequisites might help resolve this problem.
https://go.microsoft.com/fwlink/?linkid=2063370
When I run same app on clean (without .NET dependencies installed) Ubuntu 20.04 - it's ok.
I build app from Windows with command:
dotnet publish ..\Project\ -o ..\Release\Ubuntu\Project\ --runtime ubuntu.22.04-x64 --configuration Release --self-contained true
I guess that it's can be some trouble of build cause if I install dotnet and build project directly from ubuntu 22.04 - it runs, but with some problems like black background :D
run native ubuntu build
Please help me understand where the problem can be :) Thanks!
Avalonia v0.10.18

Is it possible to publish a stand alone c# application to Linux?

I am trying to publish a C# .net core console app I made, to a Linux machine running Centos 7 64 bit. Currently, I am publishing the app using the command:dotnet publish -c release -r contos.7-x64. Unfortunately, it appears as though publishing in this way requires .net core to be installed on the target machine. Is it possible to do this where I do not need to install anything on the machine that I am publishing to?
In .NET Core 2.2 you can create a self-contained app (i.e. not requiring .NET Core runtime installed on host machine) with this command dotnet publish -r centos.7-x64 -c Release --self-contained. It'll produce executable and a lot of dependencies.
In .NET Core 3 you can compress all dependencies into a single file dotnet publish -r centos.7-x64 -c Release /p:PublishSingleFile=true. You can also add flag /p:PublishTrimmed=true to reduce executable size by tree trimming.
More details can be found here and here.
I found that the issue was that the project was not compiling to core 2. Rather, the project was compiling to version 1. After making this change, the published project was able to run on Centos.

.Net core application cannot run on Linux?

I created a .Net core C# console application in Visual Studio and used the following steps to test it on Linux.
Use Visual Studio "Build -> Publish" menu item to create the executable files in ....\bin\Release\netcoreapp2.1\publish.
Copy the "publish" directory to the Linux machine
On Linux, chmod 777 myApp.dll
./myApp.dll
However, executing the app shows the error of
-bash: ./myApp.dll: cannot execute binary file
It looks like you did a Framework-Dependendent Deployment. Essentially, the publish command was:
dotnet publish -c Release
FDD assumes that you are going to have a .NET Core runtime to run your application on the target platform.
Once you copied over the publish directory to another machine (which could be Linux, macOS or Windows), your application still needs a .NET Core runtime to run your application.
Installing the .NET Core runtime depends on the particular Linux distribution that you are using. Once you have it installed, you can run your application by doing:
dotnet /path/to/publish/myApp.dll
An alternative to Framework Dependent Deployment is Self-Contained Deployment. In this mode, the published application will contain your application as well as a copy of the .NET Core runtime. On command line, doing a a SCD publish looks like this:
dotnet publish -r linux-x64 -c Release
For doing this in Visual Studio, see the link above. Then, you should see a bin\Release\netcoreapp2.1\linux-x64\publish\ directory that contains a myApp file. You can copy over this publish dir over to a Linux distribution and just run:
/path/to/linux-x64/publish/myApp

Exclude docker directory in .net core application

I have problems with running .net core app on my Debian 9. All .net core versions below 2.0 preview returns
"Failed to initialize CoreCLR, HRESULT: 0x80131500" error during execute dotnet new, restore or e.g --version.
.NET core 2.0 preview works correctly but framework doesn't want to cooperate with JwtBearer - becasue Bearer has 1.1.2 version and dotnet returns
"Downgrade package detected bla bla...."
(Maybe you have idea how to build app with downgrade package?)
So I decided to move project to docker.
I downloaded docker with .net core 1.1. I created docker-compose file, where I add a volume mount to docker see my application.
Everything works correctly, but with one small exception.
When I execute dotnet restore, build and run on my docker, everyting is beeing override, so my Visual Studio Code immidietly shows me warrining "Unresolved dependencies ...." and all references to dotnet namespaces, class etc are highlighted in red.
How I can fix it?
Below is my docker-compose.yml
version: '2'
services:
webapi:
build: .
command: "/bin/bash"
container_name: webapi
volumes:
- /home/pb/docker-test/docker-compose:/app
- /home/pb/docker-test/docker-compose/Evento.Api/obj/
ports:
- "5000:80"
tty: true
/home/pb/docker-test/docker-compose/Evento.Api/obj/ I think that this directory makes problems becouse contains a few path which are changing after dotnet restore.
I tried to exclude that directory in this way, but it doesn't work. Event if direcotry is not mounted, dotenet creates it after dotnet restore, and override on host.
Dockerfile
FROM microsoft/aspnetcore-build
WORKDIR /app

Categories