OCI and Docker image manipulation library and CLI
README.md

oci #

OCI and Docker images for OCaml: a library and an oci command-line tool.

Container images are the common way to ship software, and the OCI image and
distribution specifications define both the format and the registry protocol
that moves it. Most tooling for them is written in Go (skopeo, crane,
umoci), so OCaml code that needs to pull, unpack, build or serve an
image usually shells out. oci implements the OCI Image Format Specification
v1.1.1 and the OCI Distribution Specification v1.1.1 in OCaml, and reads the
Docker v2 manifest formats as well.

An image is a set of blobs, each named by its digest: sha256: followed by
the hex SHA-256 of the blob's bytes. The manifest is a JSON blob that lists
the image's config blob and its layer blobs by digest and size; the config
records the platform and the run settings; each layer is a tar archive of
files. A registry is an HTTP server that stores blobs and manifests and maps
tags, names such as latest, to manifest digests. A runtime bundle is a
directory holding a config.json and the root filesystem it names
(rootfs), the input from which runc starts a container. A referrer is a
manifest whose subject field names another manifest's digest, so a
signature or an attestation attaches to an image without changing it; a
registry lists them at GET /v2/<name>/referrers/<digest>.

The package holds several libraries:

  • oci pulls images from a registry into a local, content-addressed cache,
    pushes them back, pins a tag to a digest, and unpacks a cached image into a
    runtime bundle (the layout umoci unpack produces and runc consumes). It
    also reads registry credentials from Docker's and Podman's auth files, and
    attaches and discovers signatures and attestations as OCI 1.1 referrers,
    falling back to the referrers tag schema on a registry without the
    referrers API. A signature is laid out as the Notary Project signature
    specification v1.1.0 lays it out. The cache stores each blob under
    blobs/sha256/<hex>, so a blob is fetched once, and re-hashes it whenever
    it reads it back.
  • oci.notary signs the Notary Project COSE signature envelope and verifies
    the COSE and JWS envelopes notation writes, against a set of trust
    anchors.
  • oci-layer.spec holds the image specification's types: descriptors,
    manifests, indexes, configs, platforms and digests.
  • oci-layer builds layers and images through a store, and reads and writes
    OCI image layouts on disk.
  • oci-layer.server is a registry server that depends on no HTTP library,
    with a small HTTP/1.1 front end; oci.proxy turns it into a pull-through
    mirror.

Extraction is written for untrusted images. It refuses absolute paths, ..,
writes through planted symlinks and device nodes, re-hashes every blob as it
streams out of the cache, and bounds the bytes and entries of each layer and
of the whole image.

Installation #

This package is not yet in the public opam-repository. Add the overlay
repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install oci

Usage #

Fetching an image #

$ oci fetch IMAGE_NAME[:TAG]

fetch downloads the manifest and layers of the image into the local cache
(under the XDG cache directory unless --cache names another). The tag
defaults to latest. A manifest is committed to the cache only after every
blob it references is stored, so an interrupted pull leaves no half-cached
image. oci list shows what the cache holds, oci show IMAGE prints one
image's manifest and oci inspect IMAGE its inspection record.

Registry credentials #

Oci.Auth_file reads the credentials that docker login and podman login
already stored, so a caller does not need a --username and --password
pair of its own. Oci.Auth_file.load finds Docker's
$DOCKER_CONFIG/config.json (~/.docker/config.json by default) or the
$XDG_RUNTIME_DIR/containers/auth.json file of Podman and skopeo.
Oci.Auth_file.resolve turns a registry name into the username and password
that Oci.fetch and Oci.push take, running the file's
docker-credential-* helper when it names one. docker.io,
index.docker.io and registry-1.docker.io count as the same Docker Hub
registry, as they do for docker login.

Unpacking an image #

$ oci checkout IMAGE [--checkout-directory DIR]

checkout unpacks a cached image into a runtime bundle under the current
directory, or under DIR, with the merged root filesystem in its rootfs
directory. Layers are applied in order and OCI whiteouts delete what lower
layers wrote. A whiteout is an empty file named .wh.<name> in a layer:
applied, it deletes the <name> a lower layer put beside it, and the special
.wh..wh..opq marks its directory opaque, hiding everything lower layers put
there. Run as root, it restores each file's owner; run as a plain user, every
file and directory is that user's. It always keeps the exact mode, including
the setuid, setgid and sticky bits, whatever the umask. An image's several
owners are not kept by a plain user's checkout.

$ oci checkout IMAGE --release NAME [--checkout-directory DIR]

With --release, the directory is a store of releases. Each layer is
unpacked once, as the changeset it publishes, under layers/, and the image
is written at releases/NAME as a tree of hard links to the layer files, with
the whiteouts applied. Two releases that share a layer share its files, so a
new release whose only change is one layer costs that layer and a link per
file. The trees share inodes: they are read-only. A release appears only once
it is whole, and an existing name is refused.

Inspecting an image #

$ oci inspect IMAGE [--platform OS/ARCH]

inspect prints a cached image's inspection record without unpacking a
layer: its image ID, the digest of its manifest, its platform, its user, its
labels and its layers. The image ID is the digest of the config blob, as the
image specification defines it (config.md, "ImageID"): two manifests that
carry the same config, such as the same layers compressed two ways, have one
image ID. Oci.Checkout.inspect returns the same record as an
Oci.Inspect.t.

Importing a root filesystem tarball #

$ oci import core-image-minimal.rootfs.tar.gz \
>   -n registry.example.org/cm5/rootfs:latest \
>   --platform linux/arm64 \
>   --annotation org.opencontainers.image.title="CM5 rootfs"

import wraps an existing root filesystem tarball, such as a Yocto build
output, as a single-layer OCI image in the local cache, ready for push or
for uniboot to consume as a
pinned component. The tarball's bytes are stored verbatim; tar, tar.gz and
tar.zst inputs are recognised by their magic numbers. --add Image --add board.dtb packs loose files, such as a kernel and its devicetrees, into an
extra layer, and --layout DIR also writes the image as an OCI image layout
that skopeo and crane can read. oci cache-layout goes the other way and
registers an existing image layout in the cache.

Pushing and serving #

oci push FILE -n IMAGE:TAG --artifact-type TYPE pushes a file to a registry
as an OCI artifact, laid out as the image-spec's "Guidelines for Artifact
Usage" describe an artifact without config: the empty descriptor as config,
TYPE as the manifest's artifactType, and the file as its one layer under
--media-type (application/octet-stream by default), titled with the file's
name in org.opencontainers.image.title as ORAS does.
oci serve --root DIR runs a registry over a directory on port 5000; it is
read-only unless --token sets a bearer token for writes, and it refuses
request bodies larger than --max-body-size (64 MiB by default) before
reading them.

Specifications #

The package carries the OCI Image Format Specification v1.1.1 and the OCI
Distribution Specification v1.1.1 verbatim under spec/, with their
Apache-2.0 licences. spec/SOURCES.md records where each file was fetched
from and its SHA-256.

Licence #

ISC. See LICENSE.md.