How to build a simple ASP.NET Core app and deploy it with Docker
Hi everyone, in this series we are going to build a simple app using ASP.NET Core and we are going to deploy it with Docker. Also, I will explain what these two tools are and how to install them. This post will be a simple introductory step by step guide and hopefully you will be able to understand what ASP.NET Core and Docker are and also how to deploy a simple app with these incredible tools.
So, here are the tools that we are going to use:
- Docker for Windows
- .NET Core 2.1 SDK
- Visual Studio Code
What is ASP.NET Core?
It is a free and open source web framework for building modern, cloud-based, Internet-connected applications. ASP.NET Core is developed and powered by Microsoft as well as the community user base.
Now that we know what ASP.NET Core is, what is the difference between this and ASP.NET?
ASP.NET vs ASP.NET Core
ASP.NET is an open-source server-side web application framework designed for web development to produce dynamic web pages. It was developed by Microsoft to allow programmers to build dynamic web sites, web applications and web services. ASP.NET Core is the new generation of ASP.NET.
So, what are the differences? Well, here I made a beautiful table for you so you can see some of the features that these two have.
As you can see, ASP.NET Core can be used on Windows, IOS and Linux while ASP.NET can only be used on Windows. Also, ASP.NET Core stopped supporting Web Forms. You can pick both .NET Core or .NET Framework o work on your project when using ASP.NET Core. On the other hand, ASP.NET only supports .NET Framework runtime. Then, you can see that you can develop using VS for Mac with ASP.NET Core and its performance is better than ASP.NET.
If you want to know more about the features of these two and their differences, you can check the Microsoft documentation about it https://docs.microsoft.com/en-us/aspnet/core/choose-aspnet-framework?view=aspnetcore-2.1
For this tutorial, we are only going to use ASP.NET Core, so we can forget about ASP.NET and get through the installation of .Net Core.
Installing ASP.NET Core
- First, you need to download the .Net Core 2.1 SDK from here https://www.microsoft.com/net/download/windows
- After that, open the downloaded file and follow the installation steps and finally click on ”Finish”.
- Then, open the Command Shell and enter the following command: “
dotnet --version”. And the .NET Core SDK version should be there on the screen as a response.
What is Docker?
Before we talk about Docker we need to learn what containers are.
What are containers?
A container image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings. Available for both Linux and Windows based apps, containerized software will always run the same, regardless of the environment. Containers isolate software from its surroundings.
So we just talked about ASP.NET Core and cover its installation and also talked about containers, now it is time to explain what Docker is and how to install it.
Docker is an open-source project based on Linux containers. It uses Linux Kernel features like namespaces and control groups to create containers on top of an operating system. Docker works in collaboration with cloud, Linux, and Windows vendors, including Microsoft.
So why is Docker gaining so much steam?
1. Speed: Docker containers are very lightweight and fast.
2. Ease of use: Docker has made it much easier for anyone to take advantage of containers in order to quickly build and test portable applications.
3. Docker Hub: Docker users also benefit from the increasingly rich ecosystem of Docker Hub, which you can think of as an “app store for Docker images.” Docker Hub has tens of thousands of public images created by the community that are readily available for use.
4. Scalability: With Docker you can break out your application’s functionality into individual containers.
5. Lets face it, who doesn’t love Docker whale?
Here’s a beautiful image of it
Docker is available only for macOs and Windows 10. So if you have an earlier version of Windows you should install Docker Tools. Check out this link: https://docs.docker.com/toolbox/toolbox_install_windows/
- First you need to click on this link https://docs.docker.com/install/ and select the correct OS to download Docker.
- Follow up the installation steps.
- Open a terminal window (Command Prompt or PowerShell) and Run
docker --versionto ensure that you have a supported version of Docker:
> docker --version
Docker version 18.03.0-ce, build 0520e24
Lets get to work
Now that you installed ASP.NET Core and Docker we can start building out app and deploying it with Docker.
For this tutorial I will be using Visual Studio Code as the editor.
Creating the ASP.NET Core app
First of all, open the command prompt and create the ASP.NET Core app.
To do this you type the following commands:
- “mkdir DemoApp” — So here we create a directory where all the ASP.NET Core files will be into.
2. “cd DemoApp”.
3. “dotnet new web” — Create the ASP.NET Core web app
4. “code .” — Open DemoApp folder in VS Code.
Now we need to create a Dockerfile for our app. This Dockerfile will define the image of our container, think about it as a Class and its instance is the container, if you will.
5. Create a file inside DemoApp called “Dockerfile”. (VS Code may ask you to install Docker extension, press “YES”).
The first command that we add to Dockerfile is:
- FROM microsoft/dotnet:latest
This line represents the base image of the Docker image.
- COPY . /app
- WORKDIR /app
These lines are used to copy current working directory of application to “app” folder using the COPY command and set the working directory as “app” directory using WORKDIR command. Pretty simple!
- RUN [“dotnet”, “restore”]
- RUN [“dotnet”, “build”]
These commands will restore all of our CoreFx packages that our application uses, while creating the image and build the application to generate runtime assemblies.
Next, we will expose a port from within the container to external world (To the Host-Machine) by adding:
- EXPOSE 5000
And finally, we will define an entry point of our application as dotnet command and application’s default URL by adding:
- ENTRYPOINT [“dotnet”, “run”, “ — server.urls”, "http://0.0.0.0:5000"]
Creating a Docker Image
Now open-up the terminal and type the following command:
- docker build -t [image name] [directory]
Replace [image name] with your image name and [directory] as current working directory. I will be using:
- docker build -t demo-app-image .
Then hit enter and you will see that Docker has started creating the image
You can see the newly created image in the list of Docker images on our local machine by typing in the terminal:
- docker images
We run our Docker container by typing in the terminal as:
- docker run -p 80:5000 demo-app-image
Now open-up the browser and don’t navigate to your local host, instead navigate to http://Docker.
And there you go. You just deployed an ASP.NET Core app inside a Docker container.
So that’s it. You just created a simple ASP.NET Core app and deployed it with Docker. If you wannna know more about ASP.NET and Docker check out these Microsoft documentations:
ASP.NET Core is a new open-source and cross-platform .NET framework for building modern cloud-based web applications on…docs.microsoft.com
Here is my github project for this tutorial https://github.com/AxelSaredo/DemoApp