Container Guardrails
Enforce container best practices including tag stability, registry allowlists, required labels, build traceability, and security configurations.
container to your lunar-config.yml:uses: github://earthly/lunar-lib/policies/container@v1.0.5
Included Guardrails
This policy includes 10 guardrails that enforce standards for your devex build and ci.
no-latest
Prevents use of the :latest tag (explicit or implicit) in base images. Using :latest creates non-reproducible builds and makes debugging difficult.
stable-tags
Requires base images to use stable tags: digests (sha256:...) or full semver (1.2.3). Partial versions like "node:20" can change unexpectedly and break builds.
digest-pinned
Requires base images to be pinned by digest (e.g. "node:20@sha256:..."). Any tag, full semver included, can be re-pushed with different content; a digest can't.
allowed-registries
Restricts base images to approved container registries only. Prevents supply chain attacks from untrusted image sources.
allowed-image-prefixes
Restricts base images to approved image paths, such as one project on a registry. Stricter than allowed-registries, which checks only the registry host.
required-labels
Ensures containers have required labels, checking both Dockerfile LABEL instructions and docker build --label flags. A label present in either source satisfies the requirement.
healthcheck
Requires a HEALTHCHECK instruction in the final stage of multi-stage builds. Enables container orchestrators to detect and restart unhealthy containers.
user
Requires a USER instruction to run the container as a non-root user. Running as root inside containers is a security risk and violates least privilege.
build-tagged
Requires container builds to use an explicit image tag via -t/--tag. Untagged builds produce anonymous images that cannot be tracked or deployed.
dockerfile-lint-clean
Requires Dockerfiles to pass hadolint linting with no issues at or above the configured severity threshold. Enforces Dockerfile best practices including pinned versions, proper instruction usage, and security hygiene.
How Guardrails Fit into Lunar
Lunar guardrails define your engineering standards as code. They evaluate data collected by integrations and produce pass/fail checks with actionable feedback.
Policies support gradual enforcement—from silent scoring to blocking PRs or deployments—letting you roll out standards at your own pace without disrupting existing workflows.
Learn How Lunar Works →Required Integrations
This policy evaluates data gathered by one or more of the following integration(s).
Make sure to enable them in your lunar-config.yml.
Configuration
Configure this policy in your lunar-config.yml.
Inputs
| Input | Required | Default | Description |
|---|---|---|---|
allowed_registries
|
Optional |
docker.io
|
Comma-separated list of allowed registries |
allowed_image_prefixes
|
Required | — | Comma-separated image path prefixes for allowed-image-prefixes, each starting with the registry host (e.g. "registry.example.com/approved"). Matches whole path segments. Leave empty to skip this check. |
required_labels
|
Required | — | Comma-separated list of required labels checked across Dockerfile and build commands. Leave empty to skip this check. |
hadolint_severity
|
Optional |
error
|
Minimum hadolint severity to enforce (error, warning, info, style) |
Documentation
View on GitHubContainer Guardrails
Enforces best practices for container definitions and builds including tag stability, registry allowlists, required labels, build traceability, and security configurations.
Overview
This policy plugin validates container definitions (Dockerfiles) and CI build commands against common best practices and security requirements. It helps ensure consistent, secure, and reproducible container builds across your organization.
Policies
This plugin provides the following policies (use include to select a subset):
| Policy | Description | Failure Meaning |
|---|---|---|
no-latest |
No :latest tags (explicit or implicit) |
Image uses :latest tag (explicit or implicit) |
stable-tags |
Tags must be a digest or contain a full semver (e.g., 1.2.3, v4-bpl-3.24.0, 9.6.1-jdk25-alpine) |
Image uses unstable tag (partial version, branch name, etc.) |
digest-pinned |
Base images must be pinned by digest (e.g., node:20@sha256:...). Stricter than stable-tags |
Image has no digest, whatever its tag |
allowed-registries |
Images must come from allowed registries | Image pulled from registry not in allowlist |
allowed-image-prefixes |
Images must come from allowed image paths, such as one project on a registry. Stricter than allowed-registries, which checks only the host |
Image's repository is not under any allowed prefix |
required-labels |
Required labels must be present (Dockerfile or build command) | Missing one or more required labels |
healthcheck |
HEALTHCHECK instruction must be present | Final stage missing HEALTHCHECK instruction |
user |
USER instruction must be present | Final stage missing USER instruction |
build-tagged |
Container builds must use explicit -t/--tag |
Build command missing image tag |
dockerfile-lint-clean |
Dockerfiles must pass hadolint linting | Dockerfile has lint issues at or above configured severity |
Required Data
This policy reads from the following Component JSON paths:
| Path | Type | Provided By |
|---|---|---|
.containers.definitions[] |
array | docker collector (dockerfile sub-collector) |
.containers.builds[] |
array | docker collector (cicd sub-collector) |
.containers.lint_results[] |
array | docker collector (hadolint sub-collector) |
Each check skips when the data it reads is absent, rather than reporting a
pass it didn't earn. On a repository with no Dockerfiles, the docker collector
writes no .containers data at all, so every definition-based check
(no-latest, stable-tags, digest-pinned, allowed-registries,
allowed-image-prefixes, healthcheck, user, dockerfile-lint-clean) skips;
build-tagged skips independently when no container build ran in CI.
required-labels and allowed-image-prefixes also skip when their variable
(required_labels, allowed_image_prefixes) is unset, since there is nothing
to require.
Note that an empty .containers.lint_results is not the same as an absent
one: it means hadolint ran and found no issues, so dockerfile-lint-clean
passes on it.
Installation
Add to your lunar-config.yml:
policies:
- uses: github://earthly/lunar-lib/policies/container@v1.0.0
on: ["domain:your-domain"]
enforcement: report-pr
include:
- no-latest
- stable-tags
- digest-pinned
- allowed-registries
- allowed-image-prefixes
- required-labels
- healthcheck
- user
- build-tagged
- dockerfile-lint-clean
# with:
# allowed_registries: "docker.io,gcr.io,ghcr.io"
# allowed_image_prefixes: "registry.example.com/approved,docker.io/library"
# required_labels: "org.opencontainers.image.source,git_sha"
# hadolint_severity: "warning" # error (default), warning, info, style
Examples
Passing Example
{
"containers": {
"definitions": [
{
"path": "Dockerfile",
"valid": true,
"base_images": [
{ "reference": "golang:1.21.5-alpine", "image": "golang", "tag": "1.21.5-alpine" },
{ "reference": "gcr.io/distroless/static:nonroot", "image": "gcr.io/distroless/static", "tag": "nonroot" }
],
"final_stage": {
"user": "nonroot",
"has_healthcheck": true
},
"labels": {}
}
]
}
}
Failing Examples
Using :latest tag (fails no-latest, stable-tags, digest-pinned)
{
"containers": {
"definitions": [
{
"path": "Dockerfile",
"valid": true,
"base_images": [
{ "reference": "node:latest", "image": "node", "tag": "latest" }
]
}
]
}
}
Using unstable tag (fails stable-tags, digest-pinned)
{
"containers": {
"definitions": [
{
"path": "Dockerfile",
"valid": true,
"base_images": [
{ "reference": "node:20-alpine", "image": "node", "tag": "20-alpine" }
]
}
]
}
}
Using disallowed registry (fails allowed-registries)
{
"containers": {
"definitions": [
{
"path": "Dockerfile",
"valid": true,
"base_images": [
{ "reference": "my-private-registry.com/app:1.0.0", "image": "my-private-registry.com/app", "tag": "1.0.0" }
]
}
]
}
}
Remediation
no-latest / stable-tags
Replace unstable tags with:
- Digest (most stable):
alpine@sha256:abc123... - Full semver (stable):
alpine:3.18.4. A tag that contains a fullmajor.minor.patchversion is accepted too, so registry- or vendor-specific prefixes and suffixes are fine (e.g.v4-bpl-3.24.0,9.6.1-jdk25-alpine,26.0.1_8-jre-alpine).
Avoid:
- Implicit latest:
FROM alpine - Explicit latest:
FROM alpine:latest - Partial versions:
FROM node:20orFROM node:16.1(no patch component)
digest-pinned
Add the digest to every FROM. You can keep the tag for readability, but the
digest is what gets pulled:
FROM node:20.11.1-alpine@sha256:<digest>
docker buildx imagetools inspect node:20.11.1-alpine prints the digest.
Renovate or Dependabot can keep pinned digests up to date.
allowed-registries
Use images from approved registries only. Update your Dockerfile or request the registry be added to the allowlist.
This check compares only the registry host, port included: once
registry.example.com is allowed, every project on it passes. To allow only
some paths on a registry, use allowed-image-prefixes.
allowed-image-prefixes
Pull base images from an approved path, e.g.
FROM registry.example.com/approved/python:3.13, or ask for the path to be
added to allowed_image_prefixes.
How images are matched:
- Each prefix starts with the registry host and matches whole path segments.
registry.example.com/approvedallowsregistry.example.com/approved/python, but notregistry.example.com/approved-other/pythonorregistry.example.com/library/python. - Docker Hub shorthand is expanded first, so
python:3.13isdocker.io/library/python. - Tags and digests don't affect the match. A port is part of the host: list
registry.example.com:5000/approvedif images are pulled with the port.
Base images supplied at build time aren't checked. With ARG BASE and no
default, the docker collector can't resolve FROM ${BASE} and records no base
image for it; an ARG default is resolved and checked. If none of a
component's base images can be read, the check skips instead of passing.
required-labels
Add the required labels to your Dockerfile:
LABEL org.opencontainers.image.source="https://github.com/org/repo"
LABEL org.opencontainers.image.version="1.0.0"
healthcheck
Add a HEALTHCHECK instruction:
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost/health || exit 1
user
Add a USER instruction to run as non-root:
USER nonroot
# or
USER 1000:1000
build-tagged
Add an explicit -t/--tag flag to your docker build command:
docker build -t myregistry.io/app:v1.2.3 .
required-labels
Labels can be added in either the Dockerfile or the build command:
# In Dockerfile (static labels)
LABEL org.opencontainers.image.source="https://github.com/org/repo"
# In build command (dynamic labels)
docker build -t app:v1 --label git_sha=$GITHUB_SHA .
dockerfile-lint-clean
Fix the hadolint violations reported in the check output. Common fixes:
# DL3008: Pin versions in apt-get install
RUN apt-get install -y --no-install-recommends curl=7.88.1-10+deb12u5
# DL3009: Delete apt lists after install
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
# DL3025: Use JSON form for CMD
CMD ["python", "app.py"]
To suppress specific rules, add a .hadolint.yaml to your repo root:
ignore:
- DL3008
- DL3013
Open Source
This policy is open source and available on GitHub. Contribute improvements, report issues, or fork it for your own use.
Common Use Cases
Explore how individual guardrails work with specific integrations.
Ready to Automate Your Standards?
See how Lunar can turn your AGENTS.md, engineering wiki, compliance docs, or postmortem action items into automated guardrails with our 300+ built-in guardrails.