Skip to main content

Command Execution in Containers

The Dagger CLI can add follow-up processing to a just-in-time container, essentially enabling you to continue the pipeline directly from the command-line. Container objects expose a WithExec() function, which lets you execute a command in the corresponding container.

Here is an example of chaining a WithExec() function call to a container returned by a Wolfi container builder Dagger Function, to execute a command that displays the contents of the /etc/ directory:

dagger -m github.com/shykes/daggerverse/wolfi@v0.1.4 call container with-exec --args="ls","/etc/" stdout

Here is an example of chaining a WithExec() function call to a container returned by a Wolfi container builder Dagger Function, to execute a command that displays the contents of the /etc/os-release file:

dagger -m github.com/shykes/daggerverse/wolfi@v0.1.4 call container with-exec --args="cat","/etc/os-release" stdout

Here is an example of chaining a WithExec() function call to modify a container returned by a Wolfi container builder Dagger Function, by adding the current directory from the host to the container filesysytem at /src:

warning

The example below uploads the entire current directory to the container filesystem. This can take a significant amount of time with large directories. To reduce the time spent on upload, run this example from a directory containing only a few small files.

dagger -m github.com/shykes/daggerverse/wolfi@v0.1.4 call container with-directory --path=/src --directory=. with-exec --args="ls","/src" stdout

Here is an example of chaining a WithExec() function call to do the reverse: modify a container returned by a Wolfi container builder Dagger Function, by removing the /etc/os-release file from the container filesysytem:

dagger -m github.com/shykes/daggerverse/wolfi@v0.1.4 call container with-exec --args="rm","/etc/os-release" with-exec --args="ls","/etc" stdout

Here is another example which chains multiple WithExec() calls to install the curl package in a Wolfi container, send an HTTP request, and return the output:

dagger -m github.com/shykes/daggerverse/wolfi@v0.1.4 call container with-exec --args="apk,add,curl" with-exec --args="curl,-L,dagger.io" stdout