Skip to content

Dagger Scripts: a more convenient way to call Dagger Functions from your project #5774

Closed

Description

@shykes

TLDR: dagger call is very safe, but lacks the convenience of artisanal scripts. There is a way to get both: safety and convenience.

Problem

Now that Dagger Functions are generally available, the standard way to use Dagger from your project is to call functions. Today that is done with dagger call.

dagger call is a fantastic primitive: simple, reliably, and safe. If you understand dagger call, you understand how Dagger Functions work, and therefore - how Dagger as a platform works.

But, this comes at a cost. dagger call is less convenient than the artisanal scripts that it replaces, because functions can't access the caller's environment variables, network interfaces or filesystem: the user has to pass them explicitly. This is safe, but more cumbersome.

Solution

Dagger should introduce first-class support for scripts: small programs that can call functions in the context of the user's system. So unlike functions, scripts can access environment variables and local files.

Dagger Functions Dagger Scripts External clients
Access to user's system None (full sandboxing) Configurable (partial sandboxing) Complete (no sandbox)
No dependency beyond dagger ✅ ✅ ❌
Can be packaged in a module ✅ ✅ ❌
Can be used as a dependency ✅ ❓ ❌

Possible designs

There are several possible designs. They may or may not overlap.

Design A: Bash-compatible shell

In this design, dagger embeds a bash-compatible shell. The user can execute scripts, either interactively, from a file, or from a command-line argument.

Working POC implementation: https://daggerverse.dev/mod/github.com/shykes/daggerverse/dsh

Advantages:

  • No codegen required
  • No containers required. Everything can be built into the CLI binary
  • Extreme familiarity. Everyone knows bash
  • Scripts can be stored directly in dagger.json, similar to package.json
  • The Dagger CLI can be a shell. ie. dagger without argument could simply start the interpreter
  • Easy to sandbox: the library used in the POC has handlers for command execution, env variables, filesystem access.
  • Since the shell is just a convenience wrapper around dagger call, improvements to the shell also benefit dagger call

Disadvantages:

  • Limitations of dagger call also limit the shell. For example, no access to core API (Allow calling core functions from the CLI #6947), no way to compose calls from the CLI, etc.
  • Doesn't address the desire for python or typescript one-liners

Design B: ephemeral modules

In this design, scripts are basically more lightweight modules:

  • They use codegen
  • They probably map to existing SDKs: Python, Typescript, Go...
  • They run in containers
  • They (presumably) rely on the existing host() API for accessing the system

The main difference with regular modules, is that they don't require a full-blown software project, with a directory full of generated files, etc. A script fits in a single file, or even in a command-line argument. Everything else generated from there is ephemeral.

FIXME: pros and cons

Design C: codegen without containers?

This is a placeholder for the possible design described by @sipsma.

My (@shykes) understanding is that it's like design B (ephemeral modules), except that it does not run in containers. Instead, the CLI builds native binaries and executes them directly on the host.

Activity

  1. changed the title [-][Zenith] `dagger script`: invoke any function from any module, in your favorite language, from the CLI[/-] [+]`dagger script`: invoke any function from any module, in your favorite language, from the CLI[/+] on Mar 25, 2024
  2. changed the title [-]`dagger script`: invoke any function from any module, in your favorite language, from the CLI[/-] [+]Dagger Scripts: a more convenient way to call Dagger Functions from your project[/+] on Mar 25, 2024
  3. helderco commented on Mar 26, 2024

    @helderco
    Contributor

    This sounds a lot like dagger run today, rather than the "one-liner" we discussed a few times, based on the direct access to the user's system. Unless you mean through an API?

  4. sipsma commented on Mar 26, 2024

    @sipsma
    Contributor

    Yeah agree reading this sounds more and more like #5993; if you want to write a script that can call out to your functions, you will need codegen bindings to those functions.

    I think if you take that and combine it with the ability for dagger run to compile your code for you rather than require host dependencies (i.e. dagger run main.go rather than dagger run go run main.go) you'd end up with a pretty simple and easy way to write complex logic that executes direct on your host and calls out to functions.

    I've been thinking through other variations too in that issue, but something along these lines feels right. I'm hoping to pick up #5993 in the near future, so should be able to start playing around with ideas soon 🤞

  5. shykes commented on Mar 26, 2024

    @shykes
    ContributorAuthor

    @helderco @sipsma sorry I didn't have time to finish my writeup. It's different from eg. dagger run main.go in a few ways:

  6. shykes commented on Mar 26, 2024

    @shykes
    ContributorAuthor

    @helderco @sipsma update: I added more details to the "Solution" section:

    • Comparison of scripts vs. functions vs. external clients
    • Fleshed out proposal for one possible design: bash-compatible shell
    • EDIT: also fleshed out the "python or typescript one-liner" design

    Next up:

    • Discuss if those two designs are complementary, or overlapping (no idea)
  7. jedevc commented on Apr 30, 2024

    @jedevc
    Contributor

    A couple of thoughts on this, from the context of modularizing our own CI.

    There are a couple of different use cases to be aware of, things that I want to do as part of my workflow with our new ci directory, so we can get rid of mage:

    • Simple aliases. e.g. dagger script docs-generate or similar translates to dagger call --source=. docs generate export --path=.
    • Chaining commands - I want to be able to "pipe" data between different calls - if I get a Container from one script, I want to be able to use it as the input for another call. e.g. dagger script scan or similar translates to an dagger call --source=. engine container being input to dagger call -m trivy-scanner --input=<container-from-previous-step>. (this would be a huge improvement to composing modules together).
    • Running commands in parallel. e.g. dagger script generate-all should run all of dagger call --source=. sdk <go|python|...> generate export --path=. all at once, in parallel (today this is a pain, all the output really just overlaps badly).

    I like the idea of scripts being just SDKs run outside of the engine - this means it's wayyy easier to express all of these above things - trying to have something shell-like that can do all this feels a bit annoying to have to write/maintain/use.

    But... if we do go that route, potentially we could have a bash-SDK? Or something like that, maybe it could only be used for implementing scripts - that way we could still allow having (very limited) scripts written in bash, paired with the power of dagger. 😆


    It seems like the question at the moment is whether these scripts execute on the client or on the engine? If they run on the client, it feels like it's much too tempting to manually modify and edit files, instead of using the dagger api to access them - which then is trickier to visualize in the tui/dagger cloud, get good caching, etc - it's also very dependent on the client environment. I'd much rather have the environment be the SDK function containers, even if they're not using exactly the same codegen APIs, etc.

  8. helderco commented on Apr 30, 2024

    @helderco
    Contributor

    today this is a pain, all the output really just overlaps badly

    Just a note on output, we need to wrap concurrent calls in their own telemetry spans inside the functions. Additionally we can also wrap a span for each test in the test suite. That's also true for language specific tests. I think doing this will help sort the output tremendously.

  9. shykes commented on Apr 30, 2024

    @shykes
    ContributorAuthor

    today this is a pain, all the output really just overlaps badly

    Wouldn't this be fixed by all the calls sharing the same session? Similar to dagger run sh?

  10. pjmagee commented on May 27, 2024

    @pjmagee
    Contributor

    Would introducing bash not seen in some ways leaning towards "glue". I know it isn't with how Dagger works but an argument about not having to have bash scripts for nice clean portable programmable CICD Pipelines seems to have less meaning when a Bash SDK exists. Maybe I'm not fully understanding this and this is likely the case but as an outsider just being curious reading proposals.

  11. locked and limited conversation to collaborators on Mar 24, 2026
  12. converted this issue into a discussion #12490 on Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions