On your laptop, notes-api runs fine. You send the code to web-1, and the notes page shows an error right away. The code is exactly the same. What differs is what's on the machine.
Docker is a tool made for this kind of problem. It wraps an application together with everything it needs (the Node version, the libraries, the operating system files) into one package called an image. Then Docker runs that package as a container, which is kept apart from the rest of the computer. Think of a shipping container: it can hold anything, but its outside is standard. So any ship, truck or crane moves it the same way, without unpacking it. In Docker, the shipping container is the image, and what's inside is your app and everything it needs. The ship, truck and crane are your laptop, the CI server and a VPS (a server you rent). All three run your app with the same command, and what's inside doesn't change on the way.
The "works on my machine" problem
Remember notes-api, our notes API (app.js, port 3000) that runs on the server web-1. Say your laptop uses Node 24, but web-1 still has Node 18. You write notes.toSorted(...) to sort the notes, newest first. This method only exists since Node 20. The code works on the laptop, and on web-1 it fails right away:
TypeError: notes.toSorted is not a function
"It works on my machine" happens because every machine has different contents: the Node version, library versions, the operating system, environment variables. Docker fixes it by carrying those contents along. The image notes-api:1.0 holds the Alpine Linux files (a very small Linux), Node 24, node_modules and your code. web-1 only needs Docker. The Node version installed on web-1 no longer matters, because the one in use is the Node 24 inside the image.
Images and containers
- Image: a package whose contents can't be changed (read-only). An image is only a set of files, and it doesn't run. An image is made from a recipe called a
Dockerfile(the next lesson). It is stored in a registry (a place to store and share images) such as Docker Hub (the lesson after that). - Container: an image that is running. A container is an isolated process, with a writable layer (where the container writes files) of its own on top of the image. One image can become many containers.
This is the terminal on the laptop (the folder ~/notes-api), exactly as in the Lab:
$ docker run -d --name notes-api -p 3000:3000 notes-api:1.0
3f2a9c1b7d4e8a51c6f0b2d97e4a1c385f6b0d2e9a7c4b1f8e3d6a0c5b9f2e71
$ docker run -d --name notes-api-2 -p 3001:3000 notes-api:1.0
8b7e6d5c4a3f2e1d0c9b8a7f6e5d4c3b2a1f0e9d8c7b6a5f4e3d2c1b0a9f8e7d
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b7e6d5c4a3f notes-api:1.0 "docker-entrypoint.s…" 15 seconds ago Up 15 seconds 0.0.0.0:3001->3000/tcp notes-api-2
3f2a9c1b7d4e notes-api:1.0 "docker-entrypoint.s…" 30 seconds ago Up 30 seconds 0.0.0.0:3000->3000/tcp notes-api
docker runcreates a new container from an image, then starts it. What it prints is the container ID (64 characters;docker psshows its first 12).-d(detached): the container runs in the background, so your terminal is free again right away.--name notes-api: the container's name. Names must be unique, so the second container needs another one.-p 3000:3000: port 3000 on the laptop is forwarded to port 3000 inside the container. The second container uses laptop port 3001, because 3000 is taken. The details are in the Docker Networking lesson.docker psshows the containers that are running (Up).docker ps -aalso shows the ones that have stopped (Exited). A stopped container is not gone: it stays until you delete it withdocker rm.
Then stop and delete both:
$ docker stop notes-api notes-api-2
notes-api
notes-api-2
$ docker rm notes-api notes-api-2
notes-api
notes-api-2
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
notes-api 1.0 5e1f8a2c7b90 2 hours ago 182MB
node 20-alpine 1c4e7b2a9f60 3 weeks ago 135MB
Both containers are gone, but the image notes-api:1.0 is still there. The image is like a mould, and a container is a cast from it that is running. Throwing away the cast doesn't damage the mould. What does go is any file the app wrote inside the container, because the writable layer belongs to the container.
Two other commands you'll use a lot:
docker logs notes-apireads what the app printed (itsconsole.log).docker stopsends the app the signal SIGTERM (asking it to stop), then waits for the app to clean up. If the app has not stopped after 10 seconds, Docker sends SIGKILL.
Reading docker ps output
The docker ps table is easy for people to read, but hard for a program. Its columns are separated by spaces, while some values contain spaces too (Up 2 hours). For a script, ask for your own format with --format. {{.ID}} is replaced by the container's ID, {{.Image}} by its image, and so on. Each \t between them becomes a tab, so the columns are separated by tabs:
$ docker ps --format "{{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}\t{{.Names}}"
3f2a9c1b7d4e notes-api:1.0 Up 2 hours 0.0.0.0:3000->3000/tcp notes-api
One line per container, five columns: ID, image, status, ports, name. This is how to split it in JavaScript, with the sample line from the exercise:
const line = "abc123\tnode:20\tUp 2 hours\t0.0.0.0:3000->3000/tcp\tmy-app";
const parts = line.split("\t");
// ["abc123", "node:20", "Up 2 hours", "0.0.0.0:3000->3000/tcp", "my-app"]
const container = { id: parts[0], image: parts[1], status: parts[2], ports: parts[3], name: parts[4] };
split("\t")cuts at every tab, soUp 2 hoursstays whole in one column.- With several containers, the lines are separated by
\n. First cut per line withsplit("\n"), then.map(...)turns each line into one object.
Containers vs virtual machines
A virtual machine (VM) also keeps apps apart, but in a much heavier way. Every VM runs a complete operating system (OS) of its own, including a kernel (the core of the operating system that manages the CPU, memory and files). VMs run on top of software called a hypervisor (a program that runs VMs). A container has no kernel of its own. All containers share the host OS kernel, the kernel of the computer that runs Docker. Docker only separates each container's processes, files and network.
| 3 apps run as | 3 VMs | 3 containers |
|---|---|---|
| Operating system | 3 complete OSes, each with a kernel | 1 host kernel, shared |
| Memory (example) | about 3 GB | about 180 MB |
| Start time (example) | 30 to 60 seconds | about 1 second |
Because they share the host's kernel, Linux containers need a host with a Linux kernel. On Mac and Windows, Docker Desktop quietly runs one small Linux VM, and all your containers run inside it.
⚠️ Containers that vanish, clash, or get misread
docker psis empty even though you just ran a container. The app has probably crashed, so the container has stopped. Check its status withdocker ps -a(Exited (1)), then read its error message withdocker logs <name>.- Keeping important data inside the container. Files the app writes disappear when the container is deleted. Data that must last goes in a volume (the Docker Volumes & Data lesson).
- Container names clash. You run
docker run --name notes-apiagain while the containernotes-apistill exists (even stopped). Docker refuses it withConflict. The container name "/notes-api" is already in use. Delete it first withdocker rm notes-api, or use another name. - Splitting the output on spaces. A status like
Up 2 hourscontains spaces, sosplit(" ")breaks the columns. Split on the\tfrom--format.
TL;DR: Docker solves "works on my machine" (the app runs on the laptop but fails on the server) with containers. The app and everything it needs are wrapped into an image (a package that can't be changed), then run as an isolated container. An image is like a mould, and a container is a cast from it that is running. One image can become many containers, and docker rm only deletes the container, not the image. The basic commands: docker run -d --name ... -p host:container image (-d runs it in the background, -p forwards a laptop port to a container port). The others: docker ps (the running containers), docker ps -a (stopped ones too), docker stop, docker rm, docker logs (what the app printed). Containers share the kernel (the core of the OS) of the host, the computer that runs Docker. A VM (virtual machine) has a complete OS of its own. So containers are much lighter and start in seconds. docker ps --format output is tab-separated: cut per line with \n, then per column with \t.
⚡ An image: the app plus everything it needs
Write:
TypeError: notes.toSorted is not a function
Meaning: The laptop uses Node 24, web-1 (the notes-api server) still has Node 18, and the method toSorted only exists since Node 20. So the same code fails on the server. Docker carries everything the app needs along, in one package called an image. The image notes-api:1.0 holds Alpine Linux, Node 24, node_modules and your code, so web-1 only needs Docker.
⚡ One image, many containers
Write:
$ docker run -d --name notes-api -p 3000:3000 notes-api:1.0
$ docker run -d --name notes-api-2 -p 3001:3000 notes-api:1.0
$ docker ps
Meaning: An image is a package that can't be changed, and a container is an image that is running. Here one image becomes two containers. -d runs a container in the background, the name after --name must be unique, and -p 3001:3000 forwards laptop port 3001 to port 3000 inside the container. docker ps shows the running containers.
⚡ Throw the container away, the image stays
Write:
$ docker stop notes-api notes-api-2
$ docker rm notes-api notes-api-2
$ docker images
Guess first: docker stop stops both containers, and docker rm deletes them. Is the image notes-api:1.0 gone too?
No. docker images (the list of images) still shows it. What goes is only the files the app wrote inside the container, because the writable layer (where the container writes files) belongs to the container.
Meaning: An image is like a mould, and a container is a cast from it. Throwing away the cast doesn't damage the mould. Data that must last goes in a volume (a place for data outside the container, in the Docker Volumes & Data lesson).
⚡ A container is not a VM
Write:
3 VMs 3 complete OSes, about 3 GB
3 containers 1 host kernel, about 180 MB
Meaning: A VM (virtual machine) runs a complete OS with a kernel (the core of the OS) of its own. Containers share the kernel of the host, the computer that runs Docker. So containers are much lighter and start in seconds.