In this recipe, we will learn how to create an ASP.NET Core MVC application on Linux with Docker, the new Container technology.
Creating an ASP.NET Core MVC application on Linux with Docker
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:
- 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
- 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
- Add the current user to Docker group, then log out and log in again:
$ sudo usermod -aG docker $ sudo service docker restart
- 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.
- Create a directory named HelloWeb, and place the two previous files inside.
- 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"]
- Save and close Dockerfile.
- Then, verify the existing containers (not mandatory):
$ sudo docker images
- We can also check the running containers (not mandatory):
$ docker ps
- You can build your application now:
$ docker build -t
- Run it (port 5004 is the default port for Kestrel):
$ docker run -t -d -p 5004:5004
- 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.