Registries and image security
Building an image is half the job. It has to live somewhere every machine can pull it from, you need to know exactly which version is running, and it shouldn't ship with known holes or secrets baked in. This chapter covers registries, tags versus digests, scanning, and the habits that make an image hard to attack.
What a registry holds
A registry is a server that stores images: Docker Hub, GitHub Container Registry (GHCR), Amazon ECR, Google Artifact Registry, or one you run yourself. Inside it, each repository holds the versions of one image. The full name of an image tells Docker where to find it:
ghcr.io/demee/shop-api:1.4.0
└──┬──┘ └─┬─┘ └──┬───┘ └─┬─┘
registry owner repository tag
nginx:1.27 # no registry part means Docker Hub, and the owner "library" (official images)
Every image also has a digest: sha256: followed by a hash of its contents. The tag is a name you choose; the digest is calculated from what's inside.
Tags move, digests don't
A tag is just a label pointing at a digest, and anyone who can push can move it. nginx:1.27 today and nginx:1.27 next month are usually different images, because the maintainers rebuild it with security fixes. latest moves all the time. A digest can never point at anything else.
$ docker buildx imagetools inspect nginx:1.27 # shows the digest the tag points at right now
Name: docker.io/library/nginx:1.27
Digest: sha256:4ff1...c39e
$ docker pull nginx@sha256:4ff1...c39e # pull exactly that image, forever
$ docker images --digests # digests of what you already have
In a FROM line you can pin both, so the file stays readable and the build stays repeatable: FROM python:3.12-slim@sha256:.... Tools like Dependabot can bump the digest for you when a new base image comes out.
Pushing to a registry
Tag the image with the registry's name, log in, push. The three you'll meet most:
# Docker Hub
$ docker login # opens a browser or asks for an access token
$ docker tag shop-api:dev demee/shop-api:1.4.0
$ docker push demee/shop-api:1.4.0
# GitHub Container Registry: log in with a personal access token that has write:packages
$ echo "$GHCR_TOKEN" | docker login ghcr.io -u demee --password-stdin
$ docker tag shop-api:dev ghcr.io/demee/shop-api:1.4.0
$ docker push ghcr.io/demee/shop-api:1.4.0
# Amazon ECR: the repository must exist, and the password comes from the AWS CLI
$ aws ecr create-repository --repository-name shop-api
$ aws ecr get-login-password --region eu-central-1 \
| docker login --username AWS --password-stdin 123456789012.dkr.ecr.eu-central-1.amazonaws.com
$ docker tag shop-api:dev 123456789012.dkr.ecr.eu-central-1.amazonaws.com/shop-api:1.4.0
$ docker push 123456789012.dkr.ecr.eu-central-1.amazonaws.com/shop-api:1.4.0
Use --password-stdin so the token never lands in your shell history. In GitHub Actions you don't need a personal token for GHCR at all: the built-in GITHUB_TOKEN works once the workflow has permissions: packages: write (the CI/CD pipeline chapter shows the whole job). Docker Hub limits how often anonymous users can pull, so log in on CI machines and servers too.
A tagging scheme that tells you what's running
- The commit SHA on every image CI builds:
shop-api:3f9c2a1. You can always trace a running container back to the exact code. - A version for releases:
shop-api:1.4.0, plus1.4and1if people pull you by major version. - Never deploy
latest. It says nothing about what's inside, two servers can end up running different things under the same name, and rolling back to "the previous latest" is impossible.
Scanning for known vulnerabilities
Every package in an image (the OS packages from the base image and your app's libraries) might have published security holes, called CVEs. A scanner reads what's installed and checks it against vulnerability databases. Two common ones are Trivy (open source) and Docker Scout (built into Docker Desktop and the CLI):
$ trivy image shop-api:1.4.0
shop-api:1.4.0 (debian 12.7)
Total: 23 (UNKNOWN: 0, LOW: 14, MEDIUM: 6, HIGH: 3, CRITICAL: 0)
│ Library │ Vulnerability │ Severity │ Installed │ Fixed Version │
│ libssl3 │ CVE-2024-XXXXX │ HIGH │ 3.0.14-1 │ 3.0.15-1 │
$ docker scout cves shop-api:1.4.0 # the same idea with Docker Scout
# No Trivy installed? Run it as a container:
$ docker run --rm -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy image shop-api:1.4.0
Read the report by the Fixed Version column. A HIGH with a fix available usually just means "rebuild on a newer base image"; one with no fix yet is something to know about, not something you can patch today. In CI, fail the build only on what you can act on:
$ trivy image --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 shop-api:1.4.0
Scan again on a schedule too. An image that was clean when you built it collects new CVEs as they're discovered, even though nothing in it changed.
Give attackers less to work with
- Small base images.
-slim,alpineor distroless images contain fewer packages, so fewer CVEs and fewer tools an attacker can use after breaking in. Multi-stage builds (Docker chapter 3) keep compilers out of the final image. - Don't run as root. Add a user and switch to it with
USER, as in Docker chapter 3. - Lock the container down at run time when the app allows it:
A read-only filesystem means a break-in can't drop files; dropping Linux capabilities and blocking privilege escalation stops root-only tricks.$ docker run --read-only --tmpfs /tmp \ --cap-drop ALL --security-opt no-new-privileges:true \ shop-api:1.4.0 - Never put secrets in an image. Anything you
COPYorENVinto a layer can be read by anyone who can pull the image, even if a later layer deletes it. Pass secrets at run time (environment variables, mounted files, a secrets manager). If the build itself needs one, for example a token for a private package registry, use a BuildKit secret, which is never stored in a layer:# Dockerfile RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci $ docker build --secret id=npmrc,src=$HOME/.npmrc -t shop-api:dev .
SBOMs and signatures, briefly
A software bill of materials (SBOM) is a list of every package in an image, so that when the next big vulnerability is announced you can search your SBOMs instead of rescanning everything: docker scout sbom shop-api:1.4.0 or trivy image --format cyclonedx --output sbom.json shop-api:1.4.0.
Signing proves an image came from your pipeline and wasn't swapped. Cosign (from the Sigstore project) can sign images in CI using the workflow's own identity, with no long-lived key to guard, and a cluster can be set up to refuse unsigned images. You won't need it on day one, but you'll hear about it in interviews.
Common mistakes
- "denied: requested access to the resource is denied" on push: not logged in to that registry, the image isn't tagged with your namespace (
docker push shop-api:1.4.0tries Docker Hub'slibrary), or the token lacks write permission. - "no basic auth credentials" with ECR: the login expired. ECR passwords last 12 hours, so log in again.
- Pulled an old image without noticing: a server already had
app:latestcached. Deploy by SHA or version tag, or pass--pull always/docker compose pullfirst. - A secret in an old layer: deleting the file in a later layer doesn't help. Remove it from the build, rebuild, and rotate the secret, because it's already out.
- Ignoring the scanner because the list is long: filter to HIGH and CRITICAL with a fix available, fix those by updating the base image, and work down from there.