Docker - DotNet 4.8 - Getting Started

Source Code: docker-try-net48

Goal: Develop a .Net48 application - coding would happen in the host while its run inside the docker - live

Prerequisite: Docker Desktop for windows is up and running and you are using Windows Containers

There are tons of documentation online, this is just me getting started.

Base Image:

There are two base images supported officially by Microsoft for .Net windows application.

  • Windows Server Core
  • Windows Nano Server

alt Components

In this example: we are gona work on a .Net Framework 4.8 - console application. We would be using the DotNet Framework 4.8 image which internally uses servercore:ltsc2019-amd64 as base image.

Let first pull the image, into our local image cache. This would take a while as the size is huge - get yourself a coffee.

$docker pull mcr.microsoft.com/dotnet/framework/runtime:4.8

Once pulled (image is available in your local cache) - you are gona see an entry with Tag 4.8 in image list

$docker image ls

alt Components

Sample App:

Here is a sample app to demostrate the usage. Clone the repository. Open the solution jacapp.sln in VS2019 and compile it. Its a simple app which starts two threads and exits after 10 seconds

  • One thread - print the count and the thread ID - to the console every second
  • Second thread - write the same count and thread ID - into an output text file every second

note: the output file would be on a volume mounted drive with the host

alt Components

Dockerfile:

Let’s look at the contents of the dockerfile:

  1. Specify Base image we plan to use - in our case we are gona use the image (4.8 Runtime) we have already downloaded (pulled)
  2. Specify the Working Directory where all the commands would be run. Think of it as running $CD /d c:/myapp, before the EntryPoint is exectuted
  3. Specify the Command to be executed inside the working directory. In our case its the console app, so we run the output exe jacapp.exe

alt Components

Build Image:

Let’s build a docker image. Run the command from the directory which has the Dockerfile

$docker build -t testapp .

alt Components

alt Components

Run Image:

Items to note:

  • –name: name of the container image instance
  • -v: volume mount; here we mount the output “bin” directory with a specific directory inside the container “c:\myapp" .
  • -rm: remove the container instance once the program exits
$docker run --name jacinstance1 --rm -v D:\dev\dkr\docker-try-net48\jacapp\bin\Debug:C:\myapp\ testapp

Instance of the containter running:

alt Components

Console output:

alt Components

Contents of the file writted on the mounted drive:

alt Components

Well thats it! We have our application running inside a container - Now we could edit your project (try updating the console output text), compile and directly docker run the image i.e. NO docker image re-build required!!

Jacob Aloysious
Jacob Aloysious
Software Enthusiast

35yr old coder, father and spouse - my interests include Software Architecture, CI/CD, TDD, Clean Code.

Related