How to create run .net Core Console App in docker
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A .NET console application is one of the easiest workloads to containerize because it has no web server, no browser-facing assets, and a simple entry point. The usual Docker workflow is to create the project, publish it into a clean runtime image, and then run the resulting container exactly the same way on any machine with Docker.
Create the Console Project
Start by generating a new console application.
Replace the default program with something easy to verify.
Run it locally first so you know the application itself works before adding Docker to the picture.
Use a Multi-Stage Dockerfile
A multi-stage build keeps the final image small by using the SDK image for build time and a lighter runtime image for execution.
The important detail is the published DLL name in ENTRYPOINT. It must match the actual project output.
Build and Run the Image
Build the container image from the project directory.
Then run it:
If everything is wired correctly, the container prints the console output and exits.
Pass Arguments and Environment Variables
Console apps often need input from arguments or environment variables. Docker supports both.
Update the app slightly:
Run with an argument:
Or with an environment variable:
This is useful for making one image configurable across environments.
Keep the Container Build Predictable
A few habits make Dockerized .NET work smoother:
- build from a clean project directory
- use a
.dockerignorefile to avoid copyingbinandobj - prefer
dotnet publishoverdotnet buildin the final image flow - keep the runtime image as small as possible
A basic .dockerignore helps:
That reduces build context size and avoids stale artifacts leaking into the container build.
Understand Why Publishing Matters
dotnet publish prepares the app for deployment by collecting the compiled output and required runtime assets into one output directory. That makes it the right step for container images.
If you copy source files into a runtime-only image without publishing first, the container will not have what it needs to execute correctly. Publishing is what turns the project into a deployment artifact.
Common Pitfalls
- Using the wrong DLL name in
ENTRYPOINT. - Building directly into the final image instead of using a multi-stage Dockerfile.
- Forgetting
.dockerignoreand copying unnecessary build artifacts. - Debugging Docker before confirming that
dotnet runworks locally. - Using SDK images in production when a smaller runtime image would be enough.
Summary
- Create and verify the .NET console app locally first.
- Use a multi-stage Dockerfile with an SDK build stage and a runtime final stage.
- Build the image with
docker buildand run it withdocker run. - Pass configuration through arguments or environment variables as needed.
- Prefer
dotnet publishand a small runtime image for cleaner container builds.

