Docker Images vs Containers: What the Difference Is
An image is a template and a container is a running instance. Learn the layers, the writable layer, and why the distinction matters.
By Velloreth
The distinction
An image is a read-only template.
A container is a running instance of an image, with a thin writable layer.
image (read-only layers)
+ writable layer
= container
You can run many containers from one image.
A change in a container does not affect the image.
The layers
Each Dockerfile instruction produces a layer.
FROM node:20-alpine # layer 1
WORKDIR /app # layer 2
COPY package.json . # layer 3
RUN npm ci # layer 4
COPY . . # layer 5
CMD ["node", "server.js"] # metadata
The layers are stacked to form the image.
A layer is shared between images that have it in common.
Why it matters: a base image is downloaded once and reused, and a rebuild reuses the unchanged layers.
The cache: a layer is rebuilt only if its instruction or a previous layer changed.
The ordering rule: put the rarely changing instructions first, so the cache is reused.
The writable layer
A container adds a thin writable layer on top of the image.
A write to a file in the image copies the file up to the writable layer, which is the copy-on-write mechanism.
The writable layer is removed with the container.
The consequence: a change made inside a running container is lost.
The fix:
| Need | Use |
|---|---|
| Data that must persist | a volume |
| A code change | rebuild the image |
| A config change | an environment variable or a mount |
| A debug session | a temporary container |
The anti-pattern: docker exec into a container, edit a file, and expect it to persist.
Tags and digests
A tag is a movable pointer.
node:20-alpine
node:latest
A digest is a content hash.
node@sha256:abc123...
A tag can be moved to a different image, which is why latest is not reproducible.
A digest always refers to the same image.
The rule: use a digest in production, and a tag in development.
The lifecycle
# Build an image
docker build -t myapp:1.0 .
# Run a container
docker run -d --name app myapp:1.0
# List the containers
docker ps
# Stop and remove
docker stop app && docker rm app
# The image is still there
docker images
The image outlives the container.
A container that is removed leaves the image intact.
An image that is removed fails if a container still uses it.
A container is a process
A container is a process on the host kernel.
The isolation:
| Mechanism | What it isolates |
|---|---|
| Namespaces | the process tree, the network, the mounts, the hostname |
| cgroups | the CPU, the memory, the I/O |
| Capabilities | the privileged operations |
| seccomp | the system calls |
A virtual machine runs a full operating system on virtualised hardware.
| Container | Virtual machine | |
|---|---|---|
| Start time | milliseconds | seconds |
| Kernel | shared with the host | its own |
| Size | megabytes | gigabytes |
| Isolation | process-level | hardware-level |
| Density | high | lower |
A container shares the host kernel, which is why the isolation is weaker than a virtual machine’s.
The practical consequences
A container is cheap to start, so a fleet of identical containers is normal.
An image is immutable, so a deploy is a new image rather than a change to a running container.
A container is disposable, so a restart is a normal recovery.
A volume is required for data, because the writable layer is not durable.
A digest is required for reproducibility, because a tag can move.
A layer cache is required for a fast build, so the instruction order matters.
The image build in a pipeline
An image is built once and promoted through the environments, which is the reason the build must be reproducible. The build job checks out the commit, builds the image, tags it with the commit SHA, and pushes it. The deploy job pulls that tag, and the same image runs in staging and in production. The second practice is to build the image from a lockfile, so the dependency versions are the ones that were tested. The third is to keep the build context small with a .dockerignore, because a large context slows the build and can leak a secret. The fourth is to scan the image before it is pushed, because a vulnerable base image is cheapest to catch at the build.
The build cache in practice
A Docker build is fast when the cache is warm and slow when it is cold, and the instruction order is what decides which. The rule is to copy the dependency manifest first, install the dependencies, and copy the source last, because the source changes on every commit and the manifest rarely does. The second rule is to combine the related commands in one RUN, because each instruction is a layer and a layer is a snapshot. The third is to use a multi-stage build, so the build tools are in one stage and the runtime is in another, which keeps the final image small. The fourth is to add a .dockerignore, because a large context is copied to the daemon on every build and can include a secret. A build that takes ten minutes is usually a build with a cache that is invalidated on the first instruction.
Common mistakes
Editing a file in a running container. The change is lost.
Storing data in the writable layer. The data is lost.
Using latest in production. Not reproducible.
A COPY . . before npm ci. The cache is invalidated on every code change.
A container treated as a virtual machine. Multiple services in one container.
A container that runs as root. An unnecessary privilege.
No .dockerignore. A large build context and a leaked secret.
A container with no health check. A broken container is not restarted.
A volume that is not backed up. The data is not durable.
A quotable summary
An image is a read-only template built from layers, and a container is a running instance with a thin writable layer that is removed with the container. A layer is shared between images, so the instruction order determines the cache, a tag is a movable pointer and a digest is immutable, and a container is a process on the host kernel rather than a virtual machine. Use a volume for data and a digest for reproducibility.
Related reading
For the related topics, see docker-basics-guide and dockerfile-best-practices. For the broader context, docker-volumes-explained and container-registry-basics.