Skip to main content

Dependencies

When creating a Dagger module, there are two types of dependencies you will encounter:

  • Dependencies on other Dagger modules
  • Dependencies on third-party packages or modules from your programming language's ecosystem

Dependencies on Dagger modules

Dagger modules are shareable and reusable. You can call Dagger Functions from any Dagger module in your own Dagger module simply by adding it as a module dependency with dagger install, as in the following example:

dagger install github.com/shykes/daggerverse/hello@v0.3.0

This module will be added to your dagger.json:

...
"dependencies": [
{
"name": "hello",
"source": "github.com/shykes/daggerverse/hello@54d86c6002d954167796e41886a47c47d95a626d"
}
]

When you add a dependency to your module with dagger install, the dependent module will be added to the code-generation routines and can be accessed from your own module's code.

The entrypoint to accessing dependent modules from your own module's code is dag, the Dagger client, which is pre-initialized. It contains all the core types (like Container, Directory, etc.), as well as bindings to any dependencies your module has declared.

Here is an example of accessing the installed hello module from your own module's code:

func (m *MyModule) Greeting(ctx context.Context) (string, error) {
return dag.Hello().Hello(ctx)
}

You can also use local modules as dependencies. However, they must be stored in a sub-directory of your module. For example:

dagger install ./path/to/module

Dependencies on third-party packages

Dagger Functions are just regular code, written in your usual programming language. One of the key advantages of this approach is that it opens up access to your language's existing ecosystem of packages or modules. You can easily add and use these packages/modules as dependencies in your Dagger module via your language's package manager.

To add a Go dependency, add it to your go.mod file using go get. For example:

go get github.com/spf13/cobra