All guides

Image tags and digests: why latest breaks deploys

A tag can be moved to point to a different image. A digest is a hash of the image content and is fixed. The Kubernetes documentation advises against the latest tag in production.

Lines of code on a monitor, seen at an angle

Tags move, digests do not

The Kubernetes documentation puts it in one sentence: a tag can be moved to a different image, and a digest stays fixed. A digest has two parts: the hash algorithm, for instance sha256, and the hash value.

Docker says the same from the publisher's side. Image tags are mutable: the publisher can repoint a tag at a new image. Its example is alpine:3.21, which resolves to the latest patch version of 3.21.

latest is the default tag. If a FROM instruction has no tag or digest, the builder assumes latest. If a pod's image has no tag, Kubernetes assumes latest too.

What Kubernetes does with a tag

When a pod is submitted with no imagePullPolicy, the cluster sets one from the image reference:

  • a digest: IfNotPresent
  • the tag :latest, or no tag: Always
  • any other tag: IfNotPresent

The value is set when the object is first created. It is not updated if the tag or digest changes later.

IfNotPresent pulls the image only when the node has no local copy of it. With Always, the container runtime contacts the registry and resolves the tag to a digest every time the kubelet launches a container.

The same page spells out the risk. When the registry points a tag at different code, some of your pods can be running the old code while others run the new. It advises against :latest in production for two reasons: tracking which image version is running gets harder, and so does a proper rollback.

Only a change to the pod template triggers a Deployment's rollout. An updated container image in the template is one such change.

Pin by digest

Docker's example for a Dockerfile keeps the tag and adds the digest:

Dockerfile

# syntax=docker/dockerfile:1
FROM alpine:3.21@sha256:a8560b36e8b8210634f77d9f7f9efd7ffa463e380b75e2e74aff4511df3ef88c

In a pod, replace <image-name>:<tag> with <image-name>@<digest>. The Kubernetes page lists this reference among its examples:

Text

registry.k8s.io/pause@sha256:1ff6c18fbef2045af6b9c16bf034cc421a29027b800e4f9b68ae9b1cb3e9ae07

A reference can carry a tag and a digest. Then only the digest is used for pulling.

Docker prints the digest of an image after a pull, and again when you push. docker buildx imagetools inspect shows the Digest of an image in a registry.

Docker also states the cost. A pinned image is pinned to a version in time, and Docker does not pull updated versions of it. To get a security update, you change the digest.

A tag scheme from commit and version

docker/metadata-action extracts metadata from Git references and GitHub events, to tag and label Docker images. This step is from an example in its README:

YAML

      -
        name: Docker meta
        id: meta
        uses: docker/metadata-action@v6
        with:
          images: |
            name/app
          tags: |
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=sha

The README shows the result for a push of the Git tag v1.2.3: the tags name/app:1.2.3, name/app:1.2, name/app:sha-90dd603 and name/app:latest. name/app is the README's image name. Use your own.

  • type=sha outputs the short commit SHA with the prefix sha-.
  • type=semver requires a valid semver Git tag. {{version}} gives 1.2.3, and {{major}}.{{minor}} gives 1.2.
  • latest comes from the flavor input. Its default, latest=auto, generates the tag for type=semver. The input also accepts true and false.

In the README's first example, the build step of docker/build-push-action takes the list as tags: ${{ steps.meta.outputs.tags }}. That action has an output named digest, the image digest.

Immutable tags in the registry

Amazon ECR sets tag immutability per repository. name and us-east-2 are the documentation's values:

Shell

aws ecr put-image-tag-mutability --repository-name name \
  --image-tag-mutability IMMUTABLE --region us-east-2

A push with a tag that is already in the repository then returns ImageTagAlreadyExistsException. The ECR page contradicts itself on one point. Its introduction says tag immutability affects all tags, and that you cannot make some tags immutable while others are not. Further down, the same page documents an IMMUTABLE_WITH_EXCLUSION setting with tag exclusion filters. Test exclusions in your own repository before you rely on them.

Google Artifact Registry has a flag for Docker repositories:

Shell

gcloud artifacts repositories update REPOSITORY \
    --project=PROJECT-ID \
    --location=LOCATION \
    --immutable-tags

Once the setting is on, each tag stays bound to one image digest, and that includes the default latest tag. Deleting a tagged image and removing a tag from an image are no longer permitted.

Digests that stay current

  • Renovate recommends pinning Docker images to an exact digest and updates the digests for you. It keeps the tag in the FROM line for readability, and it opens a pull request when the image behind the tag is updated. Its docker:pinDigests preset pins Docker digests.
  • Dependabot, per Docker's best-practices page, raises pull requests to update base image tags and digests on a schedule when it is configured with package-ecosystem: "docker".

What to check first

  1. Search your manifests for :latest and for images with no tag. Kubernetes sets imagePullPolicy: Always for both.
  2. Read imagePullPolicy on objects that already exist. It was set at creation.
  3. Run kubectl describe pod and read the Image ID line. In the Kubernetes documentation's sample output it holds the image with its sha256 digest.
  4. If you deploy by digest, read the registry's cleanup rules. Azure Container Registry's retention policy page warns against a retention policy for untagged manifests when systems pull images by manifest digest.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides