Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Containerize .NET applications without writing Dockerfiles

August 1, 2022
Tom Deseyn
Related topics:
.NETCI/CDContainersLinux
Related products:
Red Hat Enterprise Linux

    This article introduces dotnet build-image, a tool that containerizes .NET applications automatically. You can use build-image to create Dockerfiles and containerized images. You will also discover how to use the tool in a GitHub workflow to create an image from a .NET application and push it to a repository.

    Building an image or Dockerfile with dotnet build-image

    To run a .NET application in a container, you need a Dockerfile. At first, learning about Dockerfiles and writing them can be fun. But after a while, it becomes repetitive. Moreover, the files need to be kept in sync with the application. And if you have several applications, you need to keep updating their Dockerfiles to use the same style.

    The dotnet build-image is a global tool that writes the Dockerfile for you. The tool's features include:

    • It uses a multi-stage build: First, the application is published into one image, then the result is copied into a smaller runtime image.
    • The tool determines the .NET version an image should use from the .NET project (TargetFramework).
    • You can choose the image base's operating system, such as Ubuntu or Alpine.
    • It supports both Podman and Docker to build the image.
    • It caches NuGet packages across builds.
    • It uses the SDK version from global.json for publishing the application if the file is present.

    To get started, install the tool from NuGet:

    $ dotnet tool install -g dotnet-build-image

    Next, build an image from the default web template application:

    $ dotnet new web -o web
    $ cd web
    $ dotnet build-image -t mywebapp

    You will see the build in the terminal. Once the build is finished, run the image:

    $ podman run -p 8080:8080 mywebapp

    Open the web application in your browser at: http://localhost:8080.

    The tool has several options for you to explore by passing the argument: --help ​​​​.

    $ dotnet build-image --help
    Description:
      Build a container image from a .NET project.
    
    Usage:
      build-image [<PROJECT>] [options]
    
    Arguments:
      <PROJECT>  .NET project to build [default: .]
    
    Options:
      -b, --base <base>            	Flavor of the base image
      -t, --tag <tag>              	Name for the built image [default: dotnet-app]
      --push                       	After the build, push the image to the repository
      --as-dockerfile <as-dockerfile>  Generates a Dockerfile with the specified name
      --version                    	Show version information
      -?, -h, --help               	Show help and usage information

    We have already used the tag (-t) option.

    The --base option allows you to specify the base image. You can specify any base image that Microsoft publishes on the Microsoft Artifact Registry. When you specify ubi as the base, your application will use the .NET images from the Red Hat Ecosystem Catalog. Red Hat's UBI images are enterprise-grade images that can be freely distributed and deployed anywhere. To learn more, take a look at the UBI FAQ or read the e-book Red Hat Universal Base Images (UBI).

    As an alternative to specifying the options on the command line, add them to the csproj file:

      <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <ImageTag>mywebapp</ImageTag>
        <ImageBase>ubi</ImageBase>
      </PropertyGroup>

    If you prefer to have a Dockerfile under version control, the tool can still be useful. Specify the --as-dockerfile Dockerfile argument to write out a Dockerfile instead of building an image. You can inspect the file and customize it.

    Using dotnet build-image in a GitHub workflow

    This section creates a GitHub repository and uses dotnet build-image in a GitHub workflow to set up continuous delivery (CI/CD) of a .NET application as a container image.

    As my image registry, I use my free account at Quay.io. Other registries such as Docker Hub work too.

    First, create a new repository and populate it with a .NET web app. I've named mine build-image-demo:

    $ git clone git@github.com:tmds/build-image-demo.git
    $ dotnet new web -o web --no-restore
    $ git add web
    $ git commit -m "Add web project"
    $ git push

    Add a global.json file that captures the SDK version you'd like contributors and CI to use:

    $ dotnet new globaljson --sdk-version 6.0.100 --roll-forward latestFeature
    $ git add global.json
    $ git commit -m "Use 6.0 SDK"
    $ git push

    Note that we've used the latestFeature argument. That permits the use of any 6.0.xxx SDK. Without that argument, the SDK is limited to the 6.0.1xx feature band.

    To use the Red Hat .NET images, add the ImageBase property in the web.csproj file. Add the line marked by a plus sign (+) in the PropertyGroup section:

       <PropertyGroup>
     	<TargetFramework>net6.0</TargetFramework>
     	<Nullable>enable</Nullable>
     	<ImplicitUsings>enable</ImplicitUsings>
    +	<ImageBase>ubi</ImageBase>
       </PropertyGroup>

    Include this change in your next commit:

    $ git add web/web.csproj

    Now, add the credentials for accessing the registry server to your GitHub project. Click the project's Settings link in the GitHub web interface and open the Environments tab. Then click New Environment and name it ImageRegistry. Add two secrets named REGISTRY_USER and REGISTRY_PASSWORD. These contain the credentials used to access the registry (Figure 1). After you add these values, verify that they work by using them with the podman login or docker login command.

    Add two secrets, REGISTRY_USER and REGISTRY_PASSWORD, in the GitHub project.
    Figure 1. Add two secrets, REGISTRY_USER and REGISTRY_PASSWORD, in the GitHub project.

     

    Now you're ready to add the workflow. Create a file at:  .github/workflows/deploy-image.yml and add the following content.

    name: Deploy Image
    
    on:
      push:
    	branches: [ "main" ]
    
    env:
      DOTNET_PROJECT: web
      REGISTRY: quay.io
      IMAGE_NAME: tmds/web
    
    jobs:
      deploy:
    
    	environment: ImageRegistry
    	runs-on: ubuntu-latest
    
    	steps:
    	- uses: actions/checkout@v3
    	- name: Install .NET
      	uses: actions/setup-dotnet@v2
    	- name: Install dotnet-build-image
      	run: dotnet tool install -g dotnet-build-image
    	- name: Log in to Image registry
      	run: echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login $REGISTRY -u "${{ secrets.REGISTRY_USER }}" --password-stdin
    	- name: Build and push image
      	run: dotnet build-image --push "$DOTNET_PROJECT" -t "$REGISTRY/$IMAGE_NAME"

    Update the REGISTRY and IMAGE_NAME properties under the env field to the right values for your image registry. If you're using Docker Hub, use docker.io as the REGISTRY.

    Let's take a look at the steps in the workflow:

    1. The first step (checkout) checks the source code.
    2. The next step (setup-dotnet) installs the .NET SDK using the version from global.json.
    3. A command runs which logs into the image registry.
    4. The last step runs the dotnet build-image tool to build the image and push it to the registry.

    When you've finished updating the file, push it to the repository:

    $ git add .github/workflows/deploy-image.yml
    $ git commit -m "Add deploy-image workflow."
    $ git push

    GitHub will trigger the workflow. You can follow progress under the Actions tab in the GitHub web interface.

    When you make changes to the web application and commit them to the main branch, it triggers the workflow that creates a new image and pushes it to the repository.

    $ podman run –rm -p 8080:8080 quay.io/tmds/web

    If you open the website at http://localhost:8080, the .NET application responds.

    Now when you make a change to the web application and commit it to the main branch, it triggers the workflow again and pushes a new image to the repository.

    Automating the Dockerfile creation

    In this article, you have learned how to use dotnet build-image to create a container image from a .NET application without writing a Dockerfile, and how to integrate build-image into a GitHub CI/CD workflow. To share feedback about this tool, please create a ticket in the GitHub repository.

    Last updated: August 14, 2023

    Related Posts

    • Dockerfiles now available for Red Hat Software Collections

    • Building .NET Core container images using S2I

    • .NET 5.0 now available for Red Hat Enterprise Linux and Red Hat OpenShift

    Recent Posts

    • Protect data offloaded to GPU-accelerated environments with OpenShift sandboxed containers

    • Case study: Measuring energy efficiency on the x64 platform

    • How to prevent AI inference stack silent failures

    • Preventing GPU waste: A guide to JIT checkpointing with Kubeflow Trainer on OpenShift AI

    • How to manage TLS certificates used by OpenShift GitOps operator

    What’s up next?

    Getting GitOps e-book card

    Learn how to navigate the complex world of modern container-based software development and distribution with Getting GitOps: A Practical Platform with OpenShift, Argo CD, and Tekton.

    Get the e-book
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.