Skip to content

容器对比虚拟 Machines

Compare 容器 and VMs 到选择正确 virtualization 方法 for 你的 workload.

你将学到

  • 如何虚拟 machines 工作
  • 如何容器工作
  • 键 differences: isolation, 大小, 速度
  • 当到 use 每个 and 如何它们 complement 每个 other

概念讲解

虚拟机

A virtual machine (VM) is a full computer simulated in software. It runs a complete operating system — kernel, drivers, system libraries — on top of a hypervisor (like VMware, Hyper-V, or KVM) that divides the physical hardware. Each VM is fully isolated: it has its own kernel, its own filesystem, its own network stack. This strong isolation is secure but comes at a cost: VMs are heavy (gigabytes), slow to start (minutes), and you pay for the full OS overhead even for a small application.

Containers

container 是一种 running process isolated by the host OS kernel, not a full virtual machine. Containers share the host's kernel and only package the application and its dependencies — not the OS. This makes them lightweight (megabytes), fast to start (seconds), and efficient: you can run hundreds of containers on a single host where you could only fit a few VMs. Docker is the most popular container runtime. The tradeoff is weaker isolation — containers share the kernel, so a kernel vulnerability affects all containers.

Key Differences

VMs provide strong isolation with separate kernels but are heavy and slow. Containers share the host kernel, making them lightweight and fast but with weaker isolation. VMs are best when you need strong isolation, different operating systems, or legacy applications. Containers are best for microservices, CI/CD pipelines, and cloud-native applications that need fast scaling and deployment. The choice depends on your isolation requirements, startup speed needs, and density goals.

Using Both Together

In practice, many teams use both. You run containers inside VMs — the VM provides isolation and a stable environment, and containers provide application-level packaging and density. Cloud platforms like AWS Fargate and Google Cloud Run do this automatically, running your containers in managed VMs. Kubernetes clusters typically run on VMs, with containers scheduled across them. The two technologies are complementary, not competing.

示例代码

              # Running a VM with multipass (Ubuntu VMs)
multipass launch --name myvm --cpus 2 --mem 4G --disk 20G
multipass exec myvm -- bash        # shell into the VM
multipass stop myvm                # stop the VM
multipass delete myvm              # remove the VM

# Running a container with Docker
docker run -d --name myapp -p 8080:80 nginx:alpine
docker exec myapp sh               # shell into the container
docker stop myapp                  # stop the container
docker rm myapp                    # remove the container

# Compare: container starts in ~1s, VM takes ~30s
            

VMs (通过 multipass) provide 完整 OS isolation 但 take ~30s 到开始. 容器 (通过 Docker) share kernel and 开始 in ~1s. Use VMs for isolation, 容器 for density.

动手试一试

常见错误

错误写法

Thinking 容器 provide 完整 VM-level security

正确做法

容器 share host kernel. For workloads 需要 strong isolation (multi-tenant, untrusted code), use VMs or sandboxed runtimes 如 gVisor or Firecracker.

错误写法

Using VMs 哪里容器会 是 better

正确做法

For microservices and cloud-native apps, 容器是 lighter, faster, and 更多 portable. Don't spin 上完整 VM for 每个 microservice — use 容器相反.

练习 容器对比虚拟 Machines

1 个练习

练习 容器对比虚拟 Machines

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误