Building Docker Image for ASP.Net Application

This article will describe building and deploying an ASP.Net sample web application for Docker. We’ll be using Docker Desktop for Windows and Visual Studio 2022 Developer Edition.

To learn how to install Docker Desktop on Windows 10 please refer to my article

ASP.Net web application can only be deployed on Windows Container. However, .Net Core application can be deployed either at Linux or at Windows container. To learn more about deploying .Net Core application refer to my article

Let’s follow the steps to create the project:

  • Open Visual Studio 2022 and create a new project
  • Let’s name it AspWebApp4Docker
  • Check Docker Support on the next screen. As this is a ASP.Net application so only Windows Container is supported.
  • Click Create and there will be pulling of Docker Image for Windows Server Core will be started on a separate command prompt.
  • The output window of Visual Studio will be shown something like this
  • Once done this will try to run the project from Docker Image. But it didn’t work for me !
  • Let’s remove the image from Docker.
  • Find the image at Power Shell
    • docker ps
  • It’ll be something like aspwebapp4docker:dev. Stop it and then remove
    • docker stop aspwebapp4docker:dev
    • docker rm aspwebapp4docker:dev
  • Now go to the Docker File at the project directory. It’ll be look like:
  • Let’s comment out this code using #infront of each line. Yes, that’s how we comment a docker file.
  • Now We’ll publish the application before put it into docker.
  • From Visual Studio Solution Explorer Select Publish.
  • We’ll publish it locally. Select folder to publish on next screen and put the folder name on subsequent screen.
  • Finally do Publish
  • This publish folder will be available at your project root directory bin folder.
  • Now we’ll tell Docker to use this location to build the image
  • Open the Docker File and put the following:


COPY ./bin/app.publish/ /inetpub/wwwroot

  • Here we are instructing to use the windows server core image and copying the published application to /inetpub/wwwroot
  • Now we’ll build the project form Windows Power Shell. Please note that as we are already done with pulling the windows server from Microsoft Container Registry while creating the project so this time it will use the cache to proceed.
  • At Power Shell change location to project root directory. Now build the project
    • docker build -t aspwebapp4docker .
  • The image is named to aspweb4docker. We can also do a tagging using the name:tag format. As we are not assignign any tag so the default tag is set to latest.
  • Now run the image
    • docker run -d -p 4000:80 --name aspwebapp4docker1.0 aspwebapp4docker
  • Once done it’ll show the id of the image.
  • This can also be viewed from Power Shell
    • docker ps
  • We can also view from Docker desktop


Building Your First .Net Core Docker Image

This article will describe building and deploying a .Net Core sample application for Docker. We’ll be using Docker Desktop for Windows and Visual Studio 2022 Developer Edition.

To learn how to install Docker Desktop on Windows 10 please refer to my article

Deploying on Linux Container

Let’s follow the steps to create the project:

  • Open Visual Studio 2022 and create a new project
  • Let’s name it CoreWebApp4Docker
  • Enable Docker on the next screen and select Linux as container type
  • Now make some changes at the index.cshtml file to display some customize text after we’ve deployed. Change at Line No 9 to Learn about building Web apps with ASP.NET Core and deploying at Docker.
  • Let’s have a look at the Docker file which is created automatically by Visual Studio.Net
  • The Docker file contains build instruction to deploy the image at Docker. This is known as Docker Multistage build feature. The multistage build allows container images to be created in stages that produce intermediate images. 
  • At the top the Docker Multistage build file following section is telling Docker to create ASP.Net image from Microsoft Container Registry. It’s creating an intermediate image named base that exposes Port 80 and set working directory to /app
FROM AS base
  • The next stage is Build which starts with a different original image from registry which is sdk. The sdk image has the build tools and it’s quite bigger than the aspnet image. The aspnet image only contains the runtime.

FROM AS build
COPY ["CoreWebApp4Docker.csproj", "."]
RUN dotnet restore "./CoreWebApp4Docker.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "CoreWebApp4Docker.csproj" -c Release -o /app/build

  • The next step is publishing the image from build

FROM build AS publish
RUN dotnet publish "CoreWebApp4Docker.csproj" -c Release -o /app/publish /p:UseAppHost=false

  • The final stage is using the base which have only the runtime and copy the recently published folder to the final image. As the final image is only containing the runtime so it’ll be quite smaller that the build image.

FROM base AS final
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "CoreWebApp4Docker.dll"]

  • So we are done with the Dockerfile. Visual Studio 2022 have built in feature to build the application as Docker image.
  • However we won’t do this. We’ll use WSL command line to build the application.
  • At first run your Docker Desktop and ensure that you are running Linux Container. If you find the option to “Switch to Windows Containers…”, this means you are on Linux Container.
  • Open Ubuntu Command Shell and see whether Docker is running:
    • docker
  • If you find docker help is showing that means Docker is running as Linux container
  • Build the image using the Docker file created by Visual Studio
  • Browse to the directory where Docker File exists
  • For me it is
    • cd /mnt/c/Users/sultan/source/repos/CoreWebApp4Docker
  • Note that at WSL your Windows drive will be mounted under /mnt/
  • Now build using docker build
    • docker build -t corewebapp4docker .
  • We named the build as corewebapp4docker and the . at the end indicates the current directory where Docker Files exists
  • The name can be given as name:tag format otherwise it’ll be tagged as corewebapp4docker:latest
  • The output will be like this:
  • Now we’ll run the image
    • docker run -d -p 5000:80 --name corewebapp4docker1.0 corewebapp4docker
  • The -d is telling to run the image at background and show the container Id only. -p is saying to map host port 5000 to Docker port 80. --name is naming the image and the last one is the image name that we’ve used during build.
  • Once done you can list the running image using
    • docker ps
  • We can also view the image at Docker Desktop
  • That’s all we are done with deploying our .Net core application to Docker.

Deploying on Windows Container

  • Now we’ll deploy the same project to Windows Container.
  • At first we need to stop and remove the existing image
    • docker stop corewebapp4docker1.0
    • docker rm corewebapp4docker1.0
  • We can run docker ps to be sure that we are done
  • Now let’s change the container to Windows Container from Docker Desktop
  • Now we will use Windows Power Shell to deploy and run
  • Power Shell must be opened with Administrator privilage
  • We’ll follow the same steps we’ve used for Linux containers
    • docker build -t corewebapp4docker .
    • docker run -d -p 5000:80 --name corewebapp4docker1.0 corewebapp4docker
  • And we are done. We can list the image using docker ps. Or we can see the Docker Desktop Window.
  • The application will be browsable from http://localhost:5000
  • There might be issues with accessing to download nugget package. In that case at Docker –> Setting –> Docker Engine add the following json tag with the existing:
    • "dns" : [""]
  • The setting will be look like
  • Now this image is locally available. To share the image we have to publish the image to Docker Hub.

Publishing to Docker Hub

  • To register the image into the Docker Hub we need to have an account over there.
  • Go to and create an account
  • Now create a repository over there where we can publish the app.
  • Now come on to the Power Shell again and login to Docker Hub with the user name just we’ve created
    • docker login -u sultanalam
  • We need to tag the currently build image with the username/repository name we’ve just created
    • docker tag corewebapp4docker sultanalam/corewebapp4docker1.0
  • Now pushing the image to the hub using the new tag
    • docker push sultanalam/corewebapp4docker1.0
  • The image will be available under the repository we’ve created


Configuring Docker on Windows 10

This article will describe the ways to enable a Windows 10 Machine to support docker.

You can run docker on Windows 10 using either of the following ways:

  • Linux Container
  • Windows Container

Enabling Linux Container:

  • The OS should be either Windows 10 64-bit Home or Pro 21H1 (build 19043) or higher. The version can be checked from PowerShell
    • winver
  • 64-bit processor
  • 4GB system RAM
  • BIOS-level hardware virtualization support must be enabled in the BIOS settings
  • The WSL2 (Windows Subsystem for Linux) feature must be enabled.
  • Open PowerShell in Administrator mode and run
    • wsl --install
  • You’ll be prompted for confirmation to install. Click yes to proceed.
  • Once done the following installation screen will be shown:
  • You need to reboot the system to effect the changes.
  • After reboot you can find Ubuntu (this is the default distribution) at you program menu.
  • Select Ubuntu to finish installation.
  • You will be prompted for setting your UNIX user & password (which is not necessarily your windows credential but can be anything you prefer)
  • The default UNIX/ Ubuntu user will be the username you set here.
  • If you select Ubuntu from program menu once again you will get the Ubuntu terminal
  • If you forget this default user password you can login using root and reset the passowrd.
  • To change the default user to root run windows command prompt as Administrator
    • ubuntu config --default-user root
  • Run Ubuntu from program menu once again and you’ll find the default user is set to root.
  • To change password
    • passwd username
  • Type the new password and reconfirm
  • Once done you can change the default user again
  • From PowerShell you can check the default distribution installed and can list down other distribution (if any) as well
    • wsl --list

Installing Docker:

Docker for Windows can be downloaded from:

  • Run the installer and Select “Use WSL2 instead of Hyper-V” option. However, if your Windows does not support Hyper-V (like you have Windows 10 Home Edition) you won’t get this option.
  • You need to logout from Windows to finish the installation
  • Once done you’ll find Docker Desktop is running
  • From PowerShell or Ubuntu you run
    • docker
  • Docker container on Windows can run on both as Linux Container and Windows Container
  • Select Docker Desktop from task bar and you can switch between Linux and Windows Container
  • As currently we have configured Docker for Linux Container (which is by default) so switching to Windows Container option are shown here

However switching from the taskbar won’t necessarily switch the container to Windows Container. Few more tasks are needed to be done as stated at the following section.

Enabling Windows Container:

  • Windows Container can be installed only if you have Windows 10 Pro edition
  • From start menu type “Turn Windows Feature on or off” and select Container & Hyper-V option
  • Once installation is done you’ll need to restart the PC