Docker on containers and images
A container is an isolated process
- It's an ordinary process on your machine, such as a Node.js server, fenced off from the rest.
- It gets its own filesystem and its own view of the system. It can't see other containers.
- Two Linux kernel features do this: namespaces limit what it sees, cgroups what it uses.
- The app ships with everything it needs, so it runs the same on any machine with Docker.
A container shares the host's kernel, a VM brings its own
- A virtual machine (VM) pretends to be a whole computer and boots a full operating system.
- A container has no operating system to boot. It starts as fast as a process starts.
- The trade-off: containers are kept apart only by the kernel they all share.
| Virtual machine | Container | |
|---|---|---|
| Kernel | Its own, one per VM | The host's, shared |
| To start | Boots an operating system | Starts one process |
| Kept apart by | A pretend computer | The shared kernel |
An image is the package a container starts from
- An image holds everything a container needs to start: the app, its runtime and its files.
- Once built, an image never changes. One image can start any number of containers.
- The format is an open standard (OCI), so tools other than Docker can build and run images.
An image is a stack of layers
- Each layer is a set of file changes: add these files, change those.
- Layers are stored by a hash of their contents, so identical layers are stored once.
- Every image built on
node:24-slimreuses that image's layers instead of copying them. - A container adds one thin writable layer on top. Removing the container deletes it.
- Writable layerone per container, not in image
- RUN npm ciyour dependencies
- COPY package*.json ./your dependency list
- WORKDIR /appyour app folder
- node:24-slim layersshared with other images
An image name says where it lives and which version
- The full form is registry/namespace/repository:tag, like
ghcr.io/acme/todo-app:1.0. - Leave parts out and Docker fills them in:
nginxmeansdocker.io/library/nginx:latest. - A tag is a label that can be moved to a newer image.
latestis only the default tag. - A digest, written
@sha256:..., is a hash of the image, so it always means the same one.