官术网_书友最值得收藏!

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.

Containers can significantly reduce costs, both in hosting hardware and time saved not configuring and manually security patching the OS yourself. The .NET team saved about $100,000 a year in hosting costs by moving to Docker, although they probably don't pay the market rate on Azure.

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.

One of the best reasons for using containers is that they allow you to develop as life like an environment as possible. This means that there should be fewer surprises and difficulties when the time comes to deploy your code to the production infrastructure.

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.

You can start a container from VS when using Visual Studio Tools for Docker, but Docker also allows you to try out .NET without installing anything. If you already have Docker installed, then simply download and run one of the .NET images.

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.

主站蜘蛛池模板: 洪雅县| 宁晋县| 五常市| 神池县| 沙河市| 蚌埠市| 徐闻县| 望谟县| 海原县| 来安县| 桓仁| 梨树县| 宝山区| 浦东新区| 英山县| 南郑县| 兰州市| 双柏县| 永善县| 延边| 永登县| 齐河县| 长兴县| 和顺县| 德令哈市| 黔西县| 夏津县| 墨玉县| 邛崃市| 松溪县| 延长县| 卫辉市| 西青区| 徐闻县| 阿克苏市| 南陵县| 修文县| 南江县| 黄平县| 那坡县| 黄冈市|