Click here to Skip to main content
15,867,568 members
Articles / Containers / Docker

Creating ASP.NET Core Application with Docker Support

Rate me:
Please Sign up or sign in to vote.
4.62/5 (14 votes)
16 Sep 2019CPOL7 min read 35.6K   218   36   14
In this article, we will see how to utilize docker for ASP.NET Core application.

Background

What is this buzz word ‘Docker? Let’s understand this.

Image 1

In the image, we can see that whale is carrying so many boxes and going very happily. These boxes are called Containers which contain everything that is required to run our software and Docker is the platform that provides this containerization. In technical terms, containers are nothing but VM images.

Image 2

Image source: from blog of Doug

The above picture depicts how traditional vm images look as compared to containers. Both the pictures have the same base as Infrastructure (which can be considered as a server) and the Operating System sits on top of that. In the first image, Hypervisor is a virtual box which can be anything like Hypervisor, VMWare, etc. and holds all the guest Operating Systems. Further, these guest Operating Systems contain source code and binaries, along with the application. Whereas, the second image, which is a container environment, has Docker Engine without any guest Operating System. So, where is that Operating System?

No worries. It is there. Docker Engine itself contains a small Operating System inside it, which holds everything as a container. Which means this small kernel is shared by each and every application/binary sitting on it.

In essence, we can say that Docker serves as a lightweight alternative to VMs as it doesn’t need Hypervisor.

What Does Docker Contain?

Docker contains Operating System, Source code, Environment variables (if any) and Dependent components to run the software. So, if anyone wants to run your software, they can simply take the container and get started, without putting effort to do the machine set up to make things work.

Why Do We Need It?

Many times, you must have heard developers saying – it is working fine on my machine, but I don’t know what is missing on your machine or say why the same software is not working on your machine? Such discussions usually pop up during the testing phase and as my personal experience, sometimes it takes hours to identify that small missed out dependency. Here, Docker comes to the rescue. As it is containerization, each and every dependency is packed in the form of containers and is available for both Linux and Windows. Hence, everyone using the software will have the same environment. Basically, the concept of docker has completely vanished the problem of mismatch environments. Isn’t it amazing?

Creating an ASP.NET Core Application with Docker Support

Open Visual Studio, go to New Project and Select ASP.NET Core Web Application as shown below:

Image 3

Once you click on Ok, you will get the below screen where you have to select the checkbox ‘Enable Docker Support (Requires Docker for Windows)’ and OS as ‘Windows’.

Image 4

There is also a hyperlink given ‘Requires Docker for Windows’, which can be used to install docker for Windows. In case you didn’t install docker before starting this exercise, it can be done now by clicking on this hyperlink. You can also verify the docker installation by typing the below command on Windows PowerShell:

Image 5

Docker –version

Once the application is created, you will see a file named Dockerfile is added to the application. At this point, if you want, you can change the views for your application.

What is Dockerfile?

On navigating to Solution Explorer, you will see Dockerfile having content similar to the below one:

FROM microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-1803 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.1-sdk-nanoserver-1803 AS build
WORKDIR /src
COPY ["ContainerBasedApp/ContainerBasedApp.csproj", "ContainerBasedApp/"]
RUN dotnet restore "ContainerBasedApp/ContainerBasedApp.csproj"
COPY . .
WORKDIR "/src/ContainerBasedApp"
RUN dotnet build "ContainerBasedApp.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "ContainerBasedApp.csproj" -c Release -o /app

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

Dockerfile is a text file which contains some commands. These commands are used to create docker image. Let’s take a brief of these commands:

  • FROM – This sets the base image for the following instructions and our base image is dotnet:2.1-aspnetcore
  • WORKDIR – This sets the working directory for storing the outputs, which are generated using the given command
  • EXPOSE – This informs docker that container listens on a given port
  • COPY – This copies files/folders from source to destination path
  • RUN – Executes given command on given image
  • ENTRYPOINT – This sets the container to be run as an executable

More information about Dockerfile can be found here.

Generating Docker Image From Dockerfile

Dockerfile is used to create a docker image as this file contains all the necessary information which is required to build an image. Build is the command which is used to create an image from the docker file. Now generating an image can be done either by using the Visual Studio UI or by using CLI. Let’s have a look at the Visual Studio path here.

Right click on the project and click on Publish…

Image 6

Next is to create container registry, which will deploy and store docker images and here, we will select Docker Hub as shown below:

Image 7

On clicking of Create Profile button, the below dialog will appear, in which you need to supply credentials to connect to Docker Hub.

Image 8

If everything is done correctly, the below screen would appear:

Image 9

Now click on Publish button. It will launch Docker executable and execute each and every line written in Dockerfile.

On successful publish, you will get a message ‘Successfully tagged containerbasedapp:latest‘ logged in Output window. But the same can also be verified in docker web site as shown below:

Image 10

It can also be verified by command prompt:

Image 11

Now it’s time to run the application and verify whether we can view our web page or not. For that, simply run the application from Visual Studio and you would be able to see your view. Here is what I can see from my sample application:

Image 12

Here, you can see that your application is running inside a docker.

Containerizing Existing Multi-Tier Application

Based on the application structure, whether it is a single standalone application, or it has many services, containerization can be introduced. Based on the application, sometimes single container suffices and sometimes one container per service or component is required. To enable the docker deployment in an existing application, go to Solution Explorer, right click on the project, select Add and then select Docker Support as shown below:

Image 13

Performing the above action will add the files required for Docker. Now let's understand a bit about docker-compose.

Docker-Compose

Docker-Compose is a tool to define and run multi-container applications. The best part about this tool is that it utilizes the same command as Docker, which means we need not remember any special commands in order to use this tool.There are various ways to use this tool but as part of this article, I'll be taking up yml file route. Below are the steps to create a yml file:

Installing Docker Compose Tool

This tool is automatically installed on Mac and Windows while installing Docker. But for Linux, it has to be installed explicitly and this can be done using the commands mentioned here.  Once the tool is installed, it can be verified using the below command:

Image 14

Deciding Storage Location for yml File

We can store the yml file anywhere on disk. So, let's create a folder on your machine and create an empty yml file inside that.

Adding Contents to yml File

Now before adding the contents to this file, we have to decide about our major components or say parts of the applications. For example, let's say we need to create a Web Application. So for this, the bare minimum requirement is a Web Server and a Database Server. On going through this link, webserver and database server images can be selected. Here, I'm selecting Windows IIS as the web server and MYSQL for my database.  Below is the screenshot of pulling IIS image using command mentioned here:

Image 15

Similarly, you can pull the image of database server, if it is not already available on your machine. Accordingly, I've updated my yml file as shown below:

Image 16

Validating yml File

Once file is created, next is to validate our yml file. Go to the directory where yml file is saved and run below command using CLI:

Image 17

If you can see the contents of the file, it means everything is fine.

Few Useful Commands

Command to run the yml file and creating the containers in the application:

docker-compose up

Command to stop all the running containers:

docker-compose down

Please note that the yml file created in this article is holding minimum information and is not sufficient for real production projects. In real-time projects, we may have to provide a little more information like ports, volume, etc. The complete information can be found here.

Key Takeaways

Containers provide an ability to package up and complete an application with all the required dependencies, which means the application will run on any other machine regardless of any settings. In one statement, we can summarize everything as - Dockerfile is a deployment script, Image is a packaged application, Registry is an image store and Container is a virtual environment for running the application.

References

History

  • 14th August, 2019: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United States United States
I am 5 times C# Corner MVP, a blogger and technical contributor at various forums like Microsoft TechNet, C# Corner, Code Project ,etc. I received several awards my community contributions. I have also presented technical contents as an speaker.

Comments and Discussions

 
QuestionDocker SSL connection issue Pin
Member 1310702831-Mar-21 1:43
Member 1310702831-Mar-21 1:43 
QuestionGood Article Pin
Mou_kol5-Sep-19 0:00
Mou_kol5-Sep-19 0:00 
QuestionOnce again, not convinced Pin
#realJSOP31-Aug-19 5:06
mve#realJSOP31-Aug-19 5:06 
AnswerRe: Once again, not convinced Pin
Shweta Lodha31-Aug-19 6:03
professionalShweta Lodha31-Aug-19 6:03 
GeneralRe: Once again, not convinced Pin
#realJSOP1-Sep-19 8:43
mve#realJSOP1-Sep-19 8:43 
If you build proper installers, that won’t be an issue.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

GeneralRe: Once again, not convinced Pin
brianlowe17-Sep-19 0:30
brianlowe17-Sep-19 0:30 
GeneralRe: Once again, not convinced Pin
#realJSOP17-Sep-19 4:05
mve#realJSOP17-Sep-19 4:05 
Questiongood article Pin
Member 1336835124-Aug-19 0:53
Member 1336835124-Aug-19 0:53 
BugIncorrect image Pin
Cinchoo23-Aug-19 8:27
Cinchoo23-Aug-19 8:27 
GeneralRe: Incorrect image Pin
Shweta Lodha31-Aug-19 5:56
professionalShweta Lodha31-Aug-19 5:56 
QuestionMissing image Pin
Joel WZ15-Aug-19 7:23
professionalJoel WZ15-Aug-19 7:23 
AnswerRe: Missing image Pin
Shweta Lodha31-Aug-19 6:03
professionalShweta Lodha31-Aug-19 6:03 
GeneralRe: Missing image Pin
Michael Gledhill17-Sep-19 2:31
Michael Gledhill17-Sep-19 2:31 
GeneralRe: Missing image Pin
Shweta Lodha9-Jun-21 12:18
professionalShweta Lodha9-Jun-21 12:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.