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 withget,putandhas. 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'smanifest.json, and headers if it needs them. - A build hook (
build-info.ts): every image built with it carries/etc/build-info.jsonandBUILD_INFOin 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:
| Interface | In a plugin | Directly |
|---|---|---|
ImageResolver | resolvers | createRuntime({ resolvers }), loadImage(ref, { resolvers }) |
ChunkStore | store | createRuntime({ store }), loadImage(ref, { store }) |
BuildHooks | build: files, config, done | only through build(ref, { plugins }) |
RuntimeHooks | runtime: run, exit | only 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: anImageis a manifest and oneLayerSourceper layer. UsememoryLayer(),chunkedLayer()or your own, and pass the image tort.run().ChunkSourceandChunkDecoder: whatchunkedLayer()fetches and decompresses chunks with.loadImage()always fetches over HTTP; itsfetchoption replacesfetch.NetBackend: unkernel's interface, given ascreateRuntime({ net }).policyBackend()wraps any backend.Probe:tcp,httporexec, and nothing else. There is no probe function of your own, but anexecprobe runs any command in the container.
Two things are not there at all:
- An image source for
build(): it reads OCI registries only.buildFromLayers()fromuntainer/buildbuilds 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'sFsinternally, which is not public.