2.2 Functions Chaining

Functions Chaining

Dagger Functions can return either basic types or objects. Objects can define their own functions.
So 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 , File , Service , …) are all objects. They each define various functions for interacting with their respective objects.

Let’s explore them step by step:

dagger call \
  --mod github.com/puzzle/dagger-techlab/mod@v1.0.0 \
  --help
show available 'module' functions
$$ USAGE dagger call [options] [arguments] FUNCTIONS hello Say hello to the world! lint Lint a Python codebase ls Returns the files of the directory os Returns the operating system of the container ---> ssh-service Returns a service that runs an OpenSSH server unlock Returns the answer to everything when the password is right wolfi Build a Wolfi Linux container$$
dagger call \
  --mod github.com/puzzle/dagger-techlab/mod@v1.0.0 \
  ssh-service --help
show available 'service' object functions
$$ USAGE dagger call ssh-service [arguments] FUNCTIONS endpoint Retrieves an endpoint that clients can use to reach this container. hostname Retrieves a hostname which can be used by clients to reach this container. ports Retrieves the list of ports provided by the service. start Start the service and wait for its health checks to succeed. stop Stop the service. ---> up Creates a tunnel that forwards traffic from the callers network to this service.$$
dagger call \
  --mod github.com/puzzle/dagger-techlab/mod@v1.0.0 \
  ssh-service up --help
show available 'up' function arguments
$$ USAGE dagger call ssh-service up [arguments] ARGUMENTS ---> --ports PortForward List of frontend/backend port mappings to forward. Frontend is the port accepting traffic on the host, backend is the service port. (default []) --random Bind each tunnel port to a random port on the host.$$

Now that we have got all the pieces together, let’s expose a Service returned by a Dagger Function on a specified host port, by chaining a call to the Service objects up function:

dagger call \
  --mod github.com/puzzle/dagger-techlab/mod@v1.0.0 \
  ssh-service up --ports=22022:22

Here we print the contents of a File returned by a Dagger Function, by chaining a call to the File objects contents function:

dagger call \
  --mod github.com/puzzle/dagger-techlab/mod@v1.0.0 \
  lint --source=https://github.com/puzzle/puzzle-radicale-auth-ldap report contents

Task 2.2.1: Chain calls

Display and return the contents of the /etc/os-release file in a container, by chaining additional calls to the Container object, returned by the modules wolfi function:

show hint

Have a look at the WithExec() and Stout() functions.

show solution
dagger call \
  --mod github.com/puzzle/dagger-techlab/mod@v1.0.0 \
  wolfi with-exec --args="cat","/etc/os-release" stdout

Try an alternative approach using File() instead.

show hint
dagger call \
  --mod github.com/puzzle/dagger-techlab/mod@v1.0.0 \
  wolfi file --help
show solution
dagger call \
  --mod github.com/puzzle/dagger-techlab/mod@v1.0.0 \
  wolfi file --path=/etc/os-release contents