⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Docker & Containers Interview Questions Scenario 22 of 159 in Docker & Containers
Junior / Associate DevOps [L1] Docker Core Fundamentals [L1]
🎯 Target Role / Context: Docker Scenario #22 (L1) · Docker

Q: What is the difference between a Docker image and a container?

Image = read-only template (blueprint). Container = running instance of an image.

#Docker #L1 #Containers #Linux
🎙️ Candidate Opening & Architectural Context
"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."
Advertisement
⚡ Recommended Practice Lab

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.

1️⃣

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.
2️⃣

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 images or docker image ls shows repository, tag, ID and size.
  • Containers: docker ps -a shows running and stopped containers and the image each came from.
  • Create versus start: docker create makes the container (writable layer) without running it, docker start runs it, and docker run does both.
  • Trace the link: docker inspect --format '{{.Image}}' my-app returns 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.
Advertisement
3️⃣

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 a lists added (A), changed (C) and deleted (D) paths in that container's layer only.
  • Check the sibling: container b does not have /tmp/x, and the image is unchanged.
  • Remove it: docker rm -f a deletes the writable layer and the file with it. This is why state belongs in volumes.
  • Size accounting: docker ps -s shows 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.
4️⃣

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 commit turns a container's layer into a new image. It works but is not reproducible. Prefer a Dockerfile.
  • Tags are not images: re-pushing app:latest moves 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 rm it.
  • 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.
5️⃣

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 prune and unused images with docker 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.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Say it in one line: an image is a read-only filesystem template, a container is that template plus a writable layer and a running process. Then demonstrate it with <code>docker diff</code>."
⚡ 60-Second Elevator Pitch Talking Points
  • 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.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. 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.
  2. Saying images contain a kernel Containers use the host kernel. Images only carry userland files and metadata.
  3. 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.
  4. Using docker commit as a build process The result is undocumented and not reproducible, unlike an image built from a versioned Dockerfile.
Advertisement
Want more Docker & Containers scenarios?
Explore our complete collection of scenario-based Docker & Containers interview runbooks.
Browse All Docker & Containers Questions →