untainer
Get started

Writing a plugin

Write an untainer plugin with definePlugin(): resolvers for references, a chunk store, build hooks and runtime hooks.

A plugin is an object with a name and any of: resolvers for references, a chunk store, build hooks and runtime hooks. definePlugin() from untainer/kit gives it its type.

import { definePlugin } from 'untainer/kit';

export default definePlugin({
  name: 'my-plugin',
  resolvers: [/* ImageResolver */],
  store: /* ChunkStore */ undefined,
  build: {
    files: ({ name }) => [/* files added on top of the image */],
    config: (config) => config, // change the recorded command, environment, user, ports
    done: (result) => {}, // once the image is written
  },
  runtime: {
    run: (image, options) => options, // change a container's options before it starts
    exit: (container, status) => {}, // once a container has exited
  },
});
const rt = await createRuntime({ plugins: [myPlugin] });
await build('node:24-alpine', { outDir: 'dist/node', plugins: [myPlugin] });

Plugins apply in their order. Their resolvers come after the runtime's own resolvers and before the built-in ones; the last plugin with a store gives the store, unless the runtime is given one.

#Three that run

examples/plugins has three, run by packages/untainer/test/plugins.vm.test.ts:

  • A chunk store (counting-store.ts): any object with get, put and has. The test starts an image twice and checks that the second start read every chunk from the store and none from the network.
  • A resolver (team-resolver.ts): team:<name> becomes <baseUrl><name>/. A resolver returns the base URL of the image's manifest.json, and headers if it needs them.
  • A build hook (build-info.ts): every image built with it carries /etc/build-info.json and BUILD_INFO in its environment.

A store sees chunks as they were fetched (compressed) and must not keep a reference to the bytes it is given. A resolver should refuse what it cannot read with UntainerError('ERR_UNSUPPORTED').

#What a plugin can extend

Four interfaces are a plugin's to give. Each can also be passed directly, without a plugin:

InterfaceIn a pluginDirectly
ImageResolverresolverscreateRuntime({ resolvers }), loadImage(ref, { resolvers })
ChunkStorestorecreateRuntime({ store }), loadImage(ref, { store })
BuildHooksbuild: files, config, doneonly through build(ref, { plugins })
RuntimeHooksruntime: run, exitonly through createRuntime({ plugins })

#What a plugin cannot replace

These are interfaces of the SDK that your code implements or passes itself; no plugin hook reaches them yet.

  • LayerSource: an Image is a manifest and one LayerSource per layer. Use memoryLayer(), chunkedLayer() or your own, and pass the image to rt.run().
  • ChunkSource and ChunkDecoder: what chunkedLayer() fetches and decompresses chunks with. loadImage() always fetches over HTTP; its fetch option replaces fetch.
  • NetBackend: unkernel's interface, given as createRuntime({ net }). policyBackend() wraps any backend.
  • Probe: tcp, http or exec, and nothing else. There is no probe function of your own, but an exec probe runs any command in the container.

Two things are not there at all:

  • An image source for build(): it reads OCI registries only. buildFromLayers() from untainer/build builds from layer tars and an OCI config you got any other way.
  • A volume driver: there are four kinds of volume (tmpfsVolume(), memoryVolume(), hostVolume(), diskVolume()). The runtime mounts host file systems through unkernel's Fs internally, which is not public.

untainer  OCI images as containers in a browser tab or in Node.