How to Run a C# CLI App with Docker
Date: 2025-06-11 | build | csharp | docker | tech |
Docker is a useful container technology for running code / workloads anywhere. You can configure your container with infrastructure as code so that it builds and runs the same thing on any computer with Docker installed.
In this post we'll discuss running a C# CLI app with Docker.
Creating a C# Console App
To create a console app, we can use the dotnet new console
command.
(This assumes you have the dotnet
sdk installed. If not, you can grab it from the Microsoft website or just copy and paste the code in this post).
This spits out a simple Program.cs
:
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
And an App.csproj
:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Typically you would run this with the dotnet run
command and it will output Hello, World!
.
Running with Docker
To run with Docker, we'll create a Dockerfile which specifies how to build and run the container.
- Start with a dotnet 9 sdk base image
- Copy the
.csproj
into the container and rundotnet restore
to clean / dl packages - Publish the app
- Create a new layer with aspnet and run the app
Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:9.0-bookworm-slim AS build
WORKDIR /source
# copy csproj and restore as distinct layers
COPY ./*.csproj ./
RUN dotnet restore
# copy and publish app and libraries
COPY . .
RUN dotnet publish -c release -o /app
# Copy build / styles into final location
FROM mcr.microsoft.com/dotnet/aspnet:9.0
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "App.dll"]
To run this, we'll use docker run $(docker build -q .)
which builds the image in the current directory with no tags and then immediately runs it.
If you're curious, this looks very similar to how you'd run F# in a Docker container - this makes sense as they both run on dotnet so require similar things.
Next
The full source code is available in this post but if you want access to the project files themselves, you can get it in the HAMY LABS Example Repo on GitHub, available to all HAMINIONs Members.
If you liked this post you might also like:
Want more like this?
The best way to support my work is to like / comment / share for the algorithm and subscribe for future updates.