What is a Docker image and how does it differ from a container?
Understand what a Docker image is and how it differs from a container — immutable layered templates versus running, writable instances you can scale.
Expected Interview Answer
A Docker image is a read-only, layered template that packages an application with its dependencies, while a container is a running, writable instance created from that image.
An image is built once from a Dockerfile and is immutable: it is composed of stacked, cached filesystem layers plus metadata. A container is what you get when Docker runs an image — it adds a thin writable layer on top and becomes a live process with its own isolated network, filesystem, and lifecycle. The image-to-container relationship is like a class to its objects: one image can spawn many independent containers.
- Immutable images guarantee reproducible builds
- Layer caching makes rebuilds and pulls fast
- One image can launch many identical containers
- Clear separation of build-time and run-time concerns
- Easy versioning and distribution via registries
AI Mentor Explanation
A Docker image is like the official rulebook and match template for a fixture — fixed, printed, and identical for everyone. A container is an actual match being played from that template, with a live scoreboard changing ball by ball. From one rulebook you can run many separate matches at once, each writing its own score while the shared template stays untouched.
Step-by-Step Explanation
Step 1
Author a Dockerfile
Describe the base, dependencies, and command that define how the image is built.
Step 2
Build the image
docker build produces immutable, cached layers — this is your read-only template.
Step 3
Store or share it
Tag and push the image to a registry so others can pull the exact same template.
Step 4
Create a container
docker run adds a thin writable layer on top of the image and starts it as a live process.
Step 5
Scale out
Run the same image many times to get many isolated containers, each with its own state.
What Interviewer Expects
- Image described as an immutable, layered template
- Container described as a running, writable instance
- The class-to-object relationship between them
- Awareness of the copy-on-write writable layer
- Understanding that one image yields many containers
Common Mistakes
- Using 'image' and 'container' interchangeably
- Claiming images are writable at runtime
- Thinking each container needs its own separate image
- Ignoring that container changes are lost unless committed or volume-mounted
- Confusing docker build (makes images) with docker run (makes containers)
Best Answer (HR Friendly)
“A Docker image is a fixed blueprint that packages an app and everything it needs, and a container is a live, running copy created from that blueprint. Just like one recipe can produce many meals, one image can start many independent containers.”
Code Example
# Build an immutable image from a Dockerfile
docker build -t myapp:1.0 .
# List images (read-only templates)
docker image ls
# Run three separate containers from the one image
docker run -d --name app1 myapp:1.0
docker run -d --name app2 myapp:1.0
docker run -d --name app3 myapp:1.0
# Each container has its own writable layer and lifecycle
docker psFollow-up Questions
- What are image layers and how does copy-on-write work?
- How do you persist data beyond a container's lifetime?
- What is the difference between docker build and docker run?
- How does image tagging and versioning work in a registry?
MCQ Practice
1. Which statement best describes a Docker image?
An image is an immutable, layered template; the writable layer and live process belong to a container created from it.
2. How many containers can be created from a single image?
Like objects from a class, one image can spawn many isolated containers, each with its own writable layer.
3. Where are runtime changes written inside a running container?
Containers use copy-on-write: changes go to a thin writable layer on top of the read-only image layers and are lost unless persisted.
Flash Cards
What is a Docker image? — A read-only, layered template packaging an app and its dependencies, built from a Dockerfile.
What is a Docker container? — A running, writable instance created from an image, with its own isolated filesystem and lifecycle.
Image-to-container relationship? — Like a class to its objects — one image can create many independent containers.
Where do container changes go? — Into a thin writable copy-on-write layer on top of the image; they vanish unless committed or stored in a volume.