How Does Docker Run a Linux Kernel Under MACos Host

How does Docker run a Linux kernel under macOS host?

While the other answers are correct about the hypervisor, they don't answer your specific question.

The answer is "Docker [Desktop] for Mac" does run a Linux host VM with a replacement for boot2docker - LinuxKit developed and maintained by Docker for the purpose of making lightweight distributions.

https://blog.docker.com/2017/04/introducing-linuxkit-container-os-toolkit/

The uname you saw didn't have the keyword in it, but it seems to be included now, e.g. from Docker for Mac 18.03.1 I see:

Linux a8e079429a51 4.9.87-linuxkit-aufs #1 SMP Wed Mar 14 15:12:16 UTC 2018 x86_64 Linux

You can see links to the included versions on the release pages.
https://docs.docker.com/docker-for-mac/release-notes/

So it's not so different from the old days Docker Machine + VirtualBox + boot2docker,

for the new days, it's just
the provisioning is done internally by "Docker [Desktop] for Mac"
and VirtualBox is replaced by Apple's Hyperkit,
and the "default VM" is a bit more tucked away.

Why is it possible to run Linux containers on docker in MacOS

It runs on a VM. More info here

Can I change the Linux kernel that Docker uses on macOS?


Q1: Can I change which Linux kernel that Docker uses on macOS?

Safe and simple answer: No. Unless you want to mess-up directly with the Hyperkit VM that docker desktop for Mac is deploying for you when installing. I don't use Mac, I have no clue it this is even possible and strongly suggest you don't walk that path unless you seriously know what your are doing and are ready to reinstall everything if this breaks.

Q2: When does Docker upgrade the Linux kernel version?

When that change is incorporated to a new release and announced on the Docker for mac release notes. Latest kernel upgrade was made on 2020-05-27.

That being said, docker only uses the underlying kernel of the OS/VM where it is installed. If you really need a different kernel for a very specific reason, you can always deploy in parallel your own custom vm with the exact kernel you want, install a docker engine there and use it.

why docker instances can run on both windows and linux machine without the need of VM?

When running on Windows, recent versions of Docker use WSL2, which uses a lightweight virtual machine:

WSL 2 uses the latest and greatest in virtualization technology to run a Linux kernel inside of a lightweight utility virtual machine (VM).

As such, when using docker, you are actually running a full Linux kernel in a VM, despite the transparent experience. When you run a container, it is actually a container inside that VM.

Why can macos(x86) run docker arm container arm64v8/alpine?

You are correct, the image is not multi architecture, yet, docker can run it. Reason behind this is a kernel subsystem called binfmt_misc which allows to set the magic numbers of a binary file to specific actions for their execution. You can read more in this nice wikipedia post about it.

Docker for Mac is arriving prepared for the binfmt magic, so there is nothing to be done to enable it. It will be enabled out-of-box with the installation, all you need to do is to fetch the image and run. The details of the mechanism can be found in repository of docker-for-mac project on this link.

To explain it simply, the binary images have the magic number that allows the kernel to decide how to handle the execution. When binfmt_misc intercepts a file for which he recognizes the magic numbers he will invoke the handler that is associated with the magic numbers.

This alone is not enough to run the container. The next part of the magic is QEMU which is the emulator for various CPU architectures. The kernel (binfmt_misc) will invoke the quemy for each of the binaries that are ARM64 and will emulate the ARM64v8.

This is not limited to docker nor to the virtual machine that is running the docker on macOS. Any linux system can be configured to do this.

You can use following to install it setup Ubuntu to run the emulation.

sudo apt-get install qemu binfmt-support qemu-user-static # Install the qemu packages
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes # This step will execute the registering scripts

docker run --rm -t arm64v8/ubuntu uname -m # Testing the emulation environment

More details about the whole process of the set-up can be found in the qemu-user-static repository

OP: If you are wondering what is the usefulness of this, from my personal experiance, I am using this functionality heavily when porting applications from X86 to other architectures (mainly ARM64). This allows me to run build systems for various architectures without having a physical machine on which I can run the build.



Related Topics



Leave a reply



Submit