Dagster Docker
Dagster-docker is a Dagster integration that enables launching and executing Dagster runs or individual ops/assets within Docker containers. As part of the broader Dagster ecosystem, it follows a separate versioning scheme (currently 0.29.0) but is released in lockstep with the core `dagster` library, which sees frequent updates (typically minor releases weekly or bi-weekly, with major changes less often).
Warnings
- gotcha The machine running the Dagster daemon/webserver (for `DockerRunLauncher`) or the machine launching jobs (for `docker_executor`) must have access to a running Docker daemon.
- gotcha Docker containers launched by `dagster-docker` need to be able to communicate back to the Dagster API server (e.g., Dagit, Dagster daemon). Incorrect network configuration can lead to runs hanging or failing to report status.
- breaking `dagster-docker` versions are tightly coupled with the `dagster` core library. Using `dagster-docker` with a significantly mismatched `dagster` core version (e.g., `dagster-docker==0.29.0` with `dagster==1.0.0`) can lead to runtime errors or unexpected behavior due to API changes.
Install
-
pip install dagster-docker
Imports
- DockerRunLauncher
from dagster_docker import DockerRunLauncher
- docker_executor
from dagster_docker import docker_executor
Quickstart
from dagster import Definitions, asset, define_asset_job
from dagster_docker import docker_executor
import os
# Define a simple asset
@asset
def my_docker_asset():
"""An asset whose execution will be managed by a Docker executor."""
message = os.environ.get("GREETING", "Hello from default asset!")
print(message)
return message
# Define a job that uses the docker_executor.
# The configuration demonstrates how to specify Docker image and environment variables
# for the container in which the asset will run.
my_docker_job = define_asset_job(
name="my_docker_job",
selection=[my_docker_asset],
executor_def=docker_executor.configured(
{
"container_kwargs": {
# This image needs to have Python and your Dagster code installed.
# For a minimal example, we use a basic Python image. In production,
# you'd build an image with your specific Dagster code and dependencies.
"image": os.environ.get("DOCKER_IMAGE", "python:3.10-slim-buster"),
"environment": {
"GREETING": "Hello from inside the Docker container!"
},
# Optional: specify a Docker network if your Dagster instance
# is also running in Docker and needs to communicate.
"network_mode": "bridge"
},
# If you want to pass host environment variables directly to the container,
# list them here. The values will be taken from the environment where Dagster runs.
"env_vars": ["HOST_VAR"]
}
),
)
# To make these definitions loadable by Dagster (e.g., `dagster dev -f my_file.py`)
# you would typically expose them via a Definitions object:
# defs = Definitions(
# jobs=[my_docker_job],
# )