Connection

dagger.connection(config: Config | None = None)

Connect to a Dagger Engine using the global client.

This is similar to dagger.Connection but uses a global client (dagger.dag) so there’s no need to pass around a client instance with this.

Example:

import dagger
from dagger import dag

async def main():
    async with dagger.connection():
        ctr = dag.container().from_("alpine")

    # Connection is closed when leaving the context manager's scope.

You can stream the logs from the engine to see progress:

import sys
import anyio
import dagger
from dagger import dag

async def main():
    cfg = dagger.Config(log_output=sys.stderr)

    async with dagger.connection(cfg):
        ctr = dag.container().from_("python:3.11.1-alpine")
        version = await ctr.with_exec(["python", "-V"]).stdout()

    print(version)
    # Output: Python 3.11.1

anyio.run(main)
class dagger.Connection(config: Config | None = None)

Connect to a Dagger Engine with an isolated client (legacy).

This is an older version of dagger.connection() that uses an isolated client instance. Should no longer be used in newer projects unless there’s a specific reason to do so.

Example:

import dagger

async def main():
    async with dagger.Connection() as client:
        ctr = client.container().from_("alpine")

You can stream the logs from the engine to see progress:

import sys
import anyio
import dagger

async def main():
    cfg = dagger.Config(log_output=sys.stderr)

    async with dagger.Connection(cfg) as client:
        ctr = client.container().from_("python:3.11.1-alpine")
        version = await ctr.with_exec(["python", "-V"]).stdout()

    print(version)
    # Output: Python 3.11.1

anyio.run(main)
class dagger.Config(*, timeout: ~dagger.Timeout | None = <factory>, retry: ~dagger.Retry | None = <factory>, workdir: ~os.PathLike[str] | str = '', config_path: ~os.PathLike[str] | str = '', log_output: ~typing.TextIO | None = None, execute_timeout: ~typing.Any = <object object>)

Options for connecting to the Dagger engine.

Parameters:
  • workdir (os.PathLike[str] | str) – The host workdir loaded into dagger.

  • config_path (os.PathLike[str] | str) – Project config file.

  • log_output (TextIO | None) – A TextIO object to send the logs from the engine.

  • timeout (dagger.Timeout | None) – The maximum time in seconds for establishing a connection to the server, or None to disable. Defaults to 10 seconds.

  • execute_timeout (Any) – The maximum time in seconds for the execution of a request before an ExecuteTimeoutError is raised. Passing None results in waiting forever for a response (default).