docs/current_docs/introduction/core-concepts/functions.mdx
Functions are the building blocks of Dagger workflows. They're units of computation that accept inputs, perform operations (typically in containers), and return outputs. Functions can be combined together to create complex workflows.
A Dagger Function is code you write that:
Functions run in isolated, reproducible environments and can be composed together to build workflows of any complexity.
If you're using toolchains, you don't need to write functions—you're calling functions created by others:
dagger call go build # Calls the 'build' function from your toolchain
dagger call go test # Calls the 'test' function
When you create or customize toolchains, you write functions:
// A function that builds your application
func (m *MyModule) Build() *Container {
return dag.Container().
From("golang:1.21").
WithDirectory("/src", m.Source).
WithWorkdir("/src").
WithExec([]string{"go", "build", "-o", "app"})
}
Transform source code into artifacts:
func (m *MyModule) Build() *Container {
return dag.Container().
From("node:20").
WithDirectory("/app", m.Source).
WithWorkdir("/app").
WithExec([]string{"npm", "install"}).
WithExec([]string{"npm", "run", "build"})
}
Execute tests and return results:
func (m *MyModule) Test(ctx context.Context) (string, error) {
return dag.Container().
From("python:3.11").
WithDirectory("/app", m.Source).
WithWorkdir("/app").
WithExec([]string{"pytest", "--verbose"}).
Stdout(ctx)
}
Run generation tools and return a Changeset, which will be applied to the source directory:
func (m *MyModule) Generate() *Changeset {
generated := dag.Container().
From("golang:1.21").
WithDirectory("/app", m.Source).
WithWorkdir("/app").
WithExec([]string{"go", "generate"}).
Directory("/app")
return generated.Changes(m.Source)
}
When you install a toolchain, you get access to all its functions:
# Install a toolchain
dagger toolchain install github.com/dagger/jest
# See available functions
dagger functions
# Call any function
dagger call jest test
dagger call jest list
dagger check jest:*
The toolchain author wrote the functions, and you use them without writing any code yourself.