0

I am running multiple docker containers via python script. I already handled to kill all dockers when gracefully (cltr+c) shutdown the python script but when it's forced killed, the docker remains running.

Question is, is there a way to kill the docker containers if the python script is killed ungracefully? (Perhaps setting the python script as parent process to the docker process? Is there such thing?)

2
  • Why do you use a python script and not docker-compose?
    – Panki
    Jun 3, 2020 at 7:44
  • I have a service using docker. the python serves as the service manager that runs multiple docker-compose via bash.
    – ACD
    Jun 3, 2020 at 7:47

1 Answer 1

0

Anyone having the same problem, this might help.

libc = ctypes.CDLL("libc.so.6")
def set_pdeathsig(sig=signal.SIGTERM):
    def callback():
        return libc.prctl(1, sig)
    return callback

open_container_cmd = "docker-compose run --rm test" # cannot use detach <-d>
proc = subprocess.Popen(
                open_container_cmd,
                shell=True,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                stdin=subprocess.PIPE,
                preexec_fn=set_pdeathsig(signal.SIGTERM))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.