Skip to content

prefect_docker.worker

Module containing the Docker worker used for executing flow runs as Docker containers.

To start a Docker worker, run the following command:

prefect worker start --pool 'my-work-pool' --type docker

Replace my-work-pool with the name of the work pool you want the worker to poll for flow runs.

For more information about work pools and workers, checkout out the Prefect docs.

Classes

DockerWorker

Bases: BaseWorker

Prefect worker that executes flow runs within Docker containers.

Source code in prefect_docker/worker.py
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
class DockerWorker(BaseWorker):
    """Prefect worker that executes flow runs within Docker containers."""

    type = "docker"
    job_configuration = DockerWorkerJobConfiguration
    _description = (
        "Execute flow runs within Docker containers. Works well for managing flow "
        "execution environments via Docker images. Requires access to a running "
        "Docker daemon."
    )
    _display_name = "Docker"
    _documentation_url = "https://prefecthq.github.io/prefect-docker/worker/"
    _logo_url = "https://images.ctfassets.net/gm98wzqotmnx/2IfXXfMq66mrzJBDFFCHTp/6d8f320d9e4fc4393f045673d61ab612/Moby-logo.png?h=250"  # noqa

    def __init__(self, *args: Any, test_mode: bool = None, **kwargs: Any) -> None:
        if test_mode is None:
            self.test_mode = bool(os.getenv("PREFECT_DOCKER_TEST_MODE", False))
        else:
            self.test_mode = test_mode
        super().__init__(*args, **kwargs)

    async def setup(self):
        if not self.test_mode:
            self._client = get_client()
            if self._client.server_type == ServerType.EPHEMERAL:
                raise RuntimeError(
                    "Docker worker cannot be used with an ephemeral server. Please set"
                    " PREFECT_API_URL to the URL for your Prefect API instance. You"
                    " can use a local Prefect API instance by running `prefect server"
                    " start`."
                )

        return await super().setup()

    async def run(
        self,
        flow_run: "FlowRun",
        configuration: BaseJobConfiguration,
        task_status: Optional[anyio.abc.TaskStatus] = None,
    ) -> BaseWorkerResult:
        """
        Executes a flow run within a Docker container and waits for the flow run
        to complete.
        """
        # The `docker` library uses requests instead of an async http library so it must
        # be run in a thread to avoid blocking the event loop.
        container, created_event = await run_sync_in_worker_thread(
            self._create_and_start_container, configuration
        )
        container_pid = self._get_infrastructure_pid(container_id=container.id)

        # Mark as started and return the infrastructure id
        if task_status:
            task_status.started(container_pid)

        # Monitor the container
        container = await run_sync_in_worker_thread(
            self._watch_container_safe, container, configuration, created_event
        )

        exit_code = container.attrs["State"].get("ExitCode")
        return DockerWorkerResult(
            status_code=exit_code if exit_code is not None else -1,
            identifier=container_pid,
        )

    async def kill_infrastructure(
        self,
        infrastructure_pid: str,
        configuration: DockerWorkerJobConfiguration,
        grace_seconds: int = 30,
    ):
        """
        Stops a container for a cancelled flow run based on the provided infrastructure
        PID.
        """
        docker_client = self._get_client()

        base_url, container_id = self._parse_infrastructure_pid(infrastructure_pid)
        if docker_client.api.base_url != base_url:
            raise InfrastructureNotAvailable(
                "".join(
                    [
                        (
                            f"Unable to stop container {container_id!r}: the current"
                            " Docker API "
                        ),
                        (
                            f"URL {docker_client.api.base_url!r} does not match the"
                            " expected "
                        ),
                        f"API base URL {base_url}.",
                    ]
                )
            )
        await run_sync_in_worker_thread(
            self._stop_container, container_id, docker_client, grace_seconds
        )

    def _stop_container(
        self,
        container_id: str,
        client: "DockerClient",
        grace_seconds: int = 30,
    ):
        try:
            container = client.containers.get(container_id=container_id)
        except docker.errors.NotFound:
            raise InfrastructureNotFound(
                f"Unable to stop container {container_id!r}: The container was not"
                " found."
            )

        container.stop(timeout=grace_seconds)

    def _get_client(self):
        """Returns a docker client."""
        try:
            with warnings.catch_warnings():
                # Silence warnings due to use of deprecated methods within dockerpy
                # See https://github.com/docker/docker-py/pull/2931
                warnings.filterwarnings(
                    "ignore",
                    message="distutils Version classes are deprecated.*",
                    category=DeprecationWarning,
                )

                docker_client = docker.from_env()

        except docker.errors.DockerException as exc:
            raise RuntimeError("Could not connect to Docker.") from exc

        return docker_client

    def _get_infrastructure_pid(self, container_id: str) -> str:
        """Generates a Docker infrastructure_pid string in the form of
        `<docker_host_base_url>:<container_id>`.
        """
        docker_client = self._get_client()
        base_url = docker_client.api.base_url
        docker_client.close()
        return f"{base_url}:{container_id}"

    def _parse_infrastructure_pid(self, infrastructure_pid: str) -> Tuple[str, str]:
        """Splits a Docker infrastructure_pid into its component parts"""

        # base_url can contain `:` so we only want the last item of the split
        base_url, container_id = infrastructure_pid.rsplit(":", 1)
        return base_url, str(container_id)

    def _build_container_settings(
        self,
        docker_client: "DockerClient",
        configuration: DockerWorkerJobConfiguration,
    ) -> Dict:
        """Builds a dictionary of container settings to pass to the Docker API."""
        network_mode = configuration.get_network_mode()
        return dict(
            image=configuration.image,
            network=configuration.networks[0] if configuration.networks else None,
            network_mode=network_mode,
            command=configuration.command,
            environment=configuration.env,
            auto_remove=configuration.auto_remove,
            labels=configuration.labels,
            extra_hosts=configuration.get_extra_hosts(docker_client),
            name=configuration.name,
            volumes=configuration.volumes,
            mem_limit=configuration.mem_limit,
            memswap_limit=configuration.memswap_limit,
            privileged=configuration.privileged,
        )

    def _create_and_start_container(
        self, configuration: DockerWorkerJobConfiguration
    ) -> Tuple["Container", Event]:
        """Creates and starts a Docker container."""
        docker_client = self._get_client()
        if configuration.registry_credentials:
            self._logger.info("Logging into Docker registry...")
            docker_client.login(
                username=configuration.registry_credentials.username,
                password=configuration.registry_credentials.password.get_secret_value(),
                registry=configuration.registry_credentials.registry_url,
                reauth=configuration.registry_credentials.reauth,
            )
        container_settings = self._build_container_settings(
            docker_client, configuration
        )

        if self._should_pull_image(docker_client, configuration=configuration):
            self._logger.info(f"Pulling image {configuration.image!r}...")
            self._pull_image(docker_client, configuration)

        try:
            container = self._create_container(docker_client, **container_settings)
        except Exception as exc:
            self._emit_container_creation_failed_event(configuration)
            raise exc

        created_event = self._emit_container_status_change_event(
            container, configuration
        )

        # Add additional networks after the container is created; only one network can
        # be attached at creation time
        if len(configuration.networks) > 1:
            for network_name in configuration.networks[1:]:
                network = docker_client.networks.get(network_name)
                network.connect(container)

        # Start the container
        container.start()

        docker_client.close()

        return container, created_event

    def _watch_container_safe(
        self,
        container: "Container",
        configuration: DockerWorkerJobConfiguration,
        created_event: Event,
    ) -> "Container":
        """Watches a container for completion, handling any errors that may occur."""
        # Monitor the container capturing the latest snapshot while capturing
        # not found errors
        docker_client = self._get_client()

        try:
            seen_statuses = {container.status}
            last_event = created_event
            for latest_container in self._watch_container(
                docker_client, container.id, configuration
            ):
                container = latest_container
                if container.status not in seen_statuses:
                    seen_statuses.add(container.status)
                    last_event = self._emit_container_status_change_event(
                        container, configuration, last_event=last_event
                    )

        except docker.errors.NotFound:
            # The container was removed during watching
            self._logger.warning(
                f"Docker container {container.name} was removed before we could wait "
                "for its completion."
            )
        finally:
            docker_client.close()

        return container

    def _watch_container(
        self,
        docker_client: "DockerClient",
        container_id: str,
        configuration: DockerWorkerJobConfiguration,
    ) -> Generator[None, None, "Container"]:
        """
        Watches a container for completion, yielding the latest container
        snapshot on each iteration.
        """
        container: "Container" = docker_client.containers.get(container_id)

        status = container.status
        self._logger.info(
            f"Docker container {container.name!r} has status {container.status!r}"
        )
        yield container

        if configuration.stream_output:
            try:
                for log in container.logs(stream=True):
                    log: bytes
                    print(log.decode().rstrip())
            except docker.errors.APIError as exc:
                if "marked for removal" in str(exc):
                    self._logger.warning(
                        f"Docker container {container.name} was marked for removal"
                        " before logs could be retrieved. Output will not be"
                        " streamed. "
                    )
                else:
                    self._logger.exception(
                        "An unexpected Docker API error occured while streaming output "
                        f"from container {container.name}."
                    )

            container.reload()
            if container.status != status:
                self._logger.info(
                    f"Docker container {container.name!r} has status"
                    f" {container.status!r}"
                )
            yield container

        container.wait()
        self._logger.info(
            f"Docker container {container.name!r} has status {container.status!r}"
        )
        yield container

    def _should_pull_image(
        self, docker_client: "DockerClient", configuration: DockerWorkerJobConfiguration
    ) -> bool:
        """
        Decide whether we need to pull the Docker image.
        """
        image_pull_policy = configuration._determine_image_pull_policy()

        if image_pull_policy is ImagePullPolicy.ALWAYS:
            return True
        elif image_pull_policy is ImagePullPolicy.NEVER:
            return False
        elif image_pull_policy is ImagePullPolicy.IF_NOT_PRESENT:
            try:
                # NOTE: images.get() wants the tag included with the image
                # name, while images.pull() wants them split.
                docker_client.images.get(configuration.image)
            except docker.errors.ImageNotFound:
                self._logger.debug(
                    f"Could not find Docker image locally: {configuration.image}"
                )
                return True
        return False

    def _pull_image(
        self, docker_client: "DockerClient", configuration: DockerWorkerJobConfiguration
    ):
        """
        Pull the image we're going to use to create the container.
        """
        image, tag = parse_image_tag(configuration.image)

        return docker_client.images.pull(image, tag)

    def _create_container(self, docker_client: "DockerClient", **kwargs) -> "Container":
        """
        Create a docker container with retries on name conflicts.

        If the container already exists with the given name, an incremented index is
        added.
        """
        # Create the container with retries on name conflicts (with an incremented idx)
        index = 0
        container = None
        name = original_name = kwargs.pop("name")

        while not container:
            try:
                display_name = repr(name) if name else "with auto-generated name"
                self._logger.info(f"Creating Docker container {display_name}...")
                container = docker_client.containers.create(name=name, **kwargs)
            except docker.errors.APIError as exc:
                if "Conflict" in str(exc) and "container name" in str(exc):
                    self._logger.info(
                        f"Docker container name {display_name} already exists; "
                        "retrying..."
                    )
                    index += 1
                    name = f"{original_name}-{index}"
                else:
                    raise

        self._logger.info(
            f"Docker container {container.name!r} has status {container.status!r}"
        )
        return container

    def _container_as_resource(self, container: "Container") -> Dict[str, str]:
        """Convert a container to a resource dictionary"""
        return {
            "prefect.resource.id": f"prefect.docker.container.{container.id}",
            "prefect.resource.name": container.name,
        }

    def _emit_container_creation_failed_event(
        self, configuration: DockerWorkerJobConfiguration
    ) -> Event:
        """Emit a Prefect event when a docker container fails to be created."""
        return emit_event(
            event="prefect.docker.container.creation-failed",
            resource=self._event_resource(),
            related=self._event_related_resources(configuration=configuration),
        )

    def _emit_container_status_change_event(
        self,
        container: "Container",
        configuration: DockerWorkerJobConfiguration,
        last_event: Optional[Event] = None,
    ) -> Event:
        """Emit a Prefect event for a Docker container event."""
        related = self._event_related_resources(configuration=configuration)

        worker_resource = self._event_resource()
        worker_resource["prefect.resource.role"] = "worker"
        worker_related_resource = RelatedResource(__root__=worker_resource)

        return emit_event(
            event=f"prefect.docker.container.{container.status.lower()}",
            resource=self._container_as_resource(container),
            related=related + [worker_related_resource],
            follows=last_event,
        )

Functions

kill_infrastructure async

Stops a container for a cancelled flow run based on the provided infrastructure PID.

Source code in prefect_docker/worker.py
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
async def kill_infrastructure(
    self,
    infrastructure_pid: str,
    configuration: DockerWorkerJobConfiguration,
    grace_seconds: int = 30,
):
    """
    Stops a container for a cancelled flow run based on the provided infrastructure
    PID.
    """
    docker_client = self._get_client()

    base_url, container_id = self._parse_infrastructure_pid(infrastructure_pid)
    if docker_client.api.base_url != base_url:
        raise InfrastructureNotAvailable(
            "".join(
                [
                    (
                        f"Unable to stop container {container_id!r}: the current"
                        " Docker API "
                    ),
                    (
                        f"URL {docker_client.api.base_url!r} does not match the"
                        " expected "
                    ),
                    f"API base URL {base_url}.",
                ]
            )
        )
    await run_sync_in_worker_thread(
        self._stop_container, container_id, docker_client, grace_seconds
    )
run async

Executes a flow run within a Docker container and waits for the flow run to complete.

Source code in prefect_docker/worker.py
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
async def run(
    self,
    flow_run: "FlowRun",
    configuration: BaseJobConfiguration,
    task_status: Optional[anyio.abc.TaskStatus] = None,
) -> BaseWorkerResult:
    """
    Executes a flow run within a Docker container and waits for the flow run
    to complete.
    """
    # The `docker` library uses requests instead of an async http library so it must
    # be run in a thread to avoid blocking the event loop.
    container, created_event = await run_sync_in_worker_thread(
        self._create_and_start_container, configuration
    )
    container_pid = self._get_infrastructure_pid(container_id=container.id)

    # Mark as started and return the infrastructure id
    if task_status:
        task_status.started(container_pid)

    # Monitor the container
    container = await run_sync_in_worker_thread(
        self._watch_container_safe, container, configuration, created_event
    )

    exit_code = container.attrs["State"].get("ExitCode")
    return DockerWorkerResult(
        status_code=exit_code if exit_code is not None else -1,
        identifier=container_pid,
    )

DockerWorkerJobConfiguration

Bases: BaseJobConfiguration

Configuration class used by the Docker worker.

An instance of this class is passed to the Docker worker's run method for each flow run. It contains all the information necessary to execute the flow run as a Docker container.

Attributes:

Name Type Description
name

The name to give to created Docker containers.

command

The command executed in created Docker containers to kick off flow run execution.

env

The environment variables to set in created Docker containers.

labels

The labels to set on created Docker containers.

image str

The image reference of a container image to use for created jobs. If not set, the latest Prefect image will be used.

image_pull_policy Optional[Literal['IfNotPresent', 'Always', 'Never']]

The image pull policy to use when pulling images.

networks List[str]

Docker networks that created containers should be connected to.

network_mode Optional[str]

The network mode for the created containers (e.g. host, bridge). If 'networks' is set, this cannot be set.

auto_remove bool

If set, containers will be deleted on completion.

volumes List[str]

Docker volumes that should be mounted in created containers.

stream_output bool

If set, the output from created containers will be streamed to local standard output.

mem_limit Optional[str]

Memory limit of created containers. Accepts a value with a unit identifier (e.g. 100000b, 1000k, 128m, 1g.) If a value is given without a unit, bytes are assumed.

memswap_limit Optional[str]

Total memory (memory + swap), -1 to disable swap. Should only be set if mem_limit is also set. If mem_limit is set, this defaults to allowing the container to use as much swap as memory. For example, if mem_limit is 300m and memswap_limit is not set, containers can use 600m in total of memory and swap.

privileged bool

Give extended privileges to created containers.

Source code in prefect_docker/worker.py
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
class DockerWorkerJobConfiguration(BaseJobConfiguration):
    """
    Configuration class used by the Docker worker.

    An instance of this class is passed to the Docker worker's `run` method
    for each flow run. It contains all the information necessary to execute the
    flow run as a Docker container.

    Attributes:
        name: The name to give to created Docker containers.
        command: The command executed in created Docker containers to kick off
            flow run execution.
        env: The environment variables to set in created Docker containers.
        labels: The labels to set on created Docker containers.
        image: The image reference of a container image to use for created jobs.
            If not set, the latest Prefect image will be used.
        image_pull_policy: The image pull policy to use when pulling images.
        networks: Docker networks that created containers should be connected to.
        network_mode: The network mode for the created containers (e.g. host, bridge).
            If 'networks' is set, this cannot be set.
        auto_remove: If set, containers will be deleted on completion.
        volumes: Docker volumes that should be mounted in created containers.
        stream_output: If set, the output from created containers will be streamed
            to local standard output.
        mem_limit: Memory limit of created containers. Accepts a value
            with a unit identifier (e.g. 100000b, 1000k, 128m, 1g.) If a value is
            given without a unit, bytes are assumed.
        memswap_limit: Total memory (memory + swap), -1 to disable swap. Should only be
            set if `mem_limit` is also set. If `mem_limit` is set, this defaults to
            allowing the container to use as much swap as memory. For example, if
            `mem_limit` is 300m and `memswap_limit` is not set, containers can use
            600m in total of memory and swap.
        privileged: Give extended privileges to created containers.
    """

    image: str = Field(
        default_factory=get_prefect_image_name,
        description="The image reference of a container image to use for created jobs. "
        "If not set, the latest Prefect image will be used.",
        example="docker.io/prefecthq/prefect:2-latest",
    )
    registry_credentials: Optional[DockerRegistryCredentials] = Field(
        default=None,
        description="Credentials for logging into a Docker registry to pull"
        " images from.",
    )
    image_pull_policy: Optional[Literal["IfNotPresent", "Always", "Never"]] = Field(
        default=None,
        description="The image pull policy to use when pulling images.",
    )
    networks: List[str] = Field(
        default_factory=list,
        description="Docker networks that created containers should be connected to.",
    )
    network_mode: Optional[str] = Field(
        default=None,
        description=(
            "The network mode for the created containers (e.g. host, bridge). If"
            " 'networks' is set, this cannot be set."
        ),
    )
    auto_remove: bool = Field(
        default=False,
        description="If set, containers will be deleted on completion.",
    )
    volumes: List[str] = Field(
        default_factory=list,
        description="A list of volume to mount into created containers.",
        example=["/my/local/path:/path/in/container"],
    )
    stream_output: bool = Field(
        default=True,
        description=(
            "If set, the output from created containers will be streamed to local "
            "standard output."
        ),
    )
    mem_limit: Optional[str] = Field(
        default=None,
        title="Memory Limit",
        description=(
            "Memory limit of created containers. Accepts a value "
            "with a unit identifier (e.g. 100000b, 1000k, 128m, 1g.) "
            "If a value is given without a unit, bytes are assumed."
        ),
    )
    memswap_limit: Optional[str] = Field(
        default=None,
        title="Memory Swap Limit",
        description=(
            "Total memory (memory + swap), -1 to disable swap. Should only be "
            "set if `mem_limit` is also set. If `mem_limit` is set, this defaults to"
            "allowing the container to use as much swap as memory. For example, if "
            "`mem_limit` is 300m and `memswap_limit` is not set, containers can use "
            "600m in total of memory and swap."
        ),
    )

    privileged: bool = Field(
        default=False,
        description="Give extended privileges to created container.",
    )

    @validator("volumes")
    def _validate_volume_format(cls, volumes):
        """Validates that provided volume strings are in the correct format."""
        for volume in volumes:
            if ":" not in volume:
                raise ValueError(
                    "Invalid volume specification. "
                    f"Expected format 'path:container_path', but got {volume!r}"
                )

        return volumes

    def _convert_labels_to_docker_format(self, labels: Dict[str, str]):
        """Converts labels to the format expected by Docker."""
        labels = labels or {}
        new_labels = {}
        for name, value in labels.items():
            if "/" in name:
                namespace, key = name.split("/", maxsplit=1)
                new_namespace = ".".join(reversed(namespace.split(".")))
                new_labels[f"{new_namespace}.{key}"] = value
            else:
                new_labels[name] = value
        return new_labels

    def _slugify_container_name(self) -> Optional[str]:
        """
        Generates a container name to match the configured name, ensuring it is Docker
        compatible.
        """
        # Must match `/?[a-zA-Z0-9][a-zA-Z0-9_.-]+` in the end
        if not self.name:
            return None

        return (
            slugify(
                self.name,
                lowercase=False,
                # Docker does not limit length but URL limits apply eventually so
                # limit the length for safety
                max_length=250,
                # Docker allows these characters for container names
                regex_pattern=r"[^a-zA-Z0-9_.-]+",
            ).lstrip(
                # Docker does not allow leading underscore, dash, or period
                "_-."
            )
            # Docker does not allow 0 character names so cast to null if the name is
            # empty after slufification
            or None
        )

    def _base_environment(self):
        """
        If the API URL has been set update the value to ensure connectivity
        when using a bridge network by updating local connections to use the
        docker internal host unless the network mode is "host" where localhost
        is available already.
        """

        base_env = super()._base_environment()
        network_mode = self.get_network_mode()
        if (
            "PREFECT_API_URL" in base_env
            and base_env["PREFECT_API_URL"] is not None
            and network_mode != "host"
        ):
            base_env["PREFECT_API_URL"] = (
                base_env["PREFECT_API_URL"]
                .replace("localhost", "host.docker.internal")
                .replace("127.0.0.1", "host.docker.internal")
            )
        return base_env

    def prepare_for_flow_run(
        self,
        flow_run: "FlowRun",
        deployment: Optional["DeploymentResponse"] = None,
        flow: Optional["Flow"] = None,
    ):
        """
        Prepares the agent for a flow run by setting the image, labels, and name
        attributes.
        """
        super().prepare_for_flow_run(flow_run, deployment, flow)

        self.image = self.image or get_prefect_image_name()
        self.labels = self._convert_labels_to_docker_format(
            {**self.labels, **CONTAINER_LABELS}
        )
        self.name = self._slugify_container_name()

    def get_network_mode(self) -> Optional[str]:
        """
        Returns the network mode to use for the container based on the configured
        options and the platform.
        """
        # User's value takes precedence; this may collide with the incompatible options
        # mentioned below.
        if self.network_mode:
            if sys.platform != "linux" and self.network_mode == "host":
                warnings.warn(
                    f"{self.network_mode!r} network mode is not supported on platform "
                    f"{sys.platform!r} and may not work as intended."
                )
            return self.network_mode

        # Network mode is not compatible with networks or ports (we do not support ports
        # yet though)
        if self.networks:
            return None

        # Check for a local API connection
        api_url = self.env.get("PREFECT_API_URL", PREFECT_API_URL.value())

        if api_url:
            try:
                _, netloc, _, _, _, _ = urllib.parse.urlparse(api_url)
            except Exception as exc:
                warnings.warn(
                    f"Failed to parse host from API URL {api_url!r} with exception: "
                    f"{exc}\nThe network mode will not be inferred."
                )
                return None

            host = netloc.split(":")[0]

            # If using a locally hosted API, use a host network on linux
            if sys.platform == "linux" and (host == "127.0.0.1" or host == "localhost"):
                return "host"

        # Default to unset
        return None

    def get_extra_hosts(self, docker_client) -> Optional[Dict[str, str]]:
        """
        A host.docker.internal -> host-gateway mapping is necessary for communicating
        with the API on Linux machines. Docker Desktop on macOS will automatically
        already have this mapping.
        """
        if sys.platform == "linux" and (
            # Do not warn if the user has specified a host manually that does not use
            # a local address
            "PREFECT_API_URL" not in self.env
            or re.search(
                ".*(localhost)|(127.0.0.1)|(host.docker.internal).*",
                self.env["PREFECT_API_URL"],
            )
        ):
            user_version = packaging.version.parse(
                format_outlier_version_name(docker_client.version()["Version"])
            )
            required_version = packaging.version.parse("20.10.0")

            if user_version < required_version:
                warnings.warn(
                    "`host.docker.internal` could not be automatically resolved to"
                    " your local ip address. This feature is not supported on Docker"
                    f" Engine v{user_version}, upgrade to v{required_version}+ if you"
                    " encounter issues."
                )
                return {}
            else:
                # Compatibility for linux -- https://github.com/docker/cli/issues/2290
                # Only supported by Docker v20.10.0+ which is our minimum recommend
                # version
                return {"host.docker.internal": "host-gateway"}

    def _determine_image_pull_policy(self) -> ImagePullPolicy:
        """
        Determine the appropriate image pull policy.

        1. If they specified an image pull policy, use that.

        2. If they did not specify an image pull policy and gave us
           the "latest" tag, use ImagePullPolicy.always.

        3. If they did not specify an image pull policy and did not
           specify a tag, use ImagePullPolicy.always.

        4. If they did not specify an image pull policy and gave us
           a tag other than "latest", use ImagePullPolicy.if_not_present.

        This logic matches the behavior of Kubernetes.
        See:https://kubernetes.io/docs/concepts/containers/images/#imagepullpolicy-defaulting
        """
        if not self.image_pull_policy:
            _, tag = parse_image_tag(self.image)
            if tag == "latest" or not tag:
                return ImagePullPolicy.ALWAYS
            return ImagePullPolicy.IF_NOT_PRESENT
        return ImagePullPolicy(self.image_pull_policy)

Functions

get_extra_hosts

A host.docker.internal -> host-gateway mapping is necessary for communicating with the API on Linux machines. Docker Desktop on macOS will automatically already have this mapping.

Source code in prefect_docker/worker.py
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
def get_extra_hosts(self, docker_client) -> Optional[Dict[str, str]]:
    """
    A host.docker.internal -> host-gateway mapping is necessary for communicating
    with the API on Linux machines. Docker Desktop on macOS will automatically
    already have this mapping.
    """
    if sys.platform == "linux" and (
        # Do not warn if the user has specified a host manually that does not use
        # a local address
        "PREFECT_API_URL" not in self.env
        or re.search(
            ".*(localhost)|(127.0.0.1)|(host.docker.internal).*",
            self.env["PREFECT_API_URL"],
        )
    ):
        user_version = packaging.version.parse(
            format_outlier_version_name(docker_client.version()["Version"])
        )
        required_version = packaging.version.parse("20.10.0")

        if user_version < required_version:
            warnings.warn(
                "`host.docker.internal` could not be automatically resolved to"
                " your local ip address. This feature is not supported on Docker"
                f" Engine v{user_version}, upgrade to v{required_version}+ if you"
                " encounter issues."
            )
            return {}
        else:
            # Compatibility for linux -- https://github.com/docker/cli/issues/2290
            # Only supported by Docker v20.10.0+ which is our minimum recommend
            # version
            return {"host.docker.internal": "host-gateway"}
get_network_mode

Returns the network mode to use for the container based on the configured options and the platform.

Source code in prefect_docker/worker.py
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
def get_network_mode(self) -> Optional[str]:
    """
    Returns the network mode to use for the container based on the configured
    options and the platform.
    """
    # User's value takes precedence; this may collide with the incompatible options
    # mentioned below.
    if self.network_mode:
        if sys.platform != "linux" and self.network_mode == "host":
            warnings.warn(
                f"{self.network_mode!r} network mode is not supported on platform "
                f"{sys.platform!r} and may not work as intended."
            )
        return self.network_mode

    # Network mode is not compatible with networks or ports (we do not support ports
    # yet though)
    if self.networks:
        return None

    # Check for a local API connection
    api_url = self.env.get("PREFECT_API_URL", PREFECT_API_URL.value())

    if api_url:
        try:
            _, netloc, _, _, _, _ = urllib.parse.urlparse(api_url)
        except Exception as exc:
            warnings.warn(
                f"Failed to parse host from API URL {api_url!r} with exception: "
                f"{exc}\nThe network mode will not be inferred."
            )
            return None

        host = netloc.split(":")[0]

        # If using a locally hosted API, use a host network on linux
        if sys.platform == "linux" and (host == "127.0.0.1" or host == "localhost"):
            return "host"

    # Default to unset
    return None
prepare_for_flow_run

Prepares the agent for a flow run by setting the image, labels, and name attributes.

Source code in prefect_docker/worker.py
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
def prepare_for_flow_run(
    self,
    flow_run: "FlowRun",
    deployment: Optional["DeploymentResponse"] = None,
    flow: Optional["Flow"] = None,
):
    """
    Prepares the agent for a flow run by setting the image, labels, and name
    attributes.
    """
    super().prepare_for_flow_run(flow_run, deployment, flow)

    self.image = self.image or get_prefect_image_name()
    self.labels = self._convert_labels_to_docker_format(
        {**self.labels, **CONTAINER_LABELS}
    )
    self.name = self._slugify_container_name()

DockerWorkerResult

Bases: BaseWorkerResult

Contains information about a completed Docker container

Source code in prefect_docker/worker.py
367
368
class DockerWorkerResult(BaseWorkerResult):
    """Contains information about a completed Docker container"""

ImagePullPolicy

Bases: Enum

Enum representing the image pull policy options for a Docker container.

Source code in prefect_docker/worker.py
62
63
64
65
66
67
class ImagePullPolicy(enum.Enum):
    """Enum representing the image pull policy options for a Docker container."""

    IF_NOT_PRESENT = "IfNotPresent"
    ALWAYS = "Always"
    NEVER = "Never"