1 / introduction
The virtual machine came first, the Docker container came later. However, it is important to be clear that Docker containers are not virtual machines. When I first encountered Docker in 2014, I compared it to a lightweight virtual machine. That's fine, because Docker's original secret of success was that it used less memory and started faster than a virtual machine. Docker keeps telling everyone that "the virtual machine takes minutes to start, while the Docker container takes 50 milliseconds".Copy the code
2 / virtual machine
<1> Infrastructure: It can be your PERSONAL computer, a server in a data center, or a cloud host. <2> Virtual machine management system (Hypervisor) : Using a Hypervisor, you can run multiple secondary operating systems on top of the main operating system. Hypervisors of type 1 support HyperKit for MacOS, Hyper-V, Xen, and KVM for Windows. There are VirtualBox and VMWare Workstation hypervisors of type 2. <3> Guest Operating System: Suppose you need to run three isolated applications, you need to use the Hypervisor to start three Guest Operating systems, that is, three virtual machines. These virtual machines are very large, perhaps 700MB, which means they will take up 2.1GB of disk space. Worse, they also consume a lot of CPU and memory, all sorts of dependencies. Each client operating system requires a number of dependencies to be installed. 1) If your application needs to connect to PostgreSQL, you need to install libpq-dev; 2) If you use Ruby, you should install Gems; 3) If you use other programming languages, such as Python or Node.js, you will need to install the corresponding dependent libraries. <4> Applications: With dependencies installed, applications can be run separately on each client operating system so that they are isolated from each other.Copy the code
3 / docker container
Understand the figure from bottom to top: <1> Infrastructure: same as virtual machine <2> Host Operating System: All major Linux distributions run Docker. For MacOS and Windows, there are also ways to "run" Docker. <3>Docker daemons: Docker daemons replace hypervisors. They are background processes that run on the operating system and manage the Docker container. All kinds of dependencies. For Docker, all of the application's dependencies are packaged in a Docker image, and Docker containers are created based on the Docker image. <4> Application: the source code of an application and its dependencies are packaged in Docker images. Different applications need different Docker images, and different applications run in different Docker containers, which are isolated from each other.Copy the code
4/ Compare virtual machines with Docker
Docker daemon can directly communicate with the main operating system and allocate resources for each Docker container. It can also isolate containers from the main operating system and separate containers from each other. Whereas virtual machines take minutes to start, Docker containers can start in milliseconds. Because there is no bloated secondary operating system, Docker can save a lot of disk space as well as other system resources. Having said so many advantages of Docker, there is no need to completely deny virtual machine technology, because the two have different use scenarios. Virtual machines are better at completely isolating the entire operating environment. For example, cloud service providers often employ virtual machine technology to isolate different users. Docker is usually used to isolate different applications, such as front-end, back-end, and database.Copy the code
5/ Physical machines vs VMS vs Docker containers
A physical machine is like a shipping dock: it has a space and various devices (in this case, the hardware resources of the server). A virtual machine is like a warehouse on a shipping dock: each warehouse has its own space for storing various goods or containers, and the warehouses are completely independent of each other, with separate application systems and operating systems. Docker containers are like shipping containers: packages of various goods that separate applications and the environments they depend on into standard containers.Copy the code
6/ Graphical metaphor Physical machine/VM/container
First, the physical machine is like thisCopy the code
2. Virtual machines look like thisCopy the code
The container looks like thisCopy the code