ASP.NET Core MVC 2.0 Cookbook
上QQ阅读APP看书,第一时间看更新

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.