Skip to main content

Function Chaining into Pipelines

Dagger Functions can return either basic types or objects. Objects can define their own functions (in the classic object-oriented model). When calling a Dagger Function that returns an object, the Dagger API lets you follow up by calling one of that object's functions, which itself can return another object, and so on. This is called "function chaining", and is a core feature of Dagger.

Dagger's core types (Container, Directory, Service, Secret, ...) are all objects. They each define various functions for interacting with their respective objects.

When a Dagger Function returns a core type - for example, a directory - the caller typically continues the chain by calling a function from that type - for example, to export it to the local filesystem, modify it, mount it into a container, etc.

Here are a few examples of function chaining in action:

  • List the contents of the build directory returned by a Dagger Function, by chaining a call to the Directory object's Entries() function:

    dagger -m github.com/kpenfound/dagger-modules/golang@v0.1.5 call build --project=https://github.com/dagger/dagger --args=./cmd/dagger entries
  • Print the contents of a file returned by a Dagger Function, by chaining a call to the File object's Contents() function:

    dagger -m github.com/dagger/dagger/linters/markdown@88d89e8d15ab6ad9ca4043a920d3cd735a6405fd call rules contents
  • Publish a container image of a container returned by a Dagger Function, by chaining a call to the Container object's Publish() function:

    dagger call -m github.com/shykes/daggerverse/wolfi@v0.1.2 container publish --address=ttl.sh/my-wolfi
  • Add labels to a container image before publishing it, by chaining additional calls to the Container object's WithLabel() function:

    dagger -m github.com/shykes/daggerverse/wolfi@v0.1.2 call container --packages=curl with-label --name=version --value=1.0 with-label --name=vendor --value="Unknown Corp" publish --address=ttl.sh/my-wolfi
  • Display and return the contents of the /etc/os-release file in a container, by chaining calls to the Container object's WithExec() and Stdout() functions:

    dagger -m github.com/shykes/daggerverse/wolfi@v0.1.2 call container with-exec --args="cat","/etc/os-release" stdout
  • Expose a service returned by a Dagger Function on a specified host port, by chaining a call to the Service object's Up() function:

    dagger -m github.com/levlaz/daggerverse/mariadb@b56073942f5fa65c42a63a524fc9276f7f040f2f call serve up --ports=9000:3306