In this tutorial, I will demonstrate how to host a ASP.NET Core 2.2 application on Windows Containers by using a Docker image. A Docker image will be packaged with a ASP.NET Core application that will be ran when a container in spun up.
Before we get started with creating Docker image. Let's make sure we have prerequisites done.
- Installing docker-cli and other components to get started
- Visual Studio code.
- Docker extension for visual studio code.
Once you have the prerequisites, we will use publicly available
ASP.NET Core base image from Microsoft. Microsoft maintains their Docker images on Docker hub. Docker hub is a container registry to manage your Docker images either by exposing the image publicly or maintain it privately. Private image responsibilities cost money. Visit Docker Hub website to learn more about image repository management.
Building your first Docker Image
Open PowerShell console as an administrator
Let's get started by pulling ASP.NET Core 2.2 docker image from Docker hub by executing below command.
docker pull mcr.microsoft.com/dotnet/core/aspnet:2.2
Your output should look similar to what is shown below:
Create a folder with your preference name whereever you prefer. I will use c:\docker\ for demonstration purpose.
ASP.NET Coreapplication package from this URL.
Invoke-WebRequest -UseBasicParsing -OutFile c:\docker\WebAppCore2.2.zip https://github.com/rahilmaknojia/WebAppCore2.2/archive/master.zip
What we are doing in above command is downloading packaged code that is already build to save time on building a package.
WebAppCore2.2.zipby using PowerShell 5.0 native command. If you do not have PowerShell 5.0 and above, you will have to manually extract the package.
Expand-Archive c:\docker\WebAppCore2.2.zip -DestinationPath c:\docker\ -Force
Now let's create a Docker file in c:\docker folder.
New-Item -Path C:\docker\Dockerfile -ItemType File
Go ahead and open C:\docker folder path in Visual Studio Code.
Now we will open
Dockerfileby double clicking on the file in Visual Studio Code to start writing required steps to build an image.
Copy and paste below code to
# Pull base image from Docker hub FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 # Create working directory RUN mkdir C:\\app # Set a working directory WORKDIR c:\\app # Copy package from your machine to the image. Also known as staging a package COPY WebAppCore2.2-master/Package/* c:/app/ # Run the application ENTRYPOINT ["dotnet", "WebAppCore2.2.dll"]
What we told the
Dockerfileis to pull a asp.net core base image from Docker hub . Then we ran a command to create a directory called
c:\apppath. We also told the container to set
c:\appas a working directory. That way we can access binary directly when the container is spun up. We also added a step to copy all the binaries from
c:\docker\WebAppCore2.2-master\Package\to destination path in container
c:\app. Once we had package staged in container, we told it to run the application by executing
dotnet WebAppCore2.2.dllso that the app would be accessible from outside the container. To learn more about
Dockerfilefor windows, check out this microsoft documentation.
Now that you have required steps to build an image, lets go ahead with below steps.
Dockerfileworking directory from PowerShell console. If you are already in that path you can ignore it.
Execute below command to build container image.
docker build -t demo/webappcore:2.2.0
What above command will do is create a docker image under
demopath. With the image name called as
webappcoreand version 2.2.0.
Your output should look like below once it is successful:
PS C:\docker> docker build -t demo/webappcore:2.2.0 . Sending build context to Docker daemon 9.853MB Step 1/5 : FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 ---> 36e5a01ef28f Step 2/5 : RUN mkdir C:\\app ---> Using cache ---> 8f88e30dcdd0 Step 3/5 : WORKDIR c:\\app ---> Using cache ---> 829e48e68bda Step 4/5 : COPY WebAppCore2.2-master/Package/* c:/app/ ---> Using cache ---> 6bfd9ae4b731 Step 5/5 : ENTRYPOINT ["dotnet", "WebAppCore2.2.dll"] ---> Running in 4b5488d5ea5f Removing intermediate container 4b5488d5ea5f ---> 9729270fe1ac Successfully built 9729270fe1ac Successfully tagged demo/webappcore:2.2.0
Once the image has been built, you are now ready run the container. Execute below command.
docker run --name webappcore --rm -it -p 8000:80 demo/webappcore:2.2.0
The above command will create new container called as
webappcore with parameters.
--rmis used to automatically remove the container after it is shutdown.
-itwill open a session into your container and output all the logs.
-pis used for creating external port and assigning it to internal port of a container. Port 8000 is exposed to outside container, and port 80 is used to access the app within the container.
demo/webappcore:2.2.0is the path to the docker image to run as a container.
Output of a running container
- Browsing your application from your local machine
This is it! You ran your first docker container in your local environment. Thank you for following the tutorial. Please comment below for any issue or feedback you would like to share.