<@ULVA73B9P>, how do I pass container_create_kwarg...
# ask-marvin
t
@Marvin, how do I pass container_create_kwargs to the creation of the container that the flow is executed. I use a python deployment scirpt with from_srouce().deploy
m
thought for 253.7 seconds
Great question. Assuming you’re running your flow on a Docker work pool in Prefect 3.x, you can pass Docker container creation options via the deployment’s
job_variables
, using the
container_create_kwargs
key. These values get forwarded to docker-py’s container create call. Example using a Python deployment with `Flow.from_source(...).deploy(...)`:
Copy code
from prefect.flows import Flow

flow = Flow.from_source(
    source="<https://github.com/owner/repo>",
    entrypoint="path/to/flows.py:my_flow",
)

flow.deploy(
    name="my-docker-deployment",
    work_pool_name="my-docker-pool",
    image="myrepo/myimage:latest",
    job_variables={
        # Common top-level Docker worker variables
        "env": {"PREFECT_LOGGING_LEVEL": "DEBUG"},
        "volumes": ["/host/data:/container/data:ro"],  # prefer this over putting volumes in create kwargs
        "networks": ["my-network"],
        "auto_remove": True,
        "image_pull_policy": "ALWAYS",

        # Additional docker-py create kwargs passed through as-is
        "container_create_kwargs": {
            # examples — set what you need here
            "extra_hosts": {"host.docker.internal": "host-gateway"},
            "shm_size": "2g",
            "ulimits": [{"name": "nofile", "soft": 65536, "hard": 65536}],
            "network_mode": "bridge",
            # GPU example (docker-py accepts dicts here)
            "device_requests": [
                {"Driver": "nvidia", "Count": -1, "Capabilities": [["gpu"]]}
            ],
            # other supported docker create options can go here
        },
    },
)
Notes and tips: - Set pool-wide defaults in the Docker work pool’s base job template (via the UI). Any
job_variables
you pass in
deploy(...)
will override those defaults for this deployment. - Prefer the top-level
volumes
job variable instead of putting
volumes
inside
container_create_kwargs
(Prefect warns if you do both). See the related change note: PR #18233. - There is a
container_create_kwargs
field on the Docker worker job variables; it’s meant for anything not covered by the top-level keys. - Related discussion on customizing Docker worker container parameters: Issue #18544. - General guidance on customizing job variables: Customize job variables. If you’re not using the Docker worker (e.g., Kubernetes worker or Process), this field won’t apply. Can you confirm which worker type you’re using and what specific container options you need to set? I can tailor an example.
t
@Marvin It seems that the args don't get to the worker. is there any open issue related?
m
thought for 50.8 seconds
Thanks for the details. Short answer: I’m not seeing an open issue specifically about Docker workers ignoring `job_variables`/`container_create_kwargs` when using
Flow.from_source(...).deploy(...)
. Closest related items I can find: - General request to customize Docker worker container parameters (mentions
container_create_kwargs
and GPUs): #18544 - Note about overlap between
volumes
and
container_create_kwargs
(and a warning logged if volumes are set in both): PR #18233 - CLI-only bug where
--all
could drop
job_variables
(not your case since you’re deploying in Python): #17110 To help pinpoint why your args aren’t reaching the Docker worker, could you share: - Your Prefect version and how you’re running the worker (command and version) - The exact
deploy(...)
snippet (especially your
job_variables
) - Worker logs from a run where you expected the options to apply Quick checks you can try now: 1) Confirm the deployment actually has your job variables - In the UI: open the deployment and check the “Job variables/Run config” section - Note: deployment job variables override the work pool’s base job template; workers do not override deployments 2) Use a simple canary var to prove job vars are applied
Copy code
from prefect import flow
import os

@flow
def my_flow():
    print("FOO=", os.environ.get("FOO"))

flow = flow.from_source(
    "<https://github.com/owner/repo>",
    "path/to/flows.py:my_flow",
)

flow.deploy(
    name="canary",
    work_pool_name="my-docker-pool",
    job_variables={
        "env": {"FOO": "BAR"},
        "container_create_kwargs": {"shm_size": "1g"},
    },
)
- Run it and check the log prints FOO=BAR. If not, the variables aren’t making it through. 3) Enable debug logs on the worker - Start the worker with
PREFECT_LOGGING_LEVEL=DEBUG
and watch for the rendered job payload the worker uses to create the container. It will show the values it’s sending to Docker. 4) Validate key usage and nesting - Top-level recommended keys for Docker worker include:
image
,
env
,
volumes
,
networks
,
network_mode
,
auto_remove
,
image_pull_policy
,
registry_credentials
,
privileged
, etc. - Use
volumes
at the top level (don’t also put volumes under `container_create_kwargs`; see PR #18233) - Use
container_create_kwargs
only for extras like
extra_hosts
,
shm_size
,
ulimits
,
device_requests
, etc. - Be aware that merges may be shallow: setting
container_create_kwargs
at the deployment may overwrite (not merge) a dict set at the pool level. Docs to help: - Customize job variables: Customize job variables If you share your version, deployment snippet, and a redacted worker log line showing the job payload, I can help confirm whether this is a bug or a key/structure issue—and open an issue if needed.