ocibuild_layout (ocibuild v0.10.4)

View Source

OCI image layout handling.

Produces OCI Image Layout format for use with podman load, skopeo, or direct filesystem storage.

The layout structure is:

myimage/
 oci-layout           # {"imageLayoutVersion": "1.0.0"}
 index.json           # Entry point
 blobs/
     sha256/
         <manifest>   # Manifest JSON
         <config>     # Config JSON
         <layers...>  # Layer tarballs (gzip compressed)

See: https://github.com/opencontainers/image-spec/blob/main/image-layout.md

Summary

Functions

Export image as an OCI layout directory.

Load an OCI image tarball for pushing to a registry.

Load an OCI image tarball with options.

Parallel map with bounded concurrency.

Save image as an OCI layout tarball.

Types

loaded_image()

-type loaded_image() ::
          #{manifest := binary(),
            manifest_digest := binary(),
            config := binary(),
            config_digest := binary(),
            layers :=
                [#{digest := binary(),
                   size := non_neg_integer(),
                   get_data := fun(() -> {ok, binary()} | {error, term()})}],
            platform := ocibuild:platform() | undefined,
            annotations := map()}.

Functions

build_config_blob/1

-spec build_config_blob(ocibuild:image()) -> {binary(), binary()}.

build_layer_descriptors/1

-spec build_layer_descriptors(ocibuild:image()) -> [ocibuild_manifest:descriptor()].

export_directory(Image, Path)

-spec export_directory(ocibuild:image(), file:filename()) -> ok | {error, term()}.

Export image as an OCI layout directory.

Creates the standard OCI directory structure at the given path.

ok = ocibuild_layout:export_directory(Image, "./myimage").

load_tarball_for_push(Path)

-spec load_tarball_for_push(file:filename()) ->
                               {ok,
                                #{images := [loaded_image()],
                                  tag := binary() | undefined,
                                  is_multi_platform := boolean(),
                                  cleanup := fun(() -> ok)}} |
                               {error, term()}.

Load an OCI image tarball for pushing to a registry.

Parses the OCI Image Layout tarball and extracts all necessary data for pushing: manifests, configs, and layer blobs.

Supports both single-image and multi-platform tarballs. The tag is extracted from index.json annotations if present.

Uses hybrid memory/disk loading: small images (<100MB) are loaded into memory, larger images are extracted to a temp directory.

{ok, Result} = ocibuild_layout:load_tarball_for_push("myimage.tar.gz"),
%% Result = #{
%%     images := [loaded_image()],
%%     tag := binary() | undefined,
%%     is_multi_platform := boolean(),
%%     cleanup := fun(() -> ok)
%% }

load_tarball_for_push(Path, Opts)

-spec load_tarball_for_push(file:filename(), #{memory_threshold => pos_integer()}) ->
                               {ok,
                                #{images := [loaded_image()],
                                  tag := binary() | undefined,
                                  is_multi_platform := boolean(),
                                  cleanup := fun(() -> ok)}} |
                               {error, term()}.

Load an OCI image tarball with options.

Options:

  • memory_threshold: File size threshold for in-memory loading (default: 100MB). Files smaller than this are loaded entirely into memory for speed. Larger files are extracted to a temp directory for memory efficiency.

pmap_bounded/3

-spec pmap_bounded(fun((A) -> B), [A], pos_integer()) -> [B] when A :: term(), B :: term().

Parallel map with bounded concurrency.

Executes Fun on each element of List in parallel, with at most MaxWorkers concurrent executions. Results are returned in the same order as the input list.

Example:

Results = pmap_bounded(fun(X) -> X * 2 end, [1, 2, 3, 4, 5], 2).
%% Returns [2, 4, 6, 8, 10] with max 2 concurrent workers

Throws if any worker fails with the original error.

save_tarball(Image, Path)

-spec save_tarball(ocibuild:image(), file:filename()) -> ok | {error, term()}.

Save image as an OCI layout tarball.

Creates a tar.gz file that can be loaded with podman load or other OCI-compliant tools.

ok = ocibuild_layout:save_tarball(Image, "./myimage.tar.gz").
ok = ocibuild_layout:save_tarball(Image, "./myimage.tar.gz", #{tag => ~"myapp:1.0"}).

Options:

  • tag: Image tag annotation (e.g., ~"myapp:1.0")

Note: OCI layout works with podman, skopeo, crane, buildah, and other OCI tools.

save_tarball/3

-spec save_tarball(ocibuild:image() | [ocibuild:image()], file:filename(), map()) ->
                      ok | {error, term()}.