Mohamed Zaki
08/06/2025, 4:12 PMMarvin
08/06/2025, 4:12 PMMarvin
08/06/2025, 4:13 PMprefect-docker
, the issue is that these Docker operations are asynchronous functions but they're being called in a synchronous context. Here's how to fix your code:
1. First, mark your flow as async
2. Use await
with all the Docker operations
Here's the corrected version:
python
from prefect import flow, get_run_logger
from prefect_docker.images import pull_docker_image
from prefect_docker.containers import (
create_docker_container,
start_docker_container,
get_docker_container_logs,
stop_docker_container,
remove_docker_container,
)
@flow
async def docker_flow():
logger = get_run_logger()
# Await all async operations
await pull_docker_image("python:3.11-slim", "latest")
container = await create_docker_container(
image="python:3.11-slim",
command="echo 'hello world!' && sleep 60"
)
await start_docker_container(container_id=container.id)
logs = await get_docker_container_logs(container_id=container.id)
<http://logger.info|logger.info>(logs)
await stop_docker_container(container_id=container.id)
await remove_docker_container(container_id=container.id)
return container
Key changes made:
1. Added async
keyword to the flow decorator
2. Added await
before each Docker operation
3. Fixed parameter names (removed underscores from image
and command
)
When calling this flow, you'll need to either:
1. Call it from another async function using await docker_flow()
2. Or use an async runner if calling from synchronous code:
python
from asyncio import run
run(docker_flow())
This should resolve the "coroutine was never awaited" warning and the AttributeError you were seeing. The error occurred because the coroutine wasn't being awaited, so container
was actually a coroutine object instead of the container result you were expecting.
Let me know if you need any clarification or run into other issues!