Skip to content

Commit

Permalink
Add container_create_kwargs to Docker worker (#14686)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingrismore authored Jul 22, 2024
1 parent e1aa312 commit 9e72a9f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/integrations/prefect-docker/prefect_docker/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class DockerWorkerJobConfiguration(BaseJobConfiguration):
`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.
container_create_kwargs: Extra args for docker py when creating container.
"""

image: str = Field(
Expand Down Expand Up @@ -186,11 +187,17 @@ class DockerWorkerJobConfiguration(BaseJobConfiguration):
"600m in total of memory and swap."
),
)

privileged: bool = Field(
default=False,
description="Give extended privileges to created container.",
)
container_create_kwargs: Optional[Dict[str, Any]] = Field(
default=None,
title="Container Configuration",
description=(
"Configuration for containers created by workers. See the [`docker-py` documentation](https://docker-py.readthedocs.io/en/stable/containers.html) for accepted values."
),
)

def _convert_labels_to_docker_format(self, labels: Dict[str, str]):
"""Converts labels to the format expected by Docker."""
Expand Down Expand Up @@ -488,6 +495,18 @@ def _build_container_settings(
) -> Dict:
"""Builds a dictionary of container settings to pass to the Docker API."""
network_mode = configuration.get_network_mode()

container_create_kwargs = (
configuration.container_create_kwargs
if configuration.container_create_kwargs
else {}
)
container_create_kwargs = {
k: v
for k, v in container_create_kwargs.items()
if k not in configuration.model_fields.keys()
}

return dict(
image=configuration.image,
network=configuration.networks[0] if configuration.networks else None,
Expand All @@ -502,6 +521,7 @@ def _build_container_settings(
mem_limit=configuration.mem_limit,
memswap_limit=configuration.memswap_limit,
privileged=configuration.privileged,
**container_create_kwargs,
)

def _create_and_start_container(
Expand Down
31 changes: 31 additions & 0 deletions src/integrations/prefect-docker/tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,37 @@ async def test_task_infra_pid_includes_host_and_container_id(
assert result.identifier == f"{FAKE_BASE_URL}:{FAKE_CONTAINER_ID}"


async def test_container_create_kwargs(
mock_docker_client, flow_run, default_docker_worker_job_configuration
):
default_docker_worker_job_configuration.container_create_kwargs = {
"hostname": "custom_name"
}
async with DockerWorker(work_pool_name="test") as worker:
await worker.run(
flow_run=flow_run, configuration=default_docker_worker_job_configuration
)
mock_docker_client.containers.create.assert_called_once()
hostname = mock_docker_client.containers.create.call_args[1].get("hostname")
assert hostname == "custom_name"


async def test_container_create_kwargs_excludes_job_variables(
mock_docker_client, flow_run, default_docker_worker_job_configuration
):
default_docker_worker_job_configuration.name = "job_config_name"
default_docker_worker_job_configuration.container_create_kwargs = {
"name": "create_kwarg_name"
}
async with DockerWorker(work_pool_name="test") as worker:
await worker.run(
flow_run=flow_run, configuration=default_docker_worker_job_configuration
)
mock_docker_client.containers.create.assert_called_once()
name = mock_docker_client.containers.create.call_args[1].get("name")
assert name == "job_config_name"


async def test_task_status_receives_result_identifier(
mock_docker_client, flow_run, default_docker_worker_job_configuration
):
Expand Down

0 comments on commit 9e72a9f

Please sign in to comment.