Skip to content

Validate OCI images

pycontainer validate image verifies an existing OCI layout and checks the selected image configuration without a registry, daemon, cache or file writes. It does not build an image.

Terminal window
pycontainer validate image
pycontainer validate image --output dist/image --tag myapp:latest --format json
pycontainer validate image --tag myapp:latest --platform linux/arm64 --fail-on error
pycontainer validate image --config pycontainer.toml --ignore PCB004
pycontainer validate image --max-image-size-mb 500 --max-layer-size-mb 200
pycontainer build --config pycontainer.toml --validate --push

--output defaults to dist/image. A tag selects an exact org.opencontainers.image.ref.name or io.containerd.image.name on a top-level descriptor; a single-entry layout also supports these annotations on the index. --tag @sha256:<digest> selects top-level descriptors by digest. refs/tags files are not used for selection. Multiple named images require --tag even when their descriptors share a digest; multiple matching descriptors require a unique --platform. No platform is chosen implicitly. When selecting from an image index, nested indexes are not selectable, though their reachable contents are still verified.

ID Severity Check, rationale and remediation
PCB001 warning An absent or empty User, root, or a numeric zero user (including root:group and 0:group) runs with root privileges. Set a non-root user to limit privileges.
PCB002 unknown Unverified metadata cannot establish base-image mutability. This ID is reserved; no finding is emitted until verified provenance is available.
PCB003 error Neither Entrypoint nor Cmd is set, leaving no default process. Set at least one.
PCB004 warning Missing or empty org.opencontainers.image.source or org.opencontainers.image.version labels hinder traceability. Set both labels.
PCB005 error A non-empty environment variable whose name contains PASSWORD, TOKEN, or SECRET (case-insensitive) persists a likely secret in the image. Supply secrets at runtime instead.
PCB006 warning The stored image size exceeds max_image_size_mb. Reduce the base image or layer contents, or raise the threshold. Evaluated only when the threshold is set.
PCB007 warning The largest stored layer exceeds max_layer_size_mb. Slim that layer or choose a smaller base image, or raise the threshold. Evaluated only when the threshold is set.
PCB008 error An ExposedPorts key is not <1-65535>[/tcp|udp|sctp], or two keys normalize to the same port and protocol. Fix the image port metadata.
PCB009 warning A recognized server’s TCP listener ports differ from the exposed TCP ports, in either direction. Align the command and exposed-ports.
PCB010 info Ports are exposed but the listener cannot be determined: the server has no explicit port, uses a socket or file descriptor, is shell-wrapped, or the command is not recognized. The listener check is skipped rather than guessed.
PCB011 warning A port below 1024 is exposed while the user is non-root. Listen on 1024 or higher, or ensure the runtime permits binding it.
PCB012 info UDP or SCTP ports are present. They are preserved unchanged and excluded from the listener check.
PCB013 info Base-image ports were dropped because configured exposed-ports replace ExposedPorts. Reported only by build --validate.

info findings are reported but never affect the exit code, whatever --fail-on threshold is used.

Findings contain a rule ID, severity, message and remediation. Environment names and values are not printed. Image labels and annotations are claims, not proof that a base image is immutable; even a base-digest label leaves PCB002’s provenance status unknown.

Pass a configuration file explicitly to either command; it is not discovered automatically. Both [build.validate] and [tool.pycontainer.validate] are supported:

[build.validate]
ignore = ["PCB001", "PCB004"]
max_image_size_mb = 500 # optional; total stored image size
max_layer_size_mb = 200 # optional; largest single stored layer

Repeat --ignore RULE to add CLI suppressions to the TOML list. Unknown IDs, invalid types and unknown validation settings are errors. PCB002 is a known, reserved ID, so ignoring it is currently a no-op. Suppressions apply only to findings, never to layout or integrity errors. Configuration, ignore and layout diagnostics redact declared build-argument values, as validate config does; validate image redacts declared defaults because it accepts no --build-arg. Port findings redact raw keys before JSON escaping and truncation in both text and JSON output; build --validate uses the resolved argument values. Both commands also redact declared defaults from key-normalization collisions (for example my-key and my_key), which are reported before other checks.

--max-image-size-mb and --max-layer-size-mb (on validate image, and on build together with --validate) override the matching TOML key only when supplied; otherwise the TOML value, if any, applies. Thresholds are positive, finite decimal numbers of MiB (1 MiB = 1,048,576 bytes). CLI literals and TOML numbers retain their decimal precision and are compared exactly without binary rounding, so 0.1 means 104,857.6 bytes. A size equal to the threshold passes; only a larger size warns. Values may have at most 1,000 coefficient digits (including trailing zeros) and an adjusted decimal exponent from -1,000 through 1,000 (the exponent of the leading significant digit). For example, 1e-400 is supported, while an effective value of 1e5000 is rejected. After CLI overrides are applied, zero, negative, non-finite, boolean or non-numeric values (including TOML strings), and values outside these precision/magnitude limits, are configuration or usage errors with exit 2. Without a threshold, no size finding is produced. The TOML keys use underscores only.

--fail-on warning is the default; --fail-on error still reports warnings, but does not fail solely because of them. build --validate always fails on any unsuppressed warning or error, after writing the layout and before a push. Its --ignore option requires --validate, and validation cannot be combined with --dry-run. Without --validate, the build output is unchanged. For multi-platform builds, every built platform must pass before pushing the image index. When combined with --scan, both gates must pass.

Exit code Meaning
0 No unsuppressed finding meets the threshold
1 At least one finding meets the threshold
2 Usage, configuration, layout or integrity error

JSON output contains image (selected digest and platform), provenance.status, a findings array and a size object. Each finding has rule_id, severity, message and remediation. Usage and validation errors are written to stderr with exit 2 and no JSON report on stdout.

Every successful validation reports sizes for the selected image, whether or not a threshold is set. They come from descriptors whose digests and sizes were already verified against the blobs on disk; a descriptor that disagrees with its blob is an integrity error (exit 2), never a size finding, and ignoring PCB006 or PCB007 does not suppress it.

  • image_bytes: the config descriptor size plus the sum of every layer descriptor in the manifest. A layer listed twice counts twice; the manifest itself is excluded.
  • largest_layer: the largest layer descriptor (digest, bytes, media_type), or null for an image without layers.
  • layers: each layer’s digest, bytes, media_type, compression (gzip, zstd, none for plain tar, or unknown, derived from the media type) and role. Layer media types are not restricted, so reported media_type values (here and in largest_layer) redact declared build-argument values in text and JSON, as port findings do; compression is derived from the original media type. Text output escapes media types containing control or other non-printable characters as JSON string escapes; JSON output relies on standard JSON escaping.
  • contributions: base versus application/dependency bytes, plus config_bytes separately.
  • thresholds: each configured value is an exact decimal string, using Python’s str(Decimal) notation (for example "1", "1.0" or "1E-400"), or null when unset. JSON numeric floats are not used for thresholds: consumers should parse these strings as decimals, not binary floats. Text finding messages use the same exact decimal notation.
  • note: the stored-size accounting caveat.

BuildConfig.validate also stores loaded decimal thresholds as exact strings, so json.dumps(dataclasses.asdict(config)) needs no custom decimal encoder. Restore the JSON object’s constructor fields into BuildConfig (omit fields whose dataclass init is false); threshold parsing preserves their exact value. This internal snapshot representation does not change the TOML contract: unoverridden TOML thresholds must be numbers, never quoted strings.

The validate_image(..., thresholds=...) API accepts None or a mapping with string keys naming the supported thresholds. Empty mappings and known keys with None values leave thresholds unset. Non-mapping inputs, non-string keys and unknown names raise ValueError, including when their values are None. Its redact callable is applied to unknown threshold names in diagnostics before bounded, escaped key formatting, and to reported layer media types; the default leaves values unredacted. base_layer_count requires expected_digest and must be an int (not bool, float or string) from 0 through the number of layers in the selected manifest; anything else raises ValueError.

These are stored blob sizes. Base layers pulled from a registry usually keep their compressed media types, so they count compressed bytes, while pycontainer writes dependency and application layers as uncompressed tar, so they count their full size. Network transfer, registry deduplication and cache reuse may differ from these numbers.

An OCI layout does not record which layers came from the base image, and media types or labels are not proof. validate image therefore reports the contribution status as unknown, each layer’s role as unknown, and the base and application byte counts as null. build --validate knows how many base layers it placed first in the manifest it just wrote; after confirming the layout still holds that exact manifest digest, it reports status: determined, base and application roles, and both byte totals. Text output prints the same summary and note. Multi-platform builds report and check sizes separately for each platform’s manifest.

Configured exposed-ports must be distinct integers from 1 to 65535. build and validate config reject duplicates and out-of-range values with exit 2 before the base image is pulled or any layout blob is written. Image port keys and explicit listener ports accept ASCII decimal leading zeros; normalization and range checks apply before comparison. Integer conversion is bounded to five significant digits, even for long inputs.

The listener check reads only the image’s Entrypoint and Cmd argv. It recognizes uvicorn (--port), gunicorn (every -b/--bind; unix: binds are ignored when mixed with TCP binds; Unix-only binds or file descriptors make the TCP listener unknown), flask run (--port/-p), Django runserver [addr:]port, streamlit run (--server.port) and Jupyter lab, notebook and server (--port, --ServerApp.port, --NotebookApp.port), directly or through python -m (including python -m django). Gradio apps launched as python app.py, Celery workers and other commands produce PCB010 when ports are exposed. Django options other than the standard runserver options, before or after the address, also make the listener unknown, as do attached or clustered short options (-p8000, -Rb :8000) and abbreviated long options (--bin), whose effect is parser-specific. An unmodeled option followed by an option-looking value is also ambiguous: for example, Flask can consume --port=9000 as the value of --extra-files. These cases produce non-failing PCB010 rather than a guessed port mismatch.

This check does not guess. Ports set through environment variables (UVICORN_PORT, FLASK_RUN_PORT, STREAMLIT_SERVER_PORT, GRADIO_SERVER_PORT), in application source (for example Gradio launch(server_port=...)), by a shell wrapper or by runtime command overrides are not inferred, and the image config does not record a separately resolved listener port. ExposedPorts is metadata only and does not prove what the process binds. Named users are not resolved against /etc/passwd for PCB011; non-numeric users other than root are treated as non-root.

Before reporting any finding, validation walks every reachable nested index, manifest, config and layer, including subjects and images not selected for linting. It checks each descriptor’s SHA-256 digest and size, manifest media-type consistency, and config platform and rootfs.diff_ids structure. Missing, corrupt, malformed or symlinked layout files and blobs are errors.

Metadata is limited to 16 MiB per file and 64 MiB total for referenced metadata; indexes and images are limited to 1,000 entries and layers respectively, with 100,000 descriptors, 32 nested descriptor levels and 64 GiB per layer. Layer blobs are hashed without decompression, including layers with unrecognized media types. This does not check uncompressed DiffID hashes, layer contents, registry provenance, runtime import or container startup. A passing result is not proof that the image is runnable or its base is verified.