pythondockerpy

Docker-py: Is there a way to pass a callback function to a container running in 'detach' mode?


I am running a container with docker-py in Python. Similar to the docs:

import docker
client = docker.from_env()
container = client.containers.run('bfirsh/reticulate-splines', detach=True)

The container performs some tasks and saves a .json file to a shared volume. Everything is working fine except that I do not know how to catch the container exit when running in the background. More specifically, my question is the following:

"Is there a way in docker-py to pass a callback function to a container running in detach mode?"

I want to access the saved .json on exit and avoid using ugly statements like time.sleep() or iterating while the status is running. From the docs it does not seem possible. Do you have any workaround to share?


Solution

  • Answering my own question, I adopted the following solution using threads.

    import docker
    import threading
    
    def run_docker():
        """ This function run a Docker container in detached mode and waits for completion. 
            Afterwards, it performs some tasks based on container's result.
        """
    
        docker_client = docker.from_env()
        container = docker_client.containers.run(image='bfirsh/reticulate-splines', detach=True)
    
        result = container.wait()
        container.remove()
    
        if result['StatusCode'] == 0:
            do_something()  # For example access and save container's log in a json file
        else:
            print('Error. Container exited with status code', result['StatusCode'])
    
    
    def main():
        """ This function start a thread to run the docker container and 
            immediately performs other tasks, while the thread runs in background.
        """
    
        threading.Thread(target=run_docker, name='run-docker').start()
        # Perform other tasks while the thread is running.