Q: What is the difference between a Docker image and a container?
Image = read-only template (blueprint). Container = running instance of an image.
Want to master this scenario in a live sandbox? KodeKloud's Docker Certified Associate (DCA) Hands-On Lab Course covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: The <strong>difference between a Docker image and a container</strong> is that an image is an immutable, layered template (filesystem plus metadata), while a container is a running or stopped instance of that image with its own writable layer, process and network. Interviewers use this basic question to check whether you understand why containers are disposable, why data needs volumes, and why one image can start many containers. Good answers go beyond the class and object analogy to layers, copy-on-write and content-addressable IDs.
Mental model: template versus instance
The image is what you build and ship. The container is what you run. Interviewers often follow up by asking where data lives, so keep the layer picture in mind throughout your answer.
- Image: an ordered stack of read-only layers plus a config (default command, environment, exposed ports). It is identified by a content digest such as
sha256:...and by tags that are movable labels. - Container: a process (or set of processes) started from an image, isolated with namespaces and limited by cgroups, with a thin writable layer on top.
- Copy-on-write: reads fall through to image layers. When a container modifies a file, it is copied into the writable layer, and the image stays untouched.
- One to many: a single image can back hundreds of containers that share the same read-only layers on disk.
- Image identity: an image ID is derived from its configuration and layer digests, so identical content has the same ID wherever it is built or pulled. A container ID is random and unique to that instance.
Hands-on: image vs container in practice
List each with the right command and watch the relationship.
docker pull nginx:1.27
docker run -d --name a nginx:1.27
docker run -d --name b nginx:1.27 # second container, same image
docker images nginx # 1 image
docker ps --filter ancestor=nginx:1.27 --format '{{.Names}}'
# a
# b
docker history nginx:1.27 | head # image layers, read-only
- Images:
docker imagesordocker image lsshows repository, tag, ID and size. - Containers:
docker ps -ashows running and stopped containers and the image each came from. - Create versus start:
docker createmakes the container (writable layer) without running it,docker startruns it, anddocker rundoes both. - Trace the link:
docker inspect --format '{{.Image}}' my-appreturns the image ID the container was created from. - Lifecycle states: an image is either present or absent. A container moves through created, running, paused, exited and removed, and each state is visible in
docker ps -a.
Verify the writable layer and immutability
Prove that changes land in the container, not the image.
docker exec a sh -c 'echo hi > /tmp/x'
docker diff a
# A /tmp/x
docker exec b ls /tmp/x
# ls: cannot access '/tmp/x': No such file or directory
docker rm -f a # writable layer gone
- Make a change:
docker exec a sh -c 'echo hi > /tmp/x'. - See the diff:
docker diff alists added (A), changed (C) and deleted (D) paths in that container's layer only. - Check the sibling: container
bdoes not have/tmp/x, and the image is unchanged. - Remove it:
docker rm -f adeletes the writable layer and the file with it. This is why state belongs in volumes. - Size accounting:
docker ps -sshows the writable layer size and the virtual size including the image. A large writable layer is a hint that the app writes data that should be in a volume.
Edge cases and common confusions
Interviewers often probe the corners of the model. Check each symptom against the evidence before changing anything.
- Committing a container:
docker committurns a container's layer into a new image. It works but is not reproducible. Prefer a Dockerfile. - Tags are not images: re-pushing
app:latestmoves the tag. The old image loses its tag and may become dangling. - Stopped is not gone: an exited container still holds its writable layer and keeps the image from being removed until you
docker rmit. - Not a VM: containers share the host kernel. An image holds userland files, not a kernel.
- Layer sharing: ten containers from one image do not use ten copies of the base layers. Only each container's own changes consume extra space, which is why starting many containers is cheap.
Production practice: build once, run many
The distinction drives release and operational habits.
- Immutable deploys: promote the same image digest through dev, staging and prod rather than rebuilding per environment.
- Pin by digest:
image: repo/app@sha256:...guarantees the exact bytes run. - Housekeeping: remove exited containers with
docker container pruneand unused images withdocker image prune, scheduled on build hosts. - Registry view: registries store images, never containers. To move a container's state elsewhere you must export its data, not push the container.
- An image is an immutable, layered template identified by a digest. A container is a runtime instance of it with a thin writable layer and isolated processes.
- I show the relationship with <code>docker ps --filter ancestor</code> and <code>docker diff</code>, which proves writes stay in the container.
- I automate by building once, pinning by digest and pruning stopped containers and unused images on build hosts.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Treating a container as a place to store data Its writable layer is deleted on <code>docker rm</code>, so data written there is lost. Use volumes.
- Saying images contain a kernel Containers use the host kernel. Images only carry userland files and metadata.
- Confusing a tag with an image Tags are mutable pointers. The same tag can reference different content over time, so pin by digest for reproducibility.
- Using docker commit as a build process The result is undocumented and not reproducible, unlike an image built from a versioned Dockerfile.