Dagger
Search

docker

This Dagger module provides a clean, type-safe interface for building container
images from Dockerfiles and publishing them to any container registry.

Key Features:
- Build images from Dockerfiles
- Push to any container registry (Docker Hub, GHCR, GitLab, GCR, etc.)
- Support for multiple tags per build
- Comprehensive validation and error handling
- Secure credential handling with Dagger secrets

Usage:
Build an image:
dagger call build --context=.

Build and push to Docker Hub:
dagger call push --context=. --image-name=user/app \
--username=user --password=env:DOCKERHUB_TOKEN \
--tags=latest --tags=v1.0.0

Advanced Features:
For multi-stage builds, build arguments, and platform specifications,
use standard Dockerfile syntax:

Multi-stage builds:
FROM node:18 AS builder
...
FROM node:18-alpine AS production

Build arguments:
ARG VERSION=1.0
ARG BUILD_DATE

Platform specification:
FROM --platform=linux/amd64 node:18

For more information, see the README.md file.

Installation

dagger install github.com/broadsage/dagger-modules/modules/docker@905eff044efa0c9db6dcb632a01b9f4276bd1df6

Entrypoint

Return Type
Docker !
Example
dagger -m github.com/broadsage/dagger-modules/modules/docker@905eff044efa0c9db6dcb632a01b9f4276bd1df6 call \
func (m *MyModule) Example() *dagger.Docker  {
	return dag.
			Docker()
}
@function
def example() -> dagger.Docker:
	return (
		dag.docker()
	)
@func()
example(): Docker {
	return dag
		.docker()
}

Types

Docker 🔗

Docker module for building and publishing container images. This class provides methods to build container images from Dockerfiles and push them to various container registries with proper authentication and validation. Attributes: None - This module is stateless and thread-safe. Example: # Build an image docker = Docker() container = docker.build(context=src_dir) # Build and push to Docker Hub result = await docker.push( context=src_dir, image_name="myuser/myapp", username="myuser", password=token, tags=["latest", "v1.0.0"] )

build() 🔗

Build a container image from a Dockerfile.

Args: context: Directory containing the Dockerfile and build context. dockerfile: Path to the Dockerfile relative to context (default: “Dockerfile”).

Returns: Built container ready for further operations.

Raises: ValueError: If dockerfile path is invalid.

Example: # Build with default Dockerfile container = docker.build(context=src_dir)

# Build with custom Dockerfile
container = docker.build(
    context=src_dir,
    dockerfile="Dockerfile.production"
)

Note: For multi-stage builds, build arguments, and platform specifications, use standard Dockerfile syntax (FROM –platform=, ARG, FROM … AS stage).

Return Type
Container !
Arguments
NameTypeDefault ValueDescription
contextDirectory !-A directory.
dockerfileString !"Dockerfile"No description provided
Example
dagger -m github.com/broadsage/dagger-modules/modules/docker@905eff044efa0c9db6dcb632a01b9f4276bd1df6 call \
 build --context DIR_PATH --dockerfile string
func (m *MyModule) Example(context *dagger.Directory, dockerfile string) *dagger.Container  {
	return dag.
			Docker().
			Build(context, dockerfile)
}
@function
def example(context: dagger.Directory, dockerfile: str) -> dagger.Container:
	return (
		dag.docker()
		.build(context, dockerfile)
	)
@func()
example(context: Directory, dockerfile: string): Container {
	return dag
		.docker()
		.build(context, dockerfile)
}

push() 🔗

Build and push a container image to a registry.

This is the main function for building and publishing images to any registry. Orchestrate multiple registries in your CI/CD (GitHub Actions, GitLab CI, etc.).

Args: context: Directory containing the Dockerfile and build context. image_name: Name of the image (e.g., “myorg/myapp” or “org/app” for GHCR). username: Registry username for authentication. password: Registry password/token secret for authentication. registry: Registry URL (default: “docker.io”). Examples: - “docker.io” (Docker Hub) - “ghcr.io” (GitHub Container Registry) - “registry.gitlab.com” (GitLab) - “gcr.io” (Google Container Registry) tags: List of tags to apply (default: [“latest”]). dockerfile: Path to the Dockerfile relative to context (default: “Dockerfile”).

Returns: Published image references with digests (one per line).

Raises: ValueError: If validation fails for any parameter. RuntimeError: If push operation fails.

Example: # Docker Hub with multiple tags result = await docker.push( context=src_dir, image_name=“user/app”, username=“user”, password=dockerhub_token, registry=“docker.io”, tags=[“latest”, “v1.0.0”, “stable”] )

# GHCR with custom Dockerfile
result = await docker.push(
    context=src_dir,
    image_name="org/app",
    username="github_user",
    password=github_token,
    registry="ghcr.io",
    dockerfile="Dockerfile.production",
    tags=["latest"]
)

Note: For multi-stage builds, build arguments, and platform specifications, configure these directly in your Dockerfile using standard Docker syntax.

Return Type
String !
Arguments
NameTypeDefault ValueDescription
contextDirectory !-A directory.
imageNameString !-No description provided
usernameString !-No description provided
passwordSecret !-A reference to a secret value, which can be handled more safely than the value itself.
registryString !"docker.io"No description provided
tags[String ! ] nullNo description provided
dockerfileString !"Dockerfile"No description provided
Example
dagger -m github.com/broadsage/dagger-modules/modules/docker@905eff044efa0c9db6dcb632a01b9f4276bd1df6 call \
 push --context DIR_PATH --image-name string --username string --password env:MYSECRET --registry string --dockerfile string
func (m *MyModule) Example(ctx context.Context, context *dagger.Directory, imageName string, username string, password *dagger.Secret, registry string, dockerfile string) string  {
	return dag.
			Docker().
			Push(ctx, context, imageName, username, password, registry, dockerfile)
}
@function
async def example(context: dagger.Directory, image_name: str, username: str, password: dagger.Secret, registry: str, dockerfile: str) -> str:
	return await (
		dag.docker()
		.push(context, image_name, username, password, registry, dockerfile)
	)
@func()
async example(context: Directory, imageName: string, username: string, password: Secret, registry: string, dockerfile: string): Promise<string> {
	return dag
		.docker()
		.push(context, imageName, username, password, registry, dockerfile)
}