<@ULVA73B9P> how can I set a work pool's docker cr...
# ask-marvin
m
@Marvin how can I set a work pool's docker credentials through the python sdk?
m
thought for 879.4 seconds
m
have you given up or are you still researching?
m
Assuming Prefect 3.x: set the Docker registry creds on a Docker work pool by attaching a DockerRegistryCredentials block to the pool’s default job variables under the key
registry_credentials
. Here’s a complete example using the Python SDK:
Copy code
from prefect.client.orchestration import get_client
from prefect.client.schemas.actions import WorkPoolUpdate
from prefect_docker.credentials import DockerRegistryCredentials

# 1) Create/save the Docker registry credentials as a block
creds = DockerRegistryCredentials(
    registry_url="<http://ghcr.io|ghcr.io>",   # e.g. <http://ghcr.io|ghcr.io>, <http://index.docker.io|index.docker.io>, <http://your-registry.example.com|your-registry.example.com>
    username="my-user",
    password="my-password",
)
block_document_id = creds.save("my-ghcr-creds", overwrite=True)  # returns the block doc id

# 2) Attach the block to your work pool's default job variables
pool_name = "my-docker-pool"

async def set_pool_creds():
    async with get_client() as client:
        pool = await client.read_work_pool(pool_name)
        variables = dict(pool.variables or {})
        variables["registry_credentials"] = {"block_document_id": str(block_document_id)}
        await client.update_work_pool(
            work_pool_name=pool_name,
            work_pool=WorkPoolUpdate(variables=variables),
        )

import asyncio
asyncio.run(set_pool_creds())
Notes: - The key you need to set is
registry_credentials
. You can confirm this by inspecting the default Docker job template; it includes
registry_credentials
in the job configuration and variable schema. - You can also inline credentials (not recommended for secrets). If you must, set:
Copy code
variables["registry_credentials"] = {
      "registry_url": "<http://ghcr.io|ghcr.io>",
      "username": "my-user",
      "password": "my-password",
  }
Using a block is preferred so your password is stored as a secret. - After updating pool defaults, new runs read them automatically; if you have a long-running worker, a quick restart ensures it picks up changes. Relevant references: - Default Docker work pool template (shows the
registry_credentials
variable): run
prefect work-pool get-default-base-job-template --type docker
- Docker registry credentials block class docs: DockerRegistryCredentials - Related discussion about seeing the credentials section in the UI for Docker pools (confirms the feature): GitHub issue #18490 If you share your pool name and registry (Docker Hub, GHCR, ECR, etc.), I can tailor the snippet to your setup.
m
Traceback (most recent call last): File "/home/mag/projects/test-prefect-workflow/wtf.py", line 27, in <module> asyncio.run(set_pool_creds()) File "/home/mag/.asdf/installs/python/3.12.11/lib/python3.12/asyncio/runners.py", line 195, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "/home/mag/.asdf/installs/python/3.12.11/lib/python3.12/asyncio/runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/mag/.asdf/installs/python/3.12.11/lib/python3.12/asyncio/base_events.py", line 691, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "/home/mag/projects/test-prefect-workflow/wtf.py", line 18, in set_pool_creds variables = dict(pool.variables or {}) ^^^^^^^^^^^^^^ File "/home/mag/.cache/pypoetry/virtualenvs/test-prefect-workflow-s8r5PE5C-py3.12/lib/python3.12/site-packages/pydantic/main.py", line 1026, in getattr raise AttributeError(f'{type(self).name!r} object has no attribute {item!r}') AttributeError: 'WorkPool' object has no attribute 'variables'
m
@Manuel Gomes did you eventually figure out a way? Or; do you know a way to do this with environment variables?