DevOps.Academy
CoursesCurriculumInterview prepPricing
◔My learningSign in
DevOps.Academy

Free, hands-on DevOps lessons. Made with care in India.

System status

Learn

All coursesLinux roadmapCurriculumInterview questionsCheat sheetsMy learning

Academy

PricingCertificates

Legal

PrivacyTermsContact

devops.rajeev.pro

DockerLesson 1 of 15
Your progress · 0%
On this page
  1. "It works on my machine"
  2. What a container actually is
  3. Bare metal, VMs and containers
  4. Docker and OCI
↖ Course roadmap
Lesson 1 · Introduction

Containers: what they are and why they won

What a container really is, why "it works on my machine" stopped being an excuse, how containers differ from VMs and bare metal, and where Docker and OCI fit.

20 min read · beginner · hands-on

By the end of this lesson you'll be able to

0 of 4 complete

"It works on my machine"

How software was shipped before containers. Picture a small team in 2012. A developer finishes a feature on her laptop. To get it onto the server, she writes a README:

  1. Install Python 3.10.
  2. Install these 14 libraries, at these exact versions.
  3. Install the PostgreSQL client and ImageMagick from the system package manager.
  4. Set these 6 environment variables and copy this config file to /etc/app/.

Someone in operations follows the README on the server, by hand. The app crashes.

Why it kept going wrong. The server isn't the laptop. It has Python 3.12 because another app needed it. One library installed a newer version than the README said. ImageMagick is configured differently. Each difference is small, but together they mean the code isn't running in the environment it was tested in. Every team lost days to this, and "it works on my machine" became a joke because everyone had said it. Worse, every server slowly drifted: after a year of manual fixes, no two servers were quite the same, and nobody could rebuild one from scratch with confidence.

What containers changed. Instead of shipping the code plus a list of instructions, you ship the code together with everything it needs: the language runtime, the libraries, the system tools and the default config, packed into one file called an image. The server no longer needs Python or ImageMagick installed. It only needs Docker.

How that fixes the problem. An image is built once, from a recipe (a Dockerfile), and then never changes. The exact image you tested on your laptop is the one that runs in CI, on the staging server and in production. If it works in one place, it works the same way everywhere, because it carries its environment with it.

That one property changes a lot:

  • Developers stop writing "install these 14 things" in the README. docker run is the setup.
  • Operations gets one way to run every app, whatever language it's written in.
  • Pipelines build one artifact and deploy it everywhere, as the CI/CD course explains.
  • Density. Many isolated apps can share one server without fighting over library versions.

In this lesson you'll see what a container really is under the hood, how it differs from a virtual machine, and where Docker fits among the tools that run containers.

Docker in 100 SecondsFireship · Watch on YouTube ↗

What a container actually is

A container is a normal Linux process that the kernel isolates. It's not a small VM and there's no second operating system inside it.

A good picture is a block of flats. All the flats share one building, its foundation, water and power: that's the host's kernel, the core of the operating system. But each flat has its own front door and walls, so tenants can't see into each other's rooms. And each flat has its own electricity meter, so one tenant can't use all the power. Two kernel features give a process exactly that:

  • Namespaces change what the process can see: its own process list, its own network interfaces, its own hostname and its own filesystem.
  • cgroups limit what it can use: how much CPU, memory and I/O.

The filesystem comes from the image: a read-only stack of layers holding the app and its dependencies. When a container starts, it gets a thin writable layer on top, which disappears when the container is removed.

You can see the isolation yourself:

$ docker run --rm alpine ps              # the process list inside a new container
PID   USER     TIME  COMMAND
    1 root      0:00 ps
$ docker run --rm alpine cat /etc/os-release | head -n 2
NAME="Alpine Linux"
ID=alpine
docker run --rm alpine ps
  • dockerbuild and run containers
  • runstart a new container
  • --rmdelete the container when it exits
  • alpinethe image to start
  • psthe command run inside, instead of the image’s default
docker run --rm alpine cat /etc/os-release | head -n 2
  • cat /etc/os-releasethe command run inside, instead of the image’s default
  • |pipe: send output to the next command
  • headshow the first lines
  • -nhow many lines

Inside the container, ps is PID 1 and sees nothing else on your machine, and the filesystem is Alpine Linux, whatever your host runs. --rm deletes the container when it exits, so you don't collect stopped containers while you experiment.

Bare metal, VMs and containers

Containers are the third step in how we share servers, and each step fixed a problem of the one before.

  • Bare metal. One physical server ran one main application. It was simple, but wasteful: many servers sat mostly idle, and a new server took weeks to order and set up.
  • Virtual machines. A hypervisor split one physical server into several virtual ones, each with its own full operating system. Far less waste, but every VM carries a whole OS: gigabytes of disk, its own memory and a boot time of a minute or more.
  • Containers. Apps share the host's operating system kernel and are only isolated from each other. No second OS to boot, so a container starts in well under a second and weighs megabytes.

The diagram and table compare all three:

A VM virtualises the hardware and brings its own kernel. A container isolates processes on the one kernel the host already has.© Diagram: DevOps Academy

A VM virtualises the hardware and brings its own kernel. A container isolates processes on the one kernel the host already has.© Diagram: DevOps Academy
Bare metalVirtual machineContainer
What you getA whole physical serverA full OS with its own kernelIsolated processes on the host kernel
Starts inMinutes (boot the hardware)Tens of seconds to minutesUsually under a second
SizeThe whole machineGigabytesMegabytes to a few hundred MB
IsolationCompleteStrong: separate kernelsGood, but one shared kernel
Use it forDatabases, raw performanceDifferent OSes, strong tenant isolationApps and services, CI jobs, microservices

They aren't rivals. In the cloud you'll nearly always run containers inside VMs: the VM gives you strong isolation from other customers, and containers let you pack many apps onto it.

On a Mac or Windows

Containers need a Linux kernel, so Docker Desktop quietly runs a small Linux VM and your containers run inside it. That's why uname -r in a container shows a different kernel from your Mac's. On a Linux server there's no VM: the container uses the host's kernel directly.

Docker and OCI

Docker made containers easy in 2013 with three ideas: a simple file to describe an image (the Dockerfile), a command line that builds and runs it (docker build, docker run), and a registry to share images (Docker Hub). The kernel features existed before; Docker made them usable.

Under the hood, Docker is several pieces:

PieceDoes
docker CLIWhat you type. Sends requests to the engine
Docker Engine (dockerd)Builds images, manages containers, networks and volumes
containerdRuns and supervises containers. Kubernetes uses it directly
runcTalks to the kernel to create the namespaces and cgroups

The Open Container Initiative (OCI) turned the image format and the runtime into open standards. That's why an image you build with Docker also runs on Podman, on containerd, on Kubernetes and on every cloud container service. Learn Docker and you've learned how containers work everywhere.

$ docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
alpine        latest    ...            ...            ~8MB        # yours will differ a little
hello-world   latest    ...            ...            a few kB
docker run hello-world
  • dockerbuild and run containers
  • runstart a new container
  • hello-worldthe image to start
docker images
  • imageslist images
A shared kernel is a shared risk

Containers are isolated, but not as strongly as VMs: a kernel bug can let a process escape. Don't run containers as root unless you need to, and don't run untrusted images. The Security stage of this course covers how.

In production

Pin images to a version tag, like node:22-alpine, never just latest. latest is whatever was pushed most recently, so the same deploy can run different code on Monday and on Friday.

⌘
Mini mission

Prove a container shares the host kernel

A teammate insists every container runs its own operating system, like a VM. Settle it on a Linux machine: record the host's kernel version and the kernel version seen from inside an Alpine container, side by side in one file.

Input: A Linux host with DockerOutput: ~/kernel-check.txt
Reveal one safe solution
$ echo "host:      $(uname -r)" > ~/kernel-check.txt
$ echo "container: $(docker run --rm alpine uname -r)" >> ~/kernel-check.txt
$ cat ~/kernel-check.txt
host:      6.8.0-48-generic
container: 6.8.0-48-generic
echo "host: $(uname -r)" > ~/kernel-check.txt
  • echoprint text
  • >write output to a file, replacing it
echo "container: $(docker run --rm alpine uname -r)" >> ~/kernel-check.txt
  • >>append output to a file
cat ~/kernel-check.txt
  • catprint a file

The numbers match because there's only one kernel. The container's files say Alpine, but the kernel underneath is the host's. On Docker Desktop you'll see the kernel of its Linux VM instead, which proves the same point.

Try it yourself

0 of 5 steps done

Run each task in your own account or terminal, then tick it off.

Knowledge check

Quick check

Question 1 of 4

What does every container on a Linux host share with the host?

Go deeper

Official reading for this lesson

  • GuideWhat is a container?Docker ↗
  • DocsDocker overviewDocker Docs ↗
  • DocsInstall Docker Engine on LinuxDocker Docs ↗
  • DocsDocker DesktopDocker Docs ↗
  • GuideOpen Container InitiativeOCI ↗
  • DocsOCI image specificationOCI on GitHub ↗
  • Docsnamespaces(7): the kernel feature behind isolationUbuntu manpages ↗

Or get every quiz answer right and it completes itself.

Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.

Next lesson →Under the hood: namespaces, cgroups and layered filesystems