Dijar Vrella
07/09/2024, 9:59 PMMarvin
07/09/2024, 10:00 PMdocker logs --follow <container_id>
.
2. Kubernetes: Use Kubernetes commands to fetch logs after container termination, e.g., kubectl logs --tail=100 --since=1m <pod_name>
.
Feel free to explore integrating these with Prefect's task functionality to fit your specific needs.
For more detailed requirements or potential feature requests/contributions, you might want to follow the discussions on the related GitHub issues, such as this one.
Is there anything else you'd like to know or elaborate on?Dijar Vrella
07/09/2024, 10:46 PMMarvin
07/09/2024, 10:46 PMmy flow@flow async def pull_and_run_image(): logger = get_run_logger() logger.info("Loading Docker credentials...") # Load Docker credentials docker_registry_credentials = await DockerRegistryCredentials.load("seedoo-docker-registry") # Pull the image image = await pull_image(docker_registry_credentials) logger.info(f"Pulled image: {image}") # Create the container container = await create_container() logger.info(f"Created container: {container}") # Start the container started_container = await start_container(container.id) logger.info(f"Started container: {started_container}") # Start fetching logs in parallel log_task = asyncio.create_task(get_docker_container_logs_flow(container.id)) # Wait for the container to exit await wait_for_container_exit(container.id) # Cancel the log fetching task once the container has exited log_task.cancel() try: await log_task except asyncio.CancelledError: pass if name == "__main__": asyncio.run(pull_and_run_image()) @task async def get_docker_container_logs_flow(container_id): # Export out the container logs logger = get_run_logger() while True: logger.info(f"
Marvin
07/09/2024, 10:48 PMpython
from prefect import flow, task, get_run_logger
from prefect_docker import DockerRegistryCredentials, get_docker_container_logs, get_container
import asyncio
@task
async def get_docker_container_logs_flow(container_id):
logger = get_run_logger()
while True:
container_logs = await get_docker_container_logs(container_id=container_id)
<http://logger.info|logger.info>(f"Container logs: {container_logs}")
await asyncio.sleep(5)
@task
async def wait_for_container_exit(container_id):
logger = get_run_logger()
while True:
container = await get_container(container_id=container_id)
if container.status in ["exited", "dead"]:
break
await asyncio.sleep(5)
@flow
async def pull_and_run_image():
logger = get_run_logger()
docker_registry_credentials = await DockerRegistryCredentials.load("seedoo-docker-registry")
controller_container_id = "<YOUR_CONTAINER_ID>" # Replace with your container ID
log_task = asyncio.create_task(get_docker_container_logs_flow(controller_container_id))
await wait_for_container_exit(controller_container_id)
log_task.cancel()
try:
await log_task
except asyncio.CancelledError:
pass
if __name__ == "__main__":
asyncio.run(pull_and_run_image())
This version should ensure that your flow pulls the necessary docker image, runs the container, and captures logs until the container exits. Adjust the controller_container_id
placeholder accordingly.
If you have further questions or need additional refinements, feel free to ask!