Skip to the content ForoHackers
Log inJoin

Container vs VM: what is actually shared, and what is isolated?

ContainersLinuxVirtualization
#1190 · it_works_on_my_vm · · Report

I keep hearing "a container is a lightweight VM" and something about that sentence feels wrong. A test that confused me:

uname -r                          # on my host
docker run --rm alpine uname -r   # inside a container

Both print the same kernel version. In a VM they would differ. So what is a container really, and where exactly is the isolation?

#2065 · ssh_tunnel_vision · · Report

Your feeling is right. A container is a normal process (or a group of them) on the host kernel, wrapped in:

  • namespaces: separate views of PIDs, mounts, network, hostname, IPC, users, cgroups,
  • cgroups: limits and accounting for CPU, memory, I/O,
  • seccomp and capabilities: a filter on which syscalls it may make and which privileges it keeps.

There is one kernel. A VM has its own kernel on virtual hardware, and the hypervisor sits between it and the host. A container is a process wearing a costume.

#2066 · it_works_on_my_vm · · Report

That helps. So a bug in the kernel itself could be used from inside a container to reach the host?

#2067 · ssh_tunnel_vision · · Report

Yes. Shared kernel means shared attack surface: every syscall a container can make is code in the host kernel. A VM boundary has a much smaller interface (the hypervisor), which is why people who need strong isolation between tenants put containers inside VMs. Threat model decides, as always.

#2068 · yak_shaving_daily · · Report

You can build a poor man's container by hand and see there is no magic:

ls -l /proc/self/ns
sudo unshare --pid --fork --mount-proc sh
ps aux

Inside that shell ps shows only a couple of processes, and your shell is PID 1. Same kernel, different view. I did this on a Sunday and then built a root filesystem by hand. Do not do this unless you have the weekend free.

#2069 · it_works_on_my_vm · · Report

Tried it. My shell is PID 1 and ps shows two lines. That is the first time containers felt like a mechanism and not like a spell. Thanks both.

Log in or join to reply.