Skip to content

Caching#

Coopr reuses instruction results automatically, including work inside components. Share those results through a registry or a portable local directory. Use RUN cache mounts separately to keep disposable tool data between builds.

Reuse local results#

From a repository checkout, build the hello-world example twice:

coopr build examples/first-image/image.coopr --tag hello:cached
coopr build examples/first-image/image.coopr --tag hello:cached

The second build reports Using cache for reused checkpoints. Coopr matches operation definitions, their dependencies, arguments, platform, execution settings, and declared sources. Changed inputs can invalidate a result and the instructions that depend on it.

Local instruction snapshots live in the native image store. They work with the default --layers=true; --layers=false reruns filesystem instructions instead of reading those snapshots. See layer and cache behavior.

Local COPY/ADD can still read and extract files on a cache hit. Keep source files stable while building. Live host-directory binds, including native --volume settings, force their RUNs to execute even when read-only. Use contexts, images or artifacts for reusable inputs.

Share results through a registry#

Use a cache repository that your machines or CI jobs can access. Replace registry.example.com/team/coopr-cache below and authenticate with coopr login when required:

coopr build examples/first-image/image.coopr --tag hello:cached \
  --cache-from registry.example.com/team/coopr-cache \
  --cache-to registry.example.com/team/coopr-cache

The same options apply when packaging a component:

coopr component build examples/reusable-components/components/settings/component.coopr \
  --cache-from registry.example.com/team/coopr-cache \
  --cache-to registry.example.com/team/coopr-cache

Both commands run from the repository checkout. --cache-from reads results and --cache-to writes them; supply both to use a repository in both directions. Each option is repeatable. Registry values name repositories without tags or digests.

Use a portable local cache#

Write an OCI cache directory that you can retain between CI jobs or move to another machine:

coopr build examples/first-image/image.coopr --tag hello:cached \
  --cache-from type=local,src=.coopr-cache \
  --cache-to type=local,dest=.coopr-cache

Paths are relative to the command’s working directory. Coopr creates the directory if it does not exist. Read and write destinations can differ: for example, read a registry cache and write a local cache. The contents use Coopr’s cache format and cannot be exchanged with BuildKit caches.

What is reused#

Portable caches store eligible instruction snapshots, component outputs, and package results. Restored components retain their selected image’s layers, configuration, and root metadata. RUN cache-mount contents remain local and are not uploaded by --cache-to or restored by --cache-from.

Portable instruction results include device-backed RUNs and remote ADD. Coopr checks HTTP source contents and resolves Git refs before reusing an unpinned ADD result. Dynamic bases and nested components still resolve their dependencies while reusing individual instructions. See cache scopes for whole-component, package and layer-group behavior.

Trust cache repositories as you would other build inputs. Intermediate results may contain files removed from the final image.

Misses and failures#

Missing or invalid cache entries cause a rebuild. Coopr warns about invalid entries; if imported content is corrupt, it also skips publishing the affected cache and leaves its existing tag unchanged. A build can therefore succeed without refreshing its cache. Cache export and destination-write failures still fail the command.

Refresh deliberately#

Force instructions to execute again, or limit how old a reusable result may be:

coopr build examples/first-image/image.coopr --no-cache
coopr build examples/first-image/image.coopr --cache-ttl 24h

--no-cache forces instruction execution and saves fresh results. It does not clear cache mounts or refresh a base image; use --pull to refresh registry image inputs. --cache-ttl limits reuse by cache-record age, not image timestamps. --cache-ttl 0 disables result reuse while saving fresh results.

Stored component packages are immutable artifact inputs, not cache entries. Neither flag rebuilds those packages or supplies packages missing from the component. Rebuild the component when its packaged inputs need to change.

Network responses, credential values, cache-mount contents, and live device contents do not invalidate cached RUN results. Changing a secret or SSH mount declaration can invalidate a result; changing its contents does not. Use --no-cache or change a consumed build argument when these inputs must force a rebuild. A cache hit is not a reproducibility guarantee, and required entitlements are checked even on a hit.

Writable primary-context bind mounts execute each time so their context changes remain visible to later instructions. Components using caller-context binds also bypass whole-component reuse. See RUN mounts for context lifetime and measurement.

Sources may still need resolution before a cache lookup, so a hit does not guarantee an offline build. Output compression controls the final image export, not cached instruction snapshots.

Cache mounts#

Cache mounts help when a RUN must execute again: a package manager can reuse downloads even when its instruction result no longer matches.

Save this definition as image.coopr in a new directory:

from "docker.io/redhat/ubi9:latest" {
    // Keep DNF downloads outside the image's filesystem layers.
    run {
        script """
            dnf install -y jq \\
                --setopt=cachedir=/var/cache/dnf \\
                --setopt=keepcache=True
            """
        mount "cache" target="/var/cache/dnf" id="ubi9-dnf" sharing="locked"
    }

    cmd {
        exec "jq" "--version"
    }
}

Build and run it from that directory:

coopr build image.coopr --tag tools:cached
podman run --rm localhost/tools:cached

DNF installs jq into the image while its repository metadata and downloaded RPMs stay in the mount. keepcache=True retains those RPMs after installation. A later RUN can reuse this data even with --no-cache; a new machine starts with an empty mount.

The explicit id shares tool data with other mounts using that ID. Omit it for an automatically scoped name that can change with the definition, platform, or component arguments. sharing="locked" serializes access; shared permits concurrent access. The bundled Buildah backend does not support sharing="private"; use distinct cache IDs when callers need separate caches.

Mount data is disposable: the build must work when it is empty. See mount options and cache cleanup.