Difficulty: Beginner
What is the difference between a Docker image and a Docker container? How are they related?
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.
An image is not a single file. It is defined by an OCI manifest that points to:
RUN, COPY, ADD). Each layer is identified by a SHA-256 digest.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.
When you run docker run my-app, Docker:
ENTRYPOINT and CMD.So a container is image layers + writable layer + runtime configuration (ports, volumes, env overrides, restart policy) + an isolated process.
| Aspect | Image | Container |
|---|---|---|
| Mutability | Read-only, immutable | Has a writable top layer |
| State | Static artifact on disk or in a registry | Created, running, paused, exited |
| Created with | docker build, docker pull | docker run, docker create |
| Listed with | docker images | docker ps -a |
| Shared | Pushed to registries, reused by many containers | Lives on one host |
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.
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:
docker images (images) with docker ps (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.
Docker Image, Container, Image Layers, Writable Container Layer, Image Digest