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 and push to Docker Hub:
dagger call build-and-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@0d23f76b089363c2bf5de43862ebfe71a69789e3

Entrypoint

Return Type
Docker !
Example
dagger -m github.com/broadsage/dagger-modules/modules/docker@0d23f76b089363c2bf5de43862ebfe71a69789e3 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"] )

buildAndPush() 🔗

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@0d23f76b089363c2bf5de43862ebfe71a69789e3 call \
 build-and-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().
			BuildAndPush(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()
		.build_and_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()
		.buildAndPush(context, imageName, username, password, registry, dockerfile)
}