Skip to main content

Container

The Container type represents the state of an OCI-compatible container. This Container object is not merely a string referencing an image on a remote registry. It is the actual state of a container, managed by the Dagger Engine, and passed to a Dagger Function's code as if it were just another variable.

Common operations​

Some of the common operations used on the Container type include:

FieldDescription
fromInitializes the container from a specified base image
asServiceTurns the container into a Service
asTarballReturns a serialized tarball of the container as a File
export / importWrites / reads the container as an OCI tarball to / from a file path on the host
publishPublishes the container image to a registry
stdout / stderrReturns the output / error stream of the last executed command
withDirectory / withMountedDirectoryReturns the container plus a directory copied / mounted at the given path
withEntrypointReturns the container with a custom entrypoint command
withExecReturns the container after executing a command inside it
withFile / withMountedFileReturns the container plus a file copied / mounted at the given path
withMountedCacheReturns the container plus a cache volume mounted at the given path
withRegistryAuthReturns the container with registry authentication configured
withWorkdirReturns the container configured with a specific working directory
withServiceBindingReturns the container with runtime dependency on another Service
terminalOpens an interactive terminal for this container

Default addresses​

It is possible to assign a default address for a Container argument in a Dagger Function. Dagger will automatically use this default address to pull the container image when no value is specified for the argument.

tip

Default addresses are only available for Container arguments. They are commonly used to provide a sensible default base image for build or test operations. When a value is explicitly passed for the argument, it always overrides the default address.

Here's an example:

The default address is set by adding a defaultAddress pragma on the corresponding Dagger Function ctr argument.

package main

import (
"context"
"dagger/my-module/internal/dagger"
)

type MyModule struct{}

func (m *MyModule) Version(
ctx context.Context,
// +defaultAddress="alpine:latest"
ctr *dagger.Container,
) (string, error) {
return ctr.WithExec([]string{"cat", "/etc/alpine-release"}).Stdout(ctx)
}

The default address can be any valid container image reference, such as:

  • alpine:latest - Docker Hub image with tag
  • alpine:3.19 - Docker Hub image with specific version
  • ghcr.io/owner/image:tag - GitHub Container Registry image
  • gcr.io/project/image:tag - Google Container Registry image