Skip to main content

Decorators

A Dagger Module's functions are declared as methods of a class (decorated with @object()) having the same name as the module (in PascalCase).

All objects decorated with @object(), functions decorated with @func() and fields decorated with field() with a decorator are exposed in the Dagger API.

Here is an example of a module that exposes a function alpine that returns an object Alpine with the method echo and the field ctr:

import { dag, Container, object, func, field } from "@dagger.io/dagger"

@object()
class Alpine {
@field()
ctr: Container

constructor(version = "3.14") {
this.ctr = dag.container().from(`alpine:${version}`)
}

@func()
async echo(msg: string[]): Promise<string> {
return this.ctr.withExec(["echo", ...msg]).stdout()
}
}

@object()
class MyModule {
@func()
alpine(version?: string): Alpine {
return new Alpine(version)
}
}

And here is an example of a call to this module:

dagger call alpine echo --msg="hello","world"

The result will be:

hello world

An alternative is to directly query the field ctr and chain additional function calls to it:

dagger call alpine ctr with-exec --args "echo","hello","world" stdout

This will output the same result as before:

hello world