Docker与虚拟机有何不同

Docker与虚拟机有何不同

本文介绍了Docker与虚拟机有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在重新阅读,以尝试了解Docker与完整VM之间的区别。

I keep rereading the Docker documentation to try to understand the difference between Docker and a full VM. How does it manage to provide a full filesystem, isolated networking environment, etc. without being as heavy?

为什么要在Docker映像上部署软件(如果这是正确的说法),它如何提供完整的文件系统,隔离的网络环境等? )比简单地部署到一致的生产环境更容易?

Why is deploying software to a Docker image (if that's the right term) easier than simply deploying to a consistent production environment?

推荐答案

Docker最初使用的是(LXC),但后来切换到(以前称为 libcontainer ),它在与主机相同的操作系统中运行。这使它可以共享许多主机操作系统资源。此外,它使用分层文件系统()并管理网络。

Docker originally used LinuX Containers (LXC), but later switched to runC (formerly known as libcontainer), which runs in the same operating system as its host. This allows it to share a lot of the host operating system resources. Also, it uses a layered filesystem (AuFS) and manages networking.

AuFS是分层文件系统,因此您可以具有合并在一起的只读部分和写入部分。可以将操作系统的公共部分设为只读(并在所有容器之间共享),然后将每个容器分配给自己的用于写入的安装。

AuFS is a layered file system, so you can have a read only part and a write part which are merged together. One could have the common parts of the operating system as read only (and shared amongst all of your containers) and then give each container its own mount for writing.

因此,假设您有一个1 GB的容器映像;如果要使用完整的VM,则需要1 GB x所需的VM数量。使用Docker和AuFS,您可以在所有容器之间共享1GB的大部分空间;如果您有1000个容器,则容器OS的空间可能仍然只有1GB以上(假设它们都运行相同的操作系统)

So, let's say you have a 1 GB container image; if you wanted to use a full VM, you would need to have 1 GB x number of VMs you want. With Docker and AuFS you can share the bulk of the 1 GB between all the containers and if you have 1000 containers you still might only have a little over 1 GB of space for the containers OS (assuming they are all running the same OS image).

一个完整的虚拟化系统会为其分配自己的资源,并且共享最少。您将获得更多的隔离,但是隔离会更重(需要更多资源)。使用Docker可以减少隔离,但是容器是轻量级的(需要较少的资源)。因此,您可以轻松地在主机上运行数千个容器,并且它甚至不会闪烁。尝试使用Xen进行此操作,除非您拥有非常大的主机,否则我认为不可能。

A full virtualized system gets its own set of resources allocated to it, and does minimal sharing. You get more isolation, but it is much heavier (requires more resources). With Docker you get less isolation, but the containers are lightweight (require fewer resources). So you could easily run thousands of containers on a host, and it won't even blink. Try doing that with Xen, and unless you have a really big host, I don't think it is possible.

完整的虚拟化系统通常需要几分钟才能启动,而Docker / LXC / runC容器需要几秒钟,通常甚至不到一秒钟。

A full virtualized system usually takes minutes to start, whereas Docker/LXC/runC containers take seconds, and often even less than a second.

每种类型的虚拟化系统各有利弊。如果要使用有保证的资源进行完全隔离,则必须使用完整的VM。如果您只是想将进程彼此隔离,并希望在合理大小的主机上运行大量进程,那么Doc​​ker / LXC / runC似乎是可行的方法。

There are pros and cons for each type of virtualized system. If you want full isolation with guaranteed resources, a full VM is the way to go. If you just want to isolate processes from each other and want to run a ton of them on a reasonably sized host, then Docker/LXC/runC seems to be the way to go.

有关更多信息,请查看很好地解释了LXC的工作原理。

For more information, check out this set of blog posts which do a good job of explaining how LXC works.

部署一致的生产环境说起来容易做起来难。即使您使用和,总是有操作系统更新以及其他在主机和环境之间变化的东西。

Deploying a consistent production environment is easier said than done. Even if you use tools like Chef and Puppet, there are always OS updates and other things that change between hosts and environments.

Docker使您能够将操作系统快照到共享映像中,并使其易于部署在其他Docker主机上。在本地,dev,qa,prod等:全都相同。当然,您可以使用其他工具来做到这一点,但是却不那么容易或快速。

Docker gives you the ability to snapshot the OS into a shared image, and makes it easy to deploy on other Docker hosts. Locally, dev, qa, prod, etc.: all the same image. Sure you can do this with other tools, but not nearly as easily or fast.

这非常适合测试;假设您有成千上万的测试需要连接到数据库,并且每个测试都需要数据库的原始副本,并将对数据进行更改。经典的方法是在每次测试后使用自定义代码或使用之类的工具重置数据库-这非常耗时,这意味着必须按顺序运行测试。但是,使用Docker可以创建数据库的映像并为每个测试运行一个实例,然后并行运行所有测试,因为您知道它们将针对数据库的同一快照运行。由于测试是在Docker容器中并行运行的,因此它们可以同时在同一盒子上运行,并且应该更快地完成。

This is great for testing; let's say you have thousands of tests that need to connect to a database, and each test needs a pristine copy of the database and will make changes to the data. The classic approach to this is to reset the database after every test either with custom code or with tools like Flyway - this can be very time-consuming and means that tests must be run serially. However, with Docker you could create an image of your database and run up one instance per test, and then run all the tests in parallel since you know they will all be running against the same snapshot of the database. Since the tests are running in parallel and in Docker containers they could run all on the same box at the same time and should finish much faster. Try doing that with a full VM.

通过评论...

好吧,让我看看能否解释一下。您从基本映像开始,然后进行更改,然后使用docker提交这些更改,然后创建一个映像。该图像仅包含与基准的差异。当您要运行映像时,您还需要基础,它使用分层的文件系统在基础之上分层映像:如上所述,Docker使用AuFS。 AuFS将不同的层合并在一起,您将获得所需的内容。您只需要运行它。您可以继续添加越来越多的图像(图层),它将继续仅保存差异。由于Docker通常基于的现成映像构建,因此您几乎不必快照整个操作系统本身。

Well, let's see if I can explain. You start with a base image, and then make your changes, and commit those changes using docker, and it creates an image. This image contains only the differences from the base. When you want to run your image, you also need the base, and it layers your image on top of the base using a layered file system: as mentioned above, Docker uses AuFS. AuFS merges the different layers together and you get what you want; you just need to run it. You can keep adding more and more images (layers) and it will continue to only save the diffs. Since Docker typically builds on top of ready-made images from a registry, you rarely have to "snapshot" the whole OS yourself.

这篇关于Docker与虚拟机有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 23:01