-
Notifications
You must be signed in to change notification settings - Fork 13
How do we wait for container to exit on its own? #33
Comments
@jpuris Thank for you raising this issue! At the moment, you may hold onto the container and call the
|
Seems to be working! 🎉 Is there any way to retrieve the exit code / error code and/or status from container object? |
Perhaps |
I suppose the |
Thanks! I'll check it out and get back with an update. |
✅ Solved!
logger.info("Waiting for docker container to exit")
container_status = container.wait()
if container_status['StatusCode']:
return Failed(message=f"Docker container has exited with an error! "
f"Returned status code '{container_status['StatusCode']}' "
f"with error '{container_status['Error']}'") Full MRE from prefect import flow, get_run_logger
from prefect.states import Failed
from prefect_docker.containers import (
create_docker_container,
start_docker_container,
get_docker_container_logs,
remove_docker_container,
)
from prefect_docker.images import pull_docker_image
@flow
def prefect_docker_test():
logger = get_run_logger()
docker_image = 'python:alpine3.16'
logger.info(f"Pulling docker image {docker_image}")
pull_docker_image(
repository=docker_image.split(':')[0],
tag=docker_image.split(':')[1]
)
logger.info(f"Starting docker container from '{docker_image}' image")
container = create_docker_container(
image=docker_image,
command="python -c 'import does_not_exist'"
)
start_docker_container(container_id=container.id)
logger.info("Waiting for docker container to exit")
container_status = container.wait()
if container_status['StatusCode']:
logger.error(container.status)
return Failed(message=f"Docker container has exited with an error! "
f"Returned status code '{container_status['StatusCode']}' "
f"with error '{container_status['Error']}'")
logs = get_docker_container_logs(container_id=container.id)
if not logs:
logger.warning('Docker container did not emit any log entries')
else:
logger.info('Docker container logs:')
logger.info(logs)
logger.info("Removing docker container")
remove_docker_container(container_id=container.id)
if __name__ == "__main__":
prefect_docker_test() would then output following logs
Since both of my inquiries have been solved, I'm closing this issue. Thank you very much, @ahuang11! |
Given a very simple use case: Prefect flow that runs a container that waits for 30 seconds (
sleep 30
), there seems to be no way for the flow or task to "wait" for the container to exit on its own.Minimally reproducible example
example_flow.py
What happens
What is expected to happen
Logs
As one can see, the "sleep 30" is still running, although the prefect task is done..
How do we have the prefect task "running" for the duration of the container in order to retrieve full logs of the container as well as its status for indication of success or failure?
The text was updated successfully, but these errors were encountered: