Functions
Dagger Functions are regular code, written in a supported programming language, and running in containers. Dagger Functions let you encapsulate common operations or workflows into discrete units with clear inputs and outputs.
Here's an example of a simple Dagger Function:
package main
import (
"context"
)
type MyModule struct{}
func (m *MyModule) Hello(ctx context.Context) (string, error) {
return "Hello, world", nil
}
Here is an example call for this Dagger Function:
dagger call hello
The result will be:
Hello, world
Here's an example of a more complex Dagger Function, which calls a remote API method:
package main
import (
"context"
)
type MyModule struct{}
func (m *MyModule) GetUser(ctx context.Context) (string, error) {
return dag.Container().
From("alpine:latest").
WithExec([]string{"apk", "add", "curl"}).
WithExec([]string{"apk", "add", "jq"}).
WithExec([]string{"sh", "-c", "curl https://randomuser.me/api/ | jq .results[0].name"}).
Stdout(ctx)
}
Here is an example call for this Dagger Function:
dagger call get-user
The result will look something like this:
{
"title": "Mrs",
"first": "Beatrice",
"last": "Lavigne"
}
Arguments
Dagger Functions, just like regular functions, can accept arguments. In addition to basic types (string, boolean, integer, arrays...), Dagger also defines powerful core types which functions can use for their arguments, such as Directory
, Container
, Service
, Secret
, and many more.
Here's an example of modifying the previous Dagger Function to accept a string argument:
package main
import (
"context"
"fmt"
)
type MyModule struct{}
func (m *MyModule) GetUser(ctx context.Context, gender string) (string, error) {
return dag.Container().
From("alpine:latest").
WithExec([]string{"apk", "add", "curl"}).
WithExec([]string{"apk", "add", "jq"}).
WithExec([]string{"sh", "-c", fmt.Sprintf("curl https://randomuser.me/api/?gender=%s | jq .results[0].name", gender)}).
Stdout(ctx)
}
Here is an example call for this Dagger Function:
dagger call get-user --gender=male
The result will look something like this:
{
"title": "Mr",
"first": "Hans-Werner",
"last": "Thielen"
}
The context and error return are optional in the module's function signature; remove them if you don't need them.
Here's an example of a Dagger Function that accepts a Directory
as argument:
package main
import (
"context"
)
type MyModule struct{}
func (m *MyModule) Tree(ctx context.Context, dir *Directory, depth string) (string, error) {
return dag.Container().
From("alpine:latest").
WithMountedDirectory("/mnt", dir).
WithWorkdir("/mnt").
WithExec([]string{"apk", "add", "tree"}).
WithExec([]string{"tree", "-L", depth}).
Stdout(ctx)
}
Here is an example call for this Dagger Function:
dagger call tree --dir=. --depth=1
The result will look like this:
.
├── LICENSE
├── dagger
└── dagger.json
2 directories, 2 files
You can also pass a remote Git reference, and the Dagger CLI will convert it to a Directory
referencing the contents of that repository. Here is an example call that lists the source code for the Dagger CLI, from the main
branch of the Dagger GitHub repository:
dagger call tree --dir=https://github.com/dagger/dagger#main:cmd/dagger --depth=1
The result will be the same file listing as this GitHub page:
.
├── call.go
├── cloud.go
├── debug.go
├── engine.go
├── exec_nonunix.go
├── exec_unix.go
├── flags.go
├── functions.go
├── gen.go
├── licenses.go
├── listen.go
├── log.go
├── main.go
├── module.go
├── module_test.go
├── query.go
├── run.go
├── session.go
├── shell.go
└── version.go
1 directory, 20 files
When calling a Dagger Function from the CLI, its arguments are exposed as command-line flags. How the flag is interpreted depends on the argument type.
Optional arguments
Function arguments can be marked as optional. In this case, the Dagger CLI will not display an error if the argument is omitted in the function call.
Here's an example of a Dagger Function with an optional argument:
package main
import (
"context"
"fmt"
)
type MyModule struct{}
func (m *MyModule) Hello(
ctx context.Context,
// +optional
name string,
) (string, error) {
if name != "" {
return fmt.Sprintf("Hello, %s", name), nil
} else {
return "Hello, world", nil
}
}
Here is an example call for this Dagger Function, with the optional argument:
dagger call hello --name=John
The result will look like this:
Hello, John
Here is an example call for this Dagger Function, without the optional argument:
dagger call hello
The result will look like this:
Hello, world
Default arguments
Function arguments can be configured to use default values if no value is supplied for them.
Here's an example of a Dagger Function with a default value for an argument:
package main
import (
"context"
"fmt"
)
type MyModule struct{}
func (m *MyModule) Hello(
ctx context.Context,
// +optional
// +default="world"
name string,
) (string, error) {
return fmt.Sprintf("Hello, %s", name), nil
}
Here is an example call for this Dagger Function, without the required argument:
dagger call hello
The result will look like this:
Hello, world
Return values
Not only can Dagger Functions use Dagger's core types in their arguments; they can use them in their return value as well.
This opens powerful applications to Dagger Functions. For example, a Dagger Function that builds binaries could take a directory as argument (the source code) and return another directory (containing binaries) or a container image (with the binaries included).
Here's an example of a Dagger Function that accepts a Directory
containing a Go application's source code as input, compiles it into a binary, and returns a Container
with the binary:
package main
import (
"context"
)
type MyModule struct{}
func (m *MyModule) Build(ctx context.Context, source *Directory, architecture string, os string) *Container {
dir := dag.Container().
From("golang:1.21").
WithMountedDirectory("/src", source).
WithWorkdir("/src").
WithEnvVariable("GOARCH", architecture).
WithEnvVariable("GOOS", os).
WithEnvVariable("CGO_ENABLED", "0").
WithExec([]string{"go", "build", "-o", "build/"}).
Directory("/src/build")
return dag.Container().
From("alpine:latest").
WithDirectory("/usr/local/bin", dir)
}
Here is an example call for this Dagger Function:
dagger call build --source=https://github.com/golang/example/#master:hello --os=linux --architecture=amd64 terminal
This example chains two functions calls:
- a call to
Build()
, which builds a Go application from a remote GitHub repository and returns aContainer
with the compiled binary; - a call to
Terminal()
, which opens an interactive terminal session with theContainer
returned by the previous function.
The result will be an interactive terminal session with the built container, which you can use to validate the compiled Go binary:
/ # cd /usr/local/bin
/usr/local/bin # ls
hello
/usr/local/bin # ./hello
Hello, world!
/usr/local/bin #