<@ULVA73B9P> from prefect import flow, get_run_log...
# ask-marvin
m
@Marvin 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 def docker_flow(): logger = get_run_logger() pull_docker_image("python:3.11-slim", "latest") container = create_docker_container( image="python:3.11-slim", command="echo 'hello world!' && sleep 60" ) start_docker_container(_container_id_=container.id) logs = get_docker_container_logs(_container_id_=container.id) logger.info(logs) stop_docker_container(_container_id_=container.id) remove_docker_container(_container_id_=container.id) return container When running this flow i get this: /app/wizard/flows/run_docker_image.py14 RuntimeWarning: coroutine 'run_task_async' was never awaited pull_docker_image("python:3.11-slim", "latest") RuntimeWarning: Enable tracemalloc to get the object allocation traceback 160907.566 | ERROR | Flow run 'brawny-loon' - Encountered exception during execution: AttributeError("'coroutine' object has no attribute 'id'") Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/prefect/flow_engine.py", line 782, in run_context yield self File "/usr/local/lib/python3.11/site-packages/prefect/flow_engine.py", line 1397, in run_flow_sync engine.call_flow_fn() File "/usr/local/lib/python3.11/site-packages/prefect/flow_engine.py", line 802, in call_flow_fn result = call_with_parameters(self.flow.fn, self.parameters) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/prefect/utilities/callables.py", line 210, in call_with_parameters return fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^ File "/app/wizard/flows/run_docker_image.py", line 18, in docker_flow start_docker_container(container_id=container.id) ^^^^^^^^^^^^ AttributeError: 'coroutine' object has no attribute 'id' 160907.637 | INFO | Flow run 'brawny-loon' - Finished in state Failed("Flow run encountered an exception: AttributeError: 'coroutine' object has no attribute 'id'") Traceback (most recent call last): File "/app/main.py", line 78, in test_job_endpoint docker_flow() File "/usr/local/lib/python3.11/site-packages/prefect/flows.py", line 1702, in call return run_flow( ^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/prefect/flow_engine.py", line 1554, in run_flow ret_val = run_flow_sync(**kwargs) ^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/prefect/flow_engine.py", line 1399, in run_flow_sync return engine.state if return_type == "state" else engine.result() ^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/prefect/flow_engine.py", line 361, in result
m
thought for 35.0 seconds
Based on the error message and my knowledge of
prefect-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:
Copy code
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:
Copy 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!