Chapter 1. .NET Core 2.0 on Red Hat Enterprise Linux
This Getting Started Guide describes how to install .NET Core 2.0 on Red Hat Enterprise Linux (RHEL). See Red Hat Enterprise Linux documentation for more information about Red Hat Enterprise Linux 7.
1.1. Install and Register Red Hat Enterprise Linux
Install RHEL 7 using one of the following images:
Register the system by following the appropriate steps in Registering and Unregistering a System in the Red Hat Subscription Management document.
You can also use the following command to register the system.
$ sudo subscription-manager register
Display a list of all subscriptions that are available for your system and identify the pool ID for the subscription.
$ sudo subscription-manager list --available
This command displays the subscription name, unique identifier, expiration date, and other details related to it. The pool ID is listed on a line beginning with Pool ID.
Attach the subscription that provides access to the
dotNET on RHELrepository. Use the pool ID you identified in the previous step.
$ sudo subscription-manager attach --pool=<appropriate pool ID from the subscription>
Enable the .NET Core channel for Red Hat Enterprise 7 Server, Red Hat Enterprise 7 Workstation, or HPC Compute Node with one of the following commands, respectively.
$ sudo subscription-manager repos --enable=rhel-7-server-dotnet-rpms $ sudo subscription-manager repos --enable=rhel-7-workstation-dotnet-rpms $ sudo subscription-manager repos --enable=rhel-7-hpc-node-dotnet-rpms
Verify the list of subscriptions attached to your system.
$ sudo subscription-manager list --consumed
Install the scl tool.
$ sudo yum install scl-utils
1.2. Install .NET Core
Install .NET Core 2.0 and all of its dependencies.
$ sudo yum install rh-dotnet20 -y
rh-dotnet20Software Collection environment so you can run
dotnetcommands in the bash shell.Note
This procedure installs the .NET Core 2.0 SDK. A .NET Core 2.1 SDK is also available, which you can install via
sudo yum install rh-dotnet20-dotnet-sdk-2.1. It can be installed and used side-by-side with the 2.0 SDK. By default, the 2.1 SDK takes precedence if you install both. You can use a global.json to explicitly select one or the other.
$ scl enable rh-dotnet20 bashNote
This command does not persist; it creates a new shell, and the
dotnetcommand is only available within that shell. If you log out, use another shell, or open up a new terminal, the
dotnetcommand is no longer enabled.Warning
Red Hat does not recommend permanently enabling
rh-dotnet20because it may affect other programs. For example,
rh-dotnet20includes a version of
libcurlthat differs from the base RHEL version. This may lead to issues in programs that do not expect a different verison of
libcurl. If you want to permanently enable
rh-dotnetfor yourself, add the following line to your
source scl_source enable rh-dotnet20
Run the following command to prove the installation succeeded.
$ dotnet --help .NET Command Line Tools (2.0.0) Usage: dotnet [runtime-options] [path-to-application] Usage: dotnet [sdk-options] [command] [arguments] [command-options] path-to-application: The path to an application .dll file to execute. SDK commands: new Initialize .NET projects. restore Restore dependencies specified in the .NET project. run Compiles and immediately executes a .NET project. build Builds a .NET project. publish Publishes a .NET project for deployment (including the runtime). ....
1.3. Create an Application
If you want to run the classic "Hello World" test case, create the following directory.
$ mkdir hello-world
Navigate to the
$ cd hello-world
Create a project.
$ dotnet new console The template "Console Application" was created successfully. Processing post-creation actions... Running 'dotnet restore' on /home/<USER>/hello-world/hello-world.csproj... Restore succeeded.
Pull the dependencies needed for the project.
$ dotnet build
Run the project.
$ dotnet run Hello World!
1.4. Publish Applications
The .NET Core 2.0 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 RHEL, 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. These native libraries are part of the
dotnet Software Collection. On the other hand, SCD uses a runtime built by Microsoft and uses the global Red Hat Enterprise Linux libraries. There are known issues when using .NET Core with these libraries.
1.4.1. Publish .NET Core Applications
Use the following command to publish an application using the FDD.
$ dotnet publish -f netcoreapp2.0 -c Release
If the application will only be used on RHEL, you can trim the dependencies needed for other platforms by using these commands.
$ dotnet restore -r rhel.7-x64 $ dotnet publish -f netcoreapp2.0 -c Release -r rhel.7-x64 --self-contained false
Enable the Software Collection and pass the application assembly name to the
dotnetcommand to run the application on a RHEL system.
$ scl enable rh-dotnet20 -- dotnet <app>.dll
This command can be added to a script that is published with the application. Add the following script to your project and update the ASSEMBLY variable.
#!/bin/bash ASSEMBLY=<app>.dll SCL=rh-dotnet20 DIR="$(dirname "$(readlink -f "$0")")" scl enable $SCL -- dotnet "$DIR/$ASSEMBLY" "$@"
To include the script when publishing, add this ItemGroup to the
<ItemGroup> <None Update="<scriptname>" Condition="'$(RuntimeIdentifier)' == 'rhel.7-x64' and '$(SelfContained)' == 'false'" CopyToPublishDirectory="PreserveNewest" /> </ItemGroup>
1.4.2. Publish ASP.NET Core Applications
By default, ASP.NET Core 2.0 web applications are published with a dependency on a runtime store. This is a set of packages that are expected to be available on the runtime system. They are not included with the published application. RHEL does not include the ASP.NET runtime store, so the applications must be published, including all dependencies. This can be done by setting the PublishWithAspNetCoreTargetManifest property to false in the project file.
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> <PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" /> </ItemGroup> </Project>
Alternatively, this property can be set when publishing the application.
$ dotnet publish -f netcoreapp2.0 -c Release -r rhel.7-x64 --self-contained false /p:PublishWithAspNetCoreTargetManifest=false
1.5. Run Applications on Docker
This section shows how to use the
dotnet/dotnet-20-runtime-rhel7 image to run your application inside a Docker container. It requires you to have the docker binary installed, the docker daemon running, and the
rh-dotnet20 Software Collection enabled.
Create and navigate to the following directory.
$ mkdir mvc_runtime_example && cd mvc_runtime_example
Create a new project.
$ dotnet new mvc --no-restore
Pull all dependencies.
$ dotnet restore -r rhel.7-x64
Publish the project.
$ dotnet publish -f netcoreapp2.0 -c Release -r rhel.7-x64 \ --self-contained false /p:PublishWithAspNetCoreTargetManifest=false
Create a file called
Dockerfileand add the following contents.
$ cat > Dockerfile <<EOF FROM registry.access.redhat.com/dotnet/dotnet-20-runtime-rhel7 ADD bin/Release/netcoreapp2.0/rhel.7-x64/publish/ . CMD ["dotnet", "mvc_runtime_example.dll"] EOF
Build your image.
$ sudo docker build -t dotnet-20-runtime-example .
Run your image.
$ sudo docker run -d -p8080:8080 dotnet-20-runtime-example
- View the result in a browser: http://127.0.0.1:8080