Implementation:Astronomer Astronomer cosmos Docker Operators
| Knowledge Sources | |
|---|---|
| Domains | Docker, Operators |
| Last Updated | 2026-02-07 17:00 GMT |
Overview
Provides Airflow operators for executing dbt commands inside Docker containers on the Airflow worker by combining the Cosmos abstract dbt interface with Airflow's native DockerOperator.
Description
The Docker_Operators module defines DbtDockerBaseOperator, which inherits from both AbstractDbtBase and DockerOperator. This dual inheritance allows dbt CLI commands to be constructed through the Cosmos abstract layer and then executed inside a Docker container that is spawned directly on the Airflow worker host. The base operator translates the dbt profile configuration, environment variables, and assembled command into Docker container overrides, managing the full container lifecycle including image pull, execution, log capture, and cleanup.
Unlike the cloud-specific operators (ECS, EKS, ACI), the Docker operators do not require external infrastructure or cloud credentials. They rely on the Docker daemon being accessible from the Airflow worker, making them suitable for local development, testing, and on-premises deployments.
Nine concrete operator classes extend the base, covering dbt sub-commands: build, ls, seed, snapshot, source freshness, run, test, run-operation, and clone. Each concrete class configures its own ui_color and ui_fgcolor for the Airflow graph view.
Usage
Use the Docker operators when you want to run dbt inside a container but do not need or have access to a cloud container orchestration service. This is particularly useful for local development and testing where the Airflow worker has Docker installed, for on-premises environments without Kubernetes, or for CI/CD pipelines that verify dbt transformations in isolated containers. The Docker operators also serve as a straightforward stepping stone before migrating to cloud-based container execution.
Code Reference
Source Location
- Repository: Astronomer_Astronomer_cosmos
- File: cosmos/operators/docker.py
Signature
class DbtDockerBaseOperator(AbstractDbtBase, DockerOperator):
"""
Base class for running dbt commands inside Docker containers.
"""
def __init__(
self,
image: str,
profile_config: ProfileConfig | None = None,
**kwargs,
) -> None:
...
Concrete operators:
class DbtBuildDockerOperator(DbtDockerBaseOperator): ...
class DbtLSDockerOperator(DbtDockerBaseOperator): ...
class DbtSeedDockerOperator(DbtDockerBaseOperator): ...
class DbtSnapshotDockerOperator(DbtDockerBaseOperator): ...
class DbtSourceDockerOperator(DbtDockerBaseOperator): ...
class DbtRunDockerOperator(DbtDockerBaseOperator): ...
class DbtTestDockerOperator(DbtDockerBaseOperator): ...
class DbtRunOperationDockerOperator(DbtDockerBaseOperator): ...
class DbtCloneDockerOperator(DbtDockerBaseOperator): ...
Import
from cosmos.operators.docker import DbtRunDockerOperator
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| image | str | Yes | The Docker image to use for running the dbt command (e.g., "ghcr.io/dbt-labs/dbt-core:latest").
|
| profile_config | ProfileConfig or None | No | Cosmos ProfileConfig object defining the dbt profile and target to use. |
Outputs
| Name | Type | Description |
|---|---|---|
| container_id | str | The ID of the Docker container that executed the dbt command, available via XCom. |
| log_output | str | Standard output and error from the dbt command, captured from the container logs. |
Usage Examples
from cosmos.operators.docker import DbtRunDockerOperator
run_dbt = DbtRunDockerOperator(
task_id="dbt_run_docker",
image="ghcr.io/dbt-labs/dbt-core:1.7.0",
)
from cosmos.operators.docker import DbtSeedDockerOperator, DbtTestDockerOperator
seed_dbt = DbtSeedDockerOperator(
task_id="dbt_seed_docker",
image="my-dbt-image:latest",
)
test_dbt = DbtTestDockerOperator(
task_id="dbt_test_docker",
image="my-dbt-image:latest",
)
seed_dbt >> test_dbt