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.
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 machine | Container | |
|---|---|---|
| Isolates | a whole computer, kernel included | processes, files and network on a shared kernel |
| Typical size | gigabytes | megabytes |
| Start time | tens of seconds to minutes | well under a second |
| Pick it when | you need a different kernel or OS, or a hard security boundary | you ship apps that run on the same kernel |
The four moving parts
You type commands into the CLI; a background daemon does the work.
Image versus container
The image is the blueprint. A container is a running copy of it with a thin writable layer.
Where data survives
Anything written to the container layer disappears with the container, so persistent data belongs in a volume. Chapter 7 shows how.
Try it yourself
Run these in order. Version numbers on your machine will differ.
-
Step 1 · Client and daemon
docker versionprints 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 -
Step 2 · What the daemon is holding
docker infois 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 -
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 -
Step 4 · The four objects
You just used all four. The registry served
hello-world. The daemon stored the image.docker psis empty because you have not created a container. The next chapter does that.
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
- A container shares the host kernel; a virtual machine boots its own. That is why containers are small and fast.
- An image is a read-only template. A container is a running instance with one writable layer.
- The
dockerCLI only talks to the daemon (dockerd); the daemon builds, stores and runs. - A registry stores images.
pushuploads,pulldownloads. - Persistent data belongs in a volume, not in the container layer.
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.