Skip to main content

Quickstart

Build a container with a function

Just as you can chain and return Directory types, you can also chain and return Container types in functions.

Call a builder function

Try calling this function:

dagger -m github.com/shykes/daggerverse/wolfi@v0.1.2 call container --packages=cowsay

This Wolfi container builder module exposes a Container() function that returns a base Wolfi container image, and accepts arguments to additional packages in the base image;

You can use this function to build and return a Wolfi container image containing specific packages - in this example, the cowsay package. You should see the container being built and the packages being added, as shown below:

Container evaluated. Use "dagger call base with-package container --help" to see available sub-commands.

This means that the build succeeded, and a Container type representing the built container image was returned. And, like Directory, Container is a Dagger core type which comes with useful functions of its own.

Start an interactive terminal session with the container

One of the most interesting Container functions is Terminal(), which can be used to open an interactive terminal session with the container. This feature is very useful for debugging and experimenting since it allows you to interact with containers directly and at any stage of your Dagger function execution.

To see this in action, call the previous function again, this time chaining an additional function call to Terminal() on the returned Container:

dagger -m github.com/shykes/daggerverse/wolfi@v0.1.2 call container --packages=cowsay terminal

This revised command builds the container image and then drops you into an interactive terminal, allowing you to directly execute commands in the running container.

Verify this by executing the following command:

cowsay "dagger"

You should see output similar to the following:

 ________
< dagger >
--------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
caution

While most terminal programs such as htop or vim work with dagger ... terminal, you may experience visual bugs with some programs due to terminal emulator incompatibilities.

Execute commands in the container

The Container type has a WithExec() function, which returns the container after executing a specific command inside it. So, you could achieve the same result as before (although non-interactively) by chaining a function call to withExec() on the Container returned previously, as shown in the following command:

dagger -m github.com/shykes/daggerverse/wolfi@v0.1.2 call container --packages=cowsay with-exec --args cowsay,dagger stdout

Publish the container image to a registry

The Container type also has a Publish() function, which publishes the container to a registry. To see this in action, call the previous function again, this time chaining an additional function call to Publish() on the returned Container:

dagger -m github.com/shykes/daggerverse/wolfi@v0.1.2 \
call container --packages=cowsay \
publish --address ttl.sh/dagger-cowsay-$RANDOM

The output will be a container image reference on the ttl.sh container registry, as shown below:

ttl.sh/dagger-cowsay-10939@sha256:57c15999fdc59df452161f648aaa9b9a1ea9dbda710a0a0242f509966a286b4b

Test the container image using the command below (remember to update the image reference based on the function output):

docker run --rm -it ttl.sh/dagger-cowsay-10939 cowsay "dagger"

You should see the output below:

 ________
< dagger >
--------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||