Book Image

ASP.NET Core MVC 2.0 Cookbook

By : Jason De Oliveira, Engin Polat, Stephane Belkheraz
Book Image

ASP.NET Core MVC 2.0 Cookbook

By: Jason De Oliveira, Engin Polat, Stephane Belkheraz

Overview of this book

The ASP.NET Core 2.0 Framework has been designed to meet all the needs of today’s web developers. It provides better control, support for test-driven development, and cleaner code. Moreover, it’s lightweight and allows you to run apps on Windows, OSX and Linux, making it the most popular web framework with modern day developers. This book takes a unique approach to web development, using real-world examples to guide you through problems with ASP.NET Core 2.0 web applications. It covers Visual Studio 2017- and ASP.NET Core 2.0-specifc changes and provides general MVC development recipes. It explores setting up .NET Core, Visual Studio 2017, Node.js modules, and NuGet. Next, it shows you how to work with Inversion of Control data pattern and caching. We explore everyday ASP.NET Core MVC 2.0 patterns and go beyond it into troubleshooting. Finally, we lead you through migrating, hosting, and deploying your code. By the end of the book, you’ll not only have explored every aspect of ASP.NET Core MVC 2.0, you’ll also have a reference you can keep coming back to whenever you need to get the job done.
Table of Contents (26 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Creating an ASP.NET Core MVC application on Linux with Docker


In this recipe, we will learn how to create an ASP.NET Core MVC application on Linux with Docker, the new Container technology.

Getting ready

We also have the option to use Docker to create, host, and publish an ASP.NET Core MVC application on Linux. Docker is a piece of operating system and software you will use to run your applications as a server (generally used as a VM locally, or in the cloud). We use Docker Containers as a lightweight VM with only the necessary files (system, tools, runtime, libraries, and code) for running applications, which starts much more quickly and runs independently from the environment it evaluates.

How to do it...

In our recipe, we will locally create a Docker Container with a Linux Ubuntu 17.10 VM to publish and host our ASP.NET Core MVC application.

In this recipe, we will do the following:

  1. Before beginning, you can install Nautilus, a file manager for a gnome-like explorer on Windows, which allows you to open the Terminal by right-clicking in a folder:
$ apt-get install nautilus-open-terminal 

$ killall nautilus && nautilus
  1. Download and install Docker on our Ubuntu VM:
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9 

$ sudo sh -c "echo deb https://get.docker.com/ubuntu docker main > /etc/apt/sources.list.d/docker.list" 

$ sudo apt-get update 

$ sudo apt-get install lxc-docker 
  1. Add the current user to Docker group, then log out and log in again:
$ sudo usermod -aG docker  

$ sudo service docker restart

Note

Note: We don't need to write sudo before the command lines anymore, because now we have enough rights to execute the command without writing sudo.

  1. Download project.json and Starup.cs from the HelloWeb sample, at https://github.com/aspnet/Home/tree/dev/samples/latest/HelloWeb. These two files are the only mandatory files required to run our application.
  2. Create a directory named HelloWeb, and place the two previous files inside.
  1. Create a file without an extension inside called Dockerfile, and insert in the following code:
# This code will download and use the last ASP.NET 5 Docker 
 # image based on Mono at 
 # https://github.com/aspnet/aspnet-docker/blob/master/1.0.0-
 # rc1-update1/Dockerfile

FROM Microsoft/aspnet:latest 

# This code copies the project into the folder and restores 
 # the packages using dotnet CLICOPY . /app
 WORKDIR /app
 RUN ["dotnet","restore"] 

# Open this port in the container 
EXPOSE 5000 

# Start application using DNX and the command from 
 # project.json to call kestrel 
ENTRYPOINT ["dotnet","run"] 
  1. Save and close Dockerfile.
  2. Then, verify the existing containers (not mandatory):
$ sudo docker images
  1. We can also check the running containers (not mandatory):
$ docker ps
  1. You can build your application now:
$ docker build -t 
  1. Run it (port 5004 is the default port for Kestrel):
$ docker run -t -d -p 5004:5004 
  1. You can see the home page at http:localhost:5004.

How it works...

A web server usually uses port 80 for web applications, while Kestrel will use port 5000 or 5004. We know that we cannot open ports lower than 1024 with default user permissions on Linux. To host ASP.NET Core applications using port 80, and to be production-ready on Linux, we will have to use Nginx with Kestrel to bring us all the web server features we need that Kestrel doesn't have, like load balancing, caching, and security, among others.

We can think of Docker Container as a mini VM with the minimum OS and software components you need to run the applications, isolated from the other application containers. Docker is lightweight, open, and secure, isolating applications from each other. You can consult the Docker documentation at https://www.docker.com/.

We can create a Docker Container on Windows, macOS, Linux (Ubuntu, RedHat, Suse, Arch, Debian, and so on), and on the cloud (Azure, AWS, Google, and so on). It will generally run on Linux distributions, but Windows will also support it.