Topic navigation

Blog Articles

Building .NET Core container images using S2I

Red Hat OpenShift implements .NET Core support via a source-to-image (S2I) builder. In this article, we’ll take a closer look at how you can use that builder directly. Using S2I, you can build .NET Core application images without having to write custom build scripts or Dockerfiles. This can be useful on your development machine or as part of a CI/CD pipeline.

Containers for your builds

Container images provide an efficient mechanism to deploy self-contained applications in a portable way across clouds and OS distributions.

By building the application images themselves using a builder image, the application images can be built in a portable, reproducible way.

S2I is a toolkit for building reproducible application images. S2I uses builder images to produce application images from source code or pre-compiled applications.

To use the RHEL 7 images, you need a Red Hat subscription. For development, you can use the free development subscription. Use the docker login registry.redhat.io command to configure your credentials.

When we run this command, we’ll see s2i check out the sources and build the .NET Core application image. When the command finishes, we can run the newly created image:

$ docker run --rm -p 8080:8080 mywebapp

Let’s look at the parameters we’ve passed to the s2i command:

Parameter

Description

registry.redhat.io/dotnet-21-rhel7:2.1

S2I builder image

https://…/s2i-dotnetcore-ex

The git repository

-r dotnetcore-2.1

Branch in the repository

-e DOTNET_STARTUP_PROJECT=app

Folder in the repository that contains the application csproj file

-p always

Always pull the latest builder image

The -e DOTNET_STARTUP_PROJECT is an environment variable passed to the S2I builder. The builder supports a number of environment variables that allow you to customize its behavior. See the environment variables documentation for a complete list.

Building an application image from local sources

The source parameter for the s2i command can also refer to a local folder that contains the source code. In the following example, we clone the repository locally and check out the appropriate branch into the s2i-dotnetcore-ex folder. Then we build from that local folder.

We are specifying the MicrosoftNETPlatformLibrary parameter to make the published application contain the ASP.NET Core shared framework assemblies. This is needed because the images don’t contain the shared framework.

To create the image, we pass the publish folder as the source parameter of the s2i build command. We can use the SDK builder images like before, but since the application is already built, we can use the smaller runtime image instead.