Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ASP.NET Core MVC 2.0 Cookbook

You're reading from   ASP.NET Core MVC 2.0 Cookbook Effective ways to build modern, interactive web applications with ASP.NET Core MVC 2.0

Arrow left icon
Product type Paperback
Published in Feb 2018
Publisher
ISBN-13 9781785886751
Length 668 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
Jason De Oliveira Jason De Oliveira
Author Profile Icon Jason De Oliveira
Jason De Oliveira
 Polat Polat
Author Profile Icon Polat
Polat
 Belkheraz Belkheraz
Author Profile Icon Belkheraz
Belkheraz
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Cross-Platform with .NET Core FREE CHAPTER 2. Visual Studio 2017, C# 6, IDEs, and Roslyn 3. Working with npm, Frontend Package Managers, and Task Runners 4. Reusing Code with NuGet 5. SOLID Principles, Inversion of Control, and Dependency Injection 6. Data Access - EF7 with Repository, SQL Server, and Stored Procedures 7. Accessing data with Micro ORMs, NoSQL, and Azure 8. Cache and Session - Distributed, Server, and Client 9. Routing 10. ASP.NET Core MVC 11. Web API 12. Filters 13. Views, Models, and ViewModels 14. Razor and Views 15. TagHelpers and ViewComponents 16. OWIN and Middleware 17. Security 18. Frontend Development 19. Deployment and Hosting 20. Other Books You May Enjoy

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: 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.

You have been reading a chapter from
ASP.NET Core MVC 2.0 Cookbook
Published in: Feb 2018
Publisher:
ISBN-13: 9781785886751
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime