Understanding Container Images
This lesson explains what a container image contains, how a Dockerfile builds it in layers, and how tags and digests identify it.
Learning outcomes
After completing this lesson, you will be able to:
- Explain the difference between a container and an image.
- Describe how Dockerfile instructions produce layers.
- Distinguish tags from digests.
- Identify the parts of a full image reference.
Containers and images
A container is a process that runs in isolation on a shared operating system kernel. The kernel gives it its own view of the file system, process list and network, and limits the CPU and memory it can use. Because a container does not boot a separate operating system, it starts in seconds and many containers fit on one host. This makes containers well suited to back-end services such as APIs, background workers and model gateways, which must scale out quickly.
A container starts from an image: a read-only package that holds the application, its language runtime, its libraries and the files it needs. The image is the same on a developer's laptop, in a test environment and in production, which removes the class of failure in which code works in one place but not another because a library version differs. The running container adds a thin writable layer on top of the image; anything written there is lost when the container is removed, so state belongs in a database, a cache or a storage service.
Images follow the Open Container Initiative (OCI) specifications, which define the image format and the distribution protocol. Any OCI-compliant registry can therefore store an image built by any OCI-compliant tool, and any compliant host can run it. Registries can also store other OCI artifacts, such as Helm charts and software bills of materials.
Layers and the Dockerfile
An image is built from a Dockerfile, a text file of instructions. Each instruction that changes the file system, such as COPY or RUN, produces a layer. Layers are stored once and shared between images, so ten images built on the same Python base image store that base only once, and a host that already has the base layers downloads only the layers that differ.
The following Dockerfile packages a small Python API. Its instructions are ordered so that the dependency layer, which changes rarely, comes before the application code, which changes often. When only the code changes, the build reuses the cached dependency layer and finishes quickly.
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
USER 1000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]Each instruction has a purpose. FROM selects the base image, which supplies the operating system files and the Python runtime. WORKDIR sets the directory in which later instructions run. EXPOSE documents the listening port but does not publish it; the host decides which port receives traffic. USER runs the process as a non-root user, which limits the damage that a compromised application can do. CMD gives the default command when the container starts.
Two further practices keep images small and safe. A multi-stage build compiles or installs in one stage and copies only the results into a slim final stage, so that compilers and build tools do not ship to production. A .dockerignore file stops local files such as virtual environments, test data and credentials from being copied into the build context.
Dockerfile instructions
Tags, digests and manifests
An image in a registry is described by a manifest, a small JSON document that lists its configuration and layers. Two kinds of reference point to a manifest, and the difference between them matters whenever a deployment must be repeatable.
- A tag is a human-readable name such as
orders-api:1.4.2ororders-api:latest. A tag can be moved: pushing a new image with the same tag points the tag at the new manifest. The taglatesthas no special meaning in a registry; it is simply the default tag when none is given. - A digest such as
orders-api@sha256:3f1a...is the SHA-256 hash of the manifest. It is calculated from the content, so it always identifies exactly the same image and cannot be moved.
A multi-architecture image uses a manifest list, also called an image index, that points to one manifest per platform, such as linux/amd64 and linux/arm64. A host pulls the manifest that matches its own platform. This is why a build tool may need a platform option when the build machine and the target host differ.
repo:1.4.2repo@sha256:...The following diagram compares deploying by the movable tag with deploying by a unique tag.

Tags, digests and manifests
Registries and repositories
A registry is a service that stores and distributes images. Inside it, images are grouped into repositories, one per application or component, such as orders-api or samples/embedding-worker; a slash creates a namespace that helps organize repositories by team. A full image reference therefore has three parts: the registry's login server, the repository and the tag or digest, as in contoso.azurecr.io/orders-api:1.4.2.
Public registries such as Docker Hub and Microsoft Artifact Registry (mcr.microsoft.com) distribute base images. Production applications should be stored in a private registry in the same cloud as the hosts that run them, which keeps pulls fast, avoids public rate limits and keeps proprietary code private. On Azure, that registry is Azure Container Registry, described in the next topic.
Parts of an image reference
Making a deployment repeatable
A team deploys its inference API by referencing contoso.azurecr.io/infer-api:latest. After a node restart, one replica began returning errors, although nobody had deployed anything. Investigation shows that a developer pushed an untested image with the tag latest an hour earlier. The team wants each environment to run exactly the image that was tested.
The cause is the movable tag. When the restarted replica pulled latest, it received the new image, while the other replicas kept the old one. The fix is to build each image with a unique tag, such as the build number or Git commit hash, and to deploy by that tag or by its digest. Promotion from test to production then changes only which unique tag the production deployment references, and a rollback returns to the previous tag.
Restricting who can push to the repository would reduce the chance of an accidental push, but it would not stop a legitimate new image from replacing the tested one under the same tag. Pulling with a policy that never pulls again would hide the problem until the next new node, which would still pull the changed image.
Why should a Dockerfile copy requirements.txt and install dependencies before copying the rest of the application code?
Show answer
Each instruction produces a cached layer. Dependencies change rarely and code changes often, so installing dependencies first lets a rebuild after a code change reuse the cached dependency layer instead of reinstalling every package.
Exam Tips
Remember for the exam
- A tag can move; a digest always identifies the same image.
- Order Dockerfile instructions so that rarely changing layers come first.
- Run containers as a non-root user and keep state outside the container.
- A full image reference is login server, repository and tag or digest.