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=python3
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 python3
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=python3 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:
python3 -c "print('Hello from Dagger')"
You should see the following output:
Hello from Dagger
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 function calls to WithExec()
and Stdout()
on the Container
returned previously, as shown in the following command:
dagger -m github.com/shykes/daggerverse/wolfi@v0.1.2 call \
container --packages=python3 \
with-exec --args=python3,-c,"print('Hello from Dagger')" \
stdout
You should see the following output:
Hello from Dagger
Publish the container image to a registry
The Container
type also makes a number of other functions available. To see some of these in action, call the previous function again, chaining additional function calls to WithEntrypoint()
and Publish()
on the returned Container
to set the container entrypoint command and publish it to a registry:
dagger -m github.com/shykes/daggerverse/wolfi@v0.1.2 call \
container --packages=python3 \
with-entrypoint --args=python3,-c,"print('Hello from Dagger')" \
publish --address ttl.sh/dagger-$RANDOM
The output will be a container image reference on the ttl.sh container registry, as shown below:
ttl.sh/dagger-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 ttl.sh/dagger-10939
You should see the following output:
Hello from Dagger