Sub topic 1
Images, Containers, and Why They Matter
Prerequisites
This chapter assumes you can use a terminal and run a simple web app locally, such as a Node.js Express server. You should have Docker Engine or Docker Desktop installed and understand HTTP requests and ports. No earlier Docker experience is needed.
The problem containers solve
An app that works on your laptop can fail on a teammate's machine or on a server. The cause is usually a difference in the environment: another Node.js version, a missing system library, or a different operating system setting.
A container packages the app together with the runtime and libraries it needs. The same package then runs the same way on any machine that has Docker.
Images and containers
Two terms carry most of Docker's model.
- An image is a read-only template. It holds the application code, the runtime, the libraries, and the default command to start the app.
- A container is a running instance of an image. Docker adds a thin writable layer on top of the image and starts the app as an isolated process.
The relationship is like a class and its objects. You build an image once and start as many containers from it as you need. Each container has its own writable layer, so changes inside one container do not affect the image or other containers.
Containers are not virtual machines
A virtual machine (VM) runs a full guest operating system on top of virtualized hardware. A container shares the host's kernel and isolates only the processes, files, and network. This makes containers small, often tens or hundreds of megabytes, and quick to start, often in under a second.
| Property | Virtual machine | Container |
|---|---|---|
| Includes its own OS kernel | Yes | No, it shares the host kernel |
| Typical start time | Tens of seconds to minutes | Under a second to a few seconds |
| Typical size | Gigabytes | Megabytes to hundreds of megabytes |
| Isolation | Strong, at the hardware level | Process level |
Try it
Run these commands to see an image and a container separately. The first command downloads the node:22-alpine image if you do not have it. The second starts a container from it, prints the Node.js version, and removes the container when it exits.
docker pull node:22-alpine
docker run --rm node:22-alpine node --version
docker images
docker ps -adocker images lists the images stored on your machine. docker ps -a lists containers, including stopped ones. Because of --rm, the container from the second command is already gone, but the image remains. This shows that deleting a container does not delete its image.
Why this matters for a web app
For the rest of this tutorial you will turn a Node.js web app into an image, run it as a container, and later connect it to a database. These benefits drive that work:
- Consistency: the app runs with the same Node.js version and dependencies everywhere.
- Isolation: two apps that need different Node.js versions can run side by side.
- Repeatability: a written recipe, the Dockerfile, builds the image the same way each time.
- Disposability: you can stop and replace a container at any time, so data you want to keep must live outside it. Volumes, covered later, handle that.
Common mistakes
- Mixing up the two terms. You build images and you run containers. Commands such as
docker buildact on images, anddocker run,docker stop, anddocker logsact on containers. - Assuming a stopped container is gone. It remains until you remove it with
docker rmor start it with--rm. - Expecting changes made inside a running container to persist in the image. To change an image, change the Dockerfile and rebuild.
Exercise
You run docker run --rm node:22-alpine node --version twice. How many images and how many containers exist on your machine afterward, counting only what this command created or used?
Show answer
One image (node:22-alpine) and zero containers. Each run created a separate container, but --rm removed each one when it exited. The image stays on disk and can be reused.
Exercise
A teammate edits a file inside a running container and expects the next container started from the same image to contain the edit. Will it? Why?
Show answer
No. The edit is stored in that container's writable layer, not in the image. A new container starts from the unchanged image. To keep the change, update the source files or Dockerfile and rebuild the image.