Skip to main content

Function Arguments

Dagger Functions, just like regular functions, can accept arguments. In addition to basic types (string, boolean, integer, array...), Dagger also defines powerful core types which functions can use for their arguments: Directory, File, Container, Service, and Secret.

String arguments

To pass a string argument to a Dagger Function, add the corresponding flag to the dagger call command, followed by the string value.

Here is an example of passing two string arguments to a "hello world" Dagger Function. The Dagger Function uses the arguments to adjust its output message accordingly:

dagger -m github.com/shykes/daggerverse/hello@v0.1.2 call hello --greeting=bonjour --name=daggernaut

Boolean arguments

To pass a Boolean argument to a Dagger Function, simply add the corresponding flag, like so:

  • To set the argument to true: --foo=true, or simply --foo
  • To set the argument to false: --foo=false

Here is an example of passing a a Boolean argument to the same "hello world" Dagger Function. The Boolean argument controls the size ("giant" or normal) of the output message returned by the Dagger Function:

dagger -m github.com/shykes/daggerverse/hello@v0.1.2 call hello --giant

Directory arguments

You can also pass a directory argument from the command-line. To do so, add the corresponding flag, followed by a local filesystem path or a remote Git reference. In both cases, the CLI will convert it to an object referencing the contents of that filesystem path or Git repository location, and pass the resulting Directory object as argument to the Dagger Function.

Here is an example of passing a local filesystem path to a Dagger Function as argument. The Dagger Function returns a tree representation of the files and directories at that path:

mkdir -p mydir/mysubdir
touch mydir/a mydir/b mydir/c mydir/mysubdir/y mydir/mysubdir/z
dagger -m github.com/vikram-dagger/daggerverse/fileutils@v0.1.2 call tree --dir=mydir

Here is an example of passing a GitHub repository reference - the main branch of the Dagger repository - to the same Dagger Function as an argument, instead of a local filesystem path:

dagger -m github.com/vikram-dagger/daggerverse/fileutils@v0.1.2 call tree '--dir=https://github.com/dagger/dagger#main:cmd/dagger'

Container arguments

Just like directories, you can pass a container argument to a Dagger Function from the command-line. To do so, add the corresponding flag, followed by the address of an OCI image. The CLI will dynamically pull the image, and pass the resulting Container object as argument to the Dagger Function.

Here is an example of passing a container image reference to a Dagger Function as an argument. The Dagger Function scans the container using Trivy and reports any vulnerabilities found.

dagger -m github.com/jpadams/daggerverse/trivy@v0.3.0 call scan-container --ctr=alpine:latest
tip

It is important to know that in Dagger, a Container object is not merely a string referencing an image on a remote registry. It is the actual state of a container, managed by the Dagger Engine, and passed to a Dagger Function's code as if it were just another variable. This feature, as far as we know, is unique to Dagger, and opens endless possibilities for assembling complex pipelines where container state flows from Dagger Function to Dagger Function - in just a few lines of code.

Secret arguments

Dagger allows you to utilize confidential information ("secrets") such as passwords, API keys, SSH keys and so on, in your Dagger Functions, without exposing those secrets in plaintext logs, writing them into the filesystem of containers you're building, or inserting them into the cache.

To pass a secret to a Dagger Function, source it from a host environment variable (env:), the host filesystem (file:), or a host command (cmd:).

Here is an example of passing a GitHub access token from an environment variable named GITHUB_TOKEN to a Dagger Function. The Dagger Function uses the token to query the GitHub CLI for a list of issues in the Dagger open-source repository:

dagger -m github.com/aweris/daggerverse/gh@99a1336f8091ff43bf833778a324de1cadcf25ac call run --token=env:GITHUB_TOKEN --cmd="issue list --repo=dagger/dagger"

Here is an example of passing a secret from a host file named github-token.txt:

dagger -m github.com/aweris/daggerverse/gh@99a1336f8091ff43bf833778a324de1cadcf25ac call run --token=file:/home/admin/github-token.txt --cmd="issue list --repo=dagger/dagger"

And here is an example of passing a secret from a command executed on the host:

dagger -m github.com/aweris/daggerverse/gh@99a1336f8091ff43bf833778a324de1cadcf25ac call run --token=cmd:"gh auth token" --cmd="issue list --repo=dagger/dagger"
tip

To list all the arguments accepted by a function, add the --help suffix at any point in the dagger call command to obtain context-sensitive help. For example, to list the arguments available for the Build() function of the golang module, use:

dagger -m github.com/kpenfound/dagger-modules/golang@v0.1.8 call build --help