- ASP.NET Core 2 High Performance(Second Edition)
- James Singleton
- 929字
- 2021-07-08 09:39:08
Containerization with Docker
Docker is an implementation of container technology, which is a lightweight version of VMs. It was originally built on Linux Containers (LXC), but now it supports many other technologies, including Windows. For the same server, you can run a greater number of containers than traditional VMs as they don't always include a full OS with every image, and they share many resources. For example, if you always use the official Microsoft image, then Docker will share this base between your .NET Core apps while still keeping them separated and sandboxed.
The benefits of containers are analogous to those of shipping containers in the real world. By standardizing the unit of transportation and not worrying about the content, costs can be massively reduced. When you separate the concern of how to move containers around from what's inside them, you can share a common infrastructure and scale out much more easily. It also allows much greater scope for automation.
With Docker, you build your application in a container and deploy it. The container takes care of running the app and carries its entire configuration with it. The server you deploy to doesn't need to know anything about your app; it just needs to know how to run containers. This significantly reduces the configuration issues associated with getting an application running. All the main cloud-hosting providers offer a container service for hosting applications packaged in this way.
We've all struggled to get a code base working correctly on a new live server or fresh developer workstation. These chores can become a thing of the past. As another benefit, you can be confident that all the environments will have a consistent configuration, which will help cure the headaches associated with promoting releases up the chain of environments to production.
Docker operates a Docker Hub, which hosts images that are ready to use. Microsoft maintains a selection of .NET images published there, and they keep them up to date with patches.
You can pull these images down to get started really easily and quickly, depending on the speed of your Internet connection. There are .NET Core and ASP.NET Core images for both Linux and Windows Nano Server. The Linux images are based on the popular Debian distribution. There are even .NET Framework 3.5 and ASP.NET 4 images for Windows Server, so it's not just limited to the latest cross-platform Core products.
Docker can run on Hyper-V if you have a version of Windows that supports it (for example, Windows 10 Pro, Enterprise, or Education). It requires a 64-bit version with a build number of 10,586 or higher, which unfortunately rules out the LTSB version. To install Docker for Mac, you will need a machine manufactured in 2010 or later, running El Capitan 10.11 or higher.
However, there is an older version of Docker, called Docker Toolbox, which runs on VirtualBox (a cross-platform VM host). This is supported on many more operating systems, including the slightly more mature and stable ones.
As .NET Core is cross-platform, the simplest way to use Docker is in a Linux VM. It could be hosted on your Mac or Windows system or, as in our examples here, in the cloud.
Let's install Docker Community Edition from the Docker website using the command line and try it out. First we need to add the Docker GPG key to APT. It's a good idea to manually verify the fingerprint of this before going ahead and installing anything.
This process is very similar to the way that we installed .NET earlier in this chapter, but the ordering and syntax are slightly different. First we need to add the Docker GPG public key by downloading it with curl and piping it to apt-key, using this command:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Then, we display the fingerprint of the key that we added with the following command. You should check that this matches the one on the Docker website:
sudo apt-key fingerprint 0EBFCD88
Next we add the package source to our local list. This command has been split over multiple lines so that it is easier to read:
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
Then we update our new sources with the following command:
sudo apt-get update
Now we can install Docker with this simple command:
sudo apt-get install docker-ce
Assuming you get no errors here, the next step is to test that Docker is working with the following smoke-test container:
sudo docker run hello-world
You should get an output like the following:

Let's test out .NET Core in Docker with the following command:
sudo docker run microsoft/dotnet-samples
You should see the .NET bot as ASCII art:

Next, let's upgrade to the latest version of .NET Core and try a web app.
- 騰訊iOS測(cè)試實(shí)踐
- JIRA 7 Administration Cookbook(Second Edition)
- Hands-On RESTful Web Services with Go
- 精通MATLAB(第3版)
- HTML5+CSS3 Web前端開(kāi)發(fā)技術(shù)(第2版)
- Android項(xiàng)目實(shí)戰(zhàn):手機(jī)安全衛(wèi)士開(kāi)發(fā)案例解析
- Natural Language Processing with Java and LingPipe Cookbook
- Django 3.0應(yīng)用開(kāi)發(fā)詳解
- Mastering Apache Storm
- 大學(xué)計(jì)算機(jī)基礎(chǔ)實(shí)訓(xùn)教程
- JavaEE架構(gòu)與程序設(shè)計(jì)
- VMware vSphere 5.5 Cookbook
- Java程序設(shè)計(jì)及應(yīng)用開(kāi)發(fā)
- Getting Started with the Lazarus IDE
- 微信公眾平臺(tái)開(kāi)發(fā)最佳實(shí)踐