Connection
- 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).
- class dagger.Connection(config: Config | None = None)
Connect to a Dagger Engine.
Example:
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)
Experimental
Warning
These functions are part of an experimental feature to use a globally available client instead of getting an instance from a connection. Their interfaces and availability may change in the future.
- 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 so there’s no need to pass around a client instance with this.Example:
async def main(): async with dagger.connection(): ctr = dagger.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 async def main(): cfg = dagger.Config(log_output=sys.stderr) async with dagger.connection(cfg): ctr = dagger.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)
Warning
Experimental.
- dagger.closing()
Context manager that closes the global client’s connection.
It’s an alternative to
dagger.connection()
, without automatic engine provisioning and has a lazy connection. The connection is only established when needed, i.e., when callingawait
on a client method.Example:
import anyio import dagger async def main(): async with dagger.closing(): ctr = dagger.container().from_("python:3.11.1-alpine") # Connection is only established when needed. version = await ctr.with_exec(["python", "-V"]).stdout() # Connection is closed when leaving the context manager's scope. print(version) # Output: Python 3.11.1 anyio.run(main)
Warning
Experimental.
- async dagger.connect() Self
Connect to a Dagger Engine using the global client.
Similar to
dagger.closing()
but establishes the connection explicitly rather than on first use.Example:
import anyio import dagger async def main(): async with await dagger.connect(): ctr = dagger.container().from_("python:3.11.1-alpine") # Connection is only established when needed. version = await ctr.with_exec(["python", "-V"]).stdout() # Connection is closed when leaving the context manager's scope. print(version) # Output: Python 3.11.1 anyio.run(main)