What is a Docker image, and how does it relate to a container?
An image is a read-only, layered template (filesystem plus metadata); a container is a running instance of it with its own thin writable layer on top.
An image bundles everything an application needs to run: a minimal OS filesystem (for example Alpine), libraries, the application files, and metadata such as environment variables, the exposed port and the default command. It is built from a Dockerfile and stored as a stack of layers, one per filesystem-changing instruction, each layer recording only the difference to the one below.
Images are immutable. When you start a container, Docker stacks the image's read-only layers and adds one thin writable layer for that container. Everything the running process changes goes into that writable layer, so:
- many containers can start from the same image and share its layers on disk, which is why ten containers do not cost ten times the space;
- deleting the container throws away its writable layer, while the image stays unchanged.
The class/object analogy fits: the image is the class, each container an instance.
Because several container engines exist besides Docker (Podman, containerd, CRI-O), the Open Container Initiative (OCI) standardises the image format (how layers and their metadata look), so an image built with one tool runs on another.
* Shared read-only layers below, one private writable layer per container on top. *
Go deeper:
Fireship: Docker in 100 Seconds — images, containers and Dockerfiles in a fast two-minute overview.
Docker Docs: What is an image? — layers, immutability and how images are composed.
Wikipedia: Docker (software) — history, architecture and the components around the engine.