Skip to content
Velloreth
Technology 6 min read

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

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:

NeedUse
Data that must persista volume
A code changerebuild the image
A config changean environment variable or a mount
A debug sessiona 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:

MechanismWhat it isolates
Namespacesthe process tree, the network, the mounts, the hostname
cgroupsthe CPU, the memory, the I/O
Capabilitiesthe privileged operations
seccompthe system calls

A virtual machine runs a full operating system on virtualised hardware.

ContainerVirtual machine
Start timemillisecondsseconds
Kernelshared with the hostits own
Sizemegabytesgigabytes
Isolationprocess-levelhardware-level
Densityhighlower

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.

For the related topics, see docker-basics-guide and dockerfile-best-practices. For the broader context, docker-volumes-explained and container-registry-basics.

Frequently asked questions

What is the difference between an image and a container?
An image is a read-only template that contains the filesystem and the metadata for a program. A container is a running instance of an image, with a thin writable layer on top. You can run many containers from one image, and a change in a container does not affect the image.
What is an image layer?
Each instruction in a Dockerfile produces a layer, which is a set of filesystem changes. The layers are stacked to form the image, and a layer is shared between images that have it in common. This is why a base image is downloaded once and reused.
Why is my change lost when I restart a container?
Because the writable layer is removed with the container. A change made inside a running container is not part of the image. To persist it, use a volume for the data, or rebuild the image with the change in the Dockerfile.
What is the difference between a tag and a digest?
A tag is a human-readable pointer such as latest or 1.2.3, and it can be moved to a different image. A digest is a content hash such as sha256:..., and it always refers to the same image. Use a digest in production for reproducibility.
Is a container a virtual machine?
No. A container is a process on the host kernel, isolated with namespaces and limited with cgroups. A virtual machine runs a full operating system on virtualised hardware. A container starts in milliseconds and a virtual machine in seconds, and a container shares the host kernel.
How many containers can run from one image?
As many as the resources allow. The image is read-only and shared, and each container adds only its writable layer and its process. This is why a container is cheap to start and why a fleet of identical containers is a normal pattern.