Images vs. Containers

Difficulty: Beginner

Question

What is the difference between a Docker image and a Docker container? How are they related?

Answer

A Docker image is a read-only, versioned package that contains everything needed to run an application: a filesystem made of stacked layers plus metadata such as the default command, environment variables and exposed ports. A container is a running (or stopped) instance of that image: Docker adds a thin writable layer on top of the image layers and starts an isolated process using that filesystem. One image can back many containers at the same time, the same way one class can produce many objects.

What an image actually contains

An image is not a single file. It is defined by an OCI manifest that points to:

  • Layers: compressed tar archives, one for each filesystem-changing Dockerfile instruction (RUN, COPY, ADD). Each layer is identified by a SHA-256 digest.
  • A config object: the default CMD, ENTRYPOINT, ENV, WORKDIR, USER, exposed ports and labels.

Because layers are content-addressed, two images built from node:22-alpine share the same base layers on disk and over the network. A tag like my-app:1.4 is just a movable pointer; the digest (my-app@sha256:...) is the immutable identity.

What Docker adds to make a container

When you run docker run my-app, Docker:

  1. Mounts the image layers read-only using a union filesystem (overlay2 on most Linux hosts).
  2. Adds an empty writable layer on top, private to this container.
  3. Creates Linux namespaces (PID, network, mount, UTS, IPC, user) so the process gets its own view of the system.
  4. Applies cgroup limits for CPU and memory if you set them.
  5. Starts the process defined by ENTRYPOINT and CMD.

So a container is image layers + writable layer + runtime configuration (ports, volumes, env overrides, restart policy) + an isolated process.

AspectImageContainer
MutabilityRead-only, immutableHas a writable top layer
StateStatic artifact on disk or in a registryCreated, running, paused, exited
Created withdocker build, docker pulldocker run, docker create
Listed withdocker imagesdocker ps -a
SharedPushed to registries, reused by many containersLives on one host

What happens to data and state

A stopped container still exists, including its writable layer, until you run docker rm. Any file written inside it (logs, uploads, a SQLite database) survives a stop and start but is lost when the container is removed. That is why real data goes in volumes, not in the container layer. Changes in a container never flow back into the image; if you want a new image you rebuild it from a Dockerfile.

You also cannot remove an image while a container (even a stopped one) references it, unless you force it with docker rmi -f.

How this plays out in real deployments

In a typical CI pipeline at a company like Swiggy or Razorpay, the build stage produces one image tagged with the Git commit SHA and pushes it to a registry. Every environment (staging, production, ten replicas behind a load balancer) then runs containers from that exact same image, differing only in runtime config such as environment variables and secrets. This is the core promise of containers: build once, run the identical artifact everywhere.

Common mistakes interviewers listen for:

  • Saying a container is a "lightweight VM". It is a normal Linux process with isolation, sharing the host kernel.
  • Thinking that editing files inside a running container updates the image.
  • Confusing docker images (images) with docker ps (containers).
Anatomy of a running container. The image provides read-only layers; the container adds a writable layer and an isolated process on top.
  1. Container process (PID 1): Runs the ENTRYPOINT and CMD inside its own namespaces.
  2. Writable container layer: Private to this container and deleted with docker rm.
  3. App code layer (COPY . .): Read-only layer from the image.
  4. Dependencies layer (npm ci): Read-only layer shared by every container of this image.
  5. Base image layers (node:22-alpine): Shared on disk with every image built on the same base.

Code examples

One image, many containers

# Build one image from the Dockerfile in the current directory
docker build -t my-app:1.0 .

# Start two independent containers from the same image
docker run -d --name api-1 -p 3001:3000 my-app:1.0
docker run -d --name api-2 -p 3002:3000 my-app:1.0

# Images vs containers
docker images my-app
docker ps --filter ancestor=my-app:1.0

# Writes inside api-1 are invisible to api-2 and to the image
docker exec api-1 sh -c 'echo hello > /tmp/note.txt'
docker exec api-2 ls /tmp

# Stopping keeps the writable layer; removing deletes it
docker stop api-1 && docker rm api-1

Both containers share the image's read-only layers but each gets its own writable layer, so a file created in api-1 does not appear in api-2.

Key points

  • Image = read-only layers + config; container = image + writable layer + isolated process.
  • Images are identified immutably by digest; tags are movable pointers.
  • Data written in the container layer is lost on docker rm; use volumes for persistence.
  • Many containers can run from one image and share its layers on disk.
  • Containers share the host kernel; they are processes, not virtual machines.

Common follow-up questions

  • What happens to the data when a container is deleted, and how do volumes solve that?
  • Can a single image be used to run multiple containers at once?
  • What is the difference between an image tag and an image digest?
  • How is a container different from a virtual machine?
  • What does docker create do compared to docker run?

Interview tips

  • Open with the class vs object analogy, then go one level deeper into layers and the writable layer to show you understand the mechanics.
  • Mention namespaces and cgroups briefly; it signals you know a container is just an isolated process.
  • Tie it to CI/CD: one immutable image promoted across environments is the answer interviewers want to hear.

Concepts covered

Docker Image, Container, Image Layers, Writable Container Layer, Image Digest

More Docker & Containers questions