The single most important distinction in Docker is the difference between an image and a container. Beginners often use the words interchangeably, which leads to confusion when commands behave unexpectedly. This lesson makes the distinction crystal clear and introduces the layered filesystem that makes Docker images so efficient.
If you have done any object-oriented programming, the cleanest mental model is this: an image is like a class, and a container is like an object created from that class.
Run this to see it in practice:
docker run -d --name web1 nginx
docker run -d --name web2 nginx
docker run -d --name web3 nginx
docker psThree containers, one image. Each container can be stopped, restarted, or deleted without affecting the others or the image itself.
A Docker image is not one big file. It is a stack of read-only layers, where each layer records the filesystem changes made by one build step. For example, an image might consist of:
Layers are content-addressed and shared. If ten images on your machine all start from the same base layer, that layer is stored on disk exactly once. This is why pulling a second Node-based image is much faster than the first - Docker only downloads the layers it does not already have.
Inspect the layers of any image with:
docker history nginxWhen Docker starts a container, it does not copy the image. Instead it stacks a thin writable layer on top of the read-only image layers. Every file the container creates or modifies lives in that writable layer, using a copy-on-write strategy.
This has two important consequences:
You get images in two ways:
docker pull redis:7 # download from a registry
docker build -t myapp . # build from a DockerfileThe part after the colon is a tag, a human-readable label for a specific version. If you omit it, Docker assumes latest. Tags like redis:7-alpine indicate both the version and the base variant.
Many Docker subcommands come in pairs, and knowing which side you are working on avoids mistakes:
| Task | Image command | Container command |
| --- | --- | --- |
| List | docker images | docker ps -a |
| Remove | docker rmi nginx | docker rm web1 |
| Inspect | docker inspect nginx | docker inspect web1 |
You cannot remove an image while containers created from it still exist - Docker will refuse until those containers are removed.
redis:7 pin a specific image version; avoid relying on latest in production.Next up: Running and Managing Containers, where you will master the full container lifecycle - restart policies, resource limits, and interactive sessions.