How do Docker images and containers differ and relate?

This tests your grasp of the immutable template versus mutable runtime boundary. A good answer: an image is a read-only layered template with code and dependencies; a container is a runnable instance with a writable layer on top.
WHAT THIS TESTS: This question probes whether you understand the fundamental abstraction in Docker: the separation between a static artifact and a running process. Interviewers want to see that you know an image is not just a file but a layered, read-only template, and that a container is not a full VM but a process-level runtime that reuses those layers while adding a thin writable layer on top.
A GOOD ANSWER COVERS: Four things in order. First, define a Docker image as an immutable, read-only file that packages source code, libraries, dependencies, and tools, typically built from a Dockerfile using docker build. Second, explain that images are layered, starting from a base image and stacking modifications, which makes them reusable and efficient. Third, define a container as a runnable instance of an image that adds a writable container layer on top of the read-only image layers, allowing runtime changes without altering the original image. Fourth, describe the relationship: containers are created from images, multiple containers can share the same image, and changes in the container layer can be committed back into a new image using docker commit.
COMMON WRONG ANSWERS: Treating images and containers as interchangeable. Saying an image is just a tarball or zip file without mentioning layers or immutability. Claiming a container is a mini-VM rather than an isolated process using the host kernel. Forgetting that the writable layer is ephemeral by default and disappears when the container is removed unless committed or volumes are used.
LIKELY FOLLOW-UPS: How does layer caching speed up builds? What happens to the writable layer when the container stops? How do multi-stage builds relate to image size? Can two containers modify the same image layer? What is the difference between docker commit and building from a Dockerfile?
ONE CONCRETE EXAMPLE: Imagine the official Nginx image. It consists of a base Alpine Linux layer plus an additional layer containing the Nginx installation. When you run docker run nginx, Docker creates a container that mounts the read-only image layers and adds a writable layer on top. If you edit the default index.html inside the container, that change lives only in the writable layer. If you run docker commit on that container, Docker converts the writable layer into a new read-only image layer, producing a new image that includes your modified index.html.
Source: phoenixnap.com
Read the original → phoenixnap.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.