Chapter 1. Using .NET Core 3.1 on Red Hat Enterprise Linux

1.1. Installing .NET Core

This procedure installs the .NET Core 3.1 runtime with the latest 3.1 SDK. When a newer SDK becomes available, it automatically installs as a package update.

Procedure

.NET Core 3.1 is included in the AppStream repositories for RHEL 8. The AppStream repositories are enabled by default on RHEL 8 systems.

  1. Install .NET Core 3.1 and all of its dependencies:

    $ sudo yum install dotnet-sdk-3.1 -y
  2. Run the following command to verify the installation:

    $ dotnet --info

1.2. Creating an application

Procedure

  1. Create a new Console application in a directory called hello-world:

    $ dotnet new console -o hello-world
      The template "Console Application" was created successfully.
    
      Processing post-creation actions...
      Running 'dotnet restore' on hello-world/hello-world.csproj...
      Restore completed in 87.21 ms for /home/<USER>/hello-world/hello-world.csproj.
    
      Restore succeeded.
  2. Run the project:

    $ cd hello-world
    $ dotnet run
    Hello World!

1.3. Publishing applications

.NET Core 3.1 applications can be published to use a shared system-wide version of .NET Core or to include .NET Core. These two deployment types are called framework-dependent deployment (FDD) and self-contained deployment (SCD), respectively.

For Red Hat Enterprise Linux, we recommend publishing by FDD. This method ensures the application is using an up-to-date version of .NET Core, built by Red Hat, that includes a specific set of native dependencies. On the other hand, SCD uses a runtime built by Microsoft.

Procedure

  1. Use the following command to publish a framework-dependent application:

    $ dotnet publish -f netcoreapp3.1 -c Release
  2. Optional: If the application is only for RHEL, trim out the dependencies needed for other platforms with these commands:

    $ dotnet restore -r rhel.8-x64
    $ dotnet publish -f netcoreapp3.1 -c Release -r rhel.8-x64 --self-contained false

1.4. Running applications in containers

This section shows how to use the ubi8/dotnet-31-runtime image to run a precompiled application inside a container.

Procedure

  1. Create a new MVC project in a directory named mvc_runtime_example:

    $ dotnet new mvc -o mvc_runtime_example
    $ cd mvc_runtime_example
  2. Publish the project:

    $ dotnet publish -f netcoreapp3.1 -c Release
  3. Create the Dockerfile:

    $ cat > Dockerfile <<EOF
    FROM registry.access.redhat.com/ubi8/dotnet-31-runtime
    
    ADD bin/Release/netcoreapp3.1/publish/ .
    
    CMD ["dotnet", "mvc_runtime_example.dll"]
    EOF
  4. Build your image:

    $ podman build -t dotnet-31-runtime-example .
  5. Run your image.

    $ podman run -d -p8080:8080 dotnet-31-runtime-example
  6. View the result in a browser: http://127.0.0.1:8080.