Skip to content

Docker Fundamentals and How it Works Under the Hood

Published: at 02:10 PM

Have you ever wondered how docker works seamlessly across different operating systems (OS)? I write a Dockerfile on a Windows laptop. My teammate builds it on a Mac. CI builds and creates image on Linux. It works the same everywhere, even when it contains something like RUN chmod +x blah.sh. Windows has no chmod, so how does this work? How a simple docker buiuld ... command builds the same image across Linux, macOS, and Windows?

How Docker Runs the Same Dockerfile across OSs

The answer is that Dockerfile never runs on the host OS.

A container is a Linux process, not a tiny OS. A container does not bring its own kernel. It is a normal Linux process with a restricted view of the system. Linux gives Docker two kernel features for this:

So every Linux container needs a Linux kernel underneath it. Docker makes sure one is always there:

The docker command on our Mac or Windows machine is only a client. The real work happens in a daemon running inside that Linux environment. When a Dockerfile runs chmod, it is a real Linux syscall on a real Linux filesystem. Nothing is translated to NTFS or APFS.

Docker architecture

From Dockerfile to image

An image is built one instruction at a time, and most instructions produce a layer.

If nothing changed, Docker reuses the cached layer. That is why putting rarely changing steps first makes builds fast.

What an Image Actually is

An image is three kinds of files, all named by the hash of their content:

Registries store and serve these blobs as they are. Because they are named by content, identical layers are shared between images, and a push only uploads what the registry doesn’t have.

What Happens when we Run it

docker run mounts the read-only layers together using overlayfs and adds a thin writable layer on top. runc starts the CMD process inside that merged filesystem with fresh namespaces and cgroups. Nothing is copied and no OS boots, which is why containers start in milliseconds.

One Real Limit: CPU Architecture

Even though Docker smartly hides the OS differences with the Linux VM, but it cannot hide the CPU. The binaries inside the layers are compiled for one architecture, usually amd64 or arm64, and the image records which one.

An Apple Silicon Mac builds arm64 images by default. If we push one to an x86 server, we get exec format error. We need to pass platform flag to build architecture specific image e.g. docker buildx build --platform linux/amd64 . A multi-platform tag points to an index with one manifest per architecture, and each machine pulls the one that matches its CPU.

References