Stage 1 · Foundations · Chapter 1 of 20 · 5 min read

Docker fundamentals

Docker packages an app and its whole environment into one image, then runs that image anywhere as an isolated container.

key idea / Docker itselfcontainer / good pathhost / watch outproblem / blockedneutral

Why Docker exists

The classic bug is a program that works on one machine and not another. Docker removes the difference by shipping the environment too.

Why Docker existsAn app that works on one laptop is packed into an image together with its runtime and libraries, and the image runs the same on any machine.It works on my laptopNode 20, libssl 3packImageapp + runtime + libsrunRuns the same anywherelaptop, CI, serverAn image is the whole environment frozen into one immutable file.
Package the app together with its environment, and the machine stops mattering.

Containers versus virtual machines

Both isolate an app. A virtual machine fakes a whole computer; a container only fences off a process on a computer that already exists.

Virtual machines versus containersLeft: each virtual machine runs its own guest operating system on a hypervisor. Right: containers share the host kernel and run on the Docker engine, so each one carries only the app and its libraries.Virtual machinesContainersApp Abins / libsGuest OSfull kernel, GBsApp Bbins / libsGuest OSfull kernel, GBsHypervisorHost OSHardwareApp A+ its libsApp B+ its libsApp C+ its libsDocker engineHost OS, one shared kernelHardwareEach VM boots a whole OS: gigabytes, starts in minutes.Containers share the kernel: megabytes, start in ms.
The missing layer is the guest OS. That is where the container’s speed and small size come from, and also why every container on a host shares one kernel.
Virtual machineContainer
Isolatesa whole computer, kernel includedprocesses, files and network on a shared kernel
Typical sizegigabytesmegabytes
Start timetens of seconds to minuteswell under a second
Pick it whenyou need a different kernel or OS, or a hard security boundaryyou ship apps that run on the same kernel

The four moving parts

You type commands into the CLI; a background daemon does the work.

The four moving partsThe docker CLI talks to the daemon. The daemon stores images, runs containers, and pulls and pushes images to a registry.docker CLIwhat you typeDocker hostDocker daemon (dockerd)builds, runs, distributesImagesread-onlyContainersrunning processesstored on disk, run as processesREST APIRegistryDocker Hub, ECRpush / pull

Image versus container

The image is the blueprint. A container is a running copy of it with a thin writable layer.

Image versus containerAn image is a stack of read-only layers. A container is the same stack plus one writable layer on top.Image (read-only)Container (image + one writable layer)Your app codeYour app codenpm packagesnpm packagesNode.js runtimeNode.js runtimeAlpine baseAlpine baseWritable layer: logs, temp filesnever changes after builddeleted with the container
The writable layer is why a container can write logs without ever changing its image.

Where data survives

Anything written to the container layer disappears with the container, so persistent data belongs in a volume. Chapter 7 shows how.

Where data survivesA container writable layer is deleted when the container is removed. A volume lives outside the container and survives.ContainerWritable layerdeleted on docker rmApp processpostgres, node, nginxVolumemanaged by Docker, outsidemounted at startsurvives docker rm: keep databases here

Try it yourself

Run these in order. Version numbers on your machine will differ.

  1. Step 1 · Client and daemon

    docker version prints two blocks. Client is the command you typed. Server is the daemon. If the Server block is missing, the daemon is not running.

    $ docker version
    Client:
     Version:    27.3.1
    Server:
     Engine:
      Version:    27.3.1
  2. Step 2 · What the daemon is holding

    docker info is the daemon’s inventory: how many images and containers it has, and which storage driver it uses.

    $ docker info --format 'containers={{.Containers}} images={{.Images}} driver={{.Driver}}'
    containers=0 images=0 driver=overlayfs
  3. Step 3 · An image is not a container yet

    Pull stores an image. Nothing is running until run.

    $ docker pull hello-world
    $ docker images
    REPOSITORY    TAG       IMAGE ID       SIZE
    hello-world   latest    d2c94e258dcb   13.3kB
    $ docker ps
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
  4. Step 4 · The four objects

    You just used all four. The registry served hello-world. The daemon stored the image. docker ps is empty because you have not created a container. The next chapter does that.

Common mistakeTreating a container like a small VM: installing tools with docker exec and expecting them to stay. Changes in the writable layer vanish with the container. Put tools in the Dockerfile and data in a volume.

Recap

Quick check

You ran docker pull hello-world and docker ps shows nothing. What does that mean?

Show answer

Answer: Pull only stored the image. No container exists until docker run.

Pull only stores the image. A container exists only after docker run, and hello-world exits immediately, so docker ps -a would show it.

Why does a container start in well under a second while a virtual machine takes much longer?

Show answer

Answer: It shares the host’s running kernel instead of booting its own OS.

A container is an isolated process on the host’s already running kernel. A VM has to boot a whole guest OS first.

Docker fundamentals: common questions

What is the difference between a container and a virtual machine?

A virtual machine runs a whole guest operating system on virtual hardware. A container shares the host kernel and isolates only processes, files and network, so it starts in milliseconds.