1

I have a python script as a "service manager" to run multiple docker containers. I already handled graceful shutdown (cltr+c). However when I pkill -9 the python script, the dockers are still running.

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 -d test"
proc = subprocess.Popen(
                open_container_cmd,
                shell=True,
                stdout=open(os.devnull, 'wb'),
                preexec_fn=set_pdeathsig(signal.SIGTERM))

I followed this answer but no luck. I even tried executing in terminal kill -9 pid to kill docker container with process ID from proc = subprocess.Popen(open_container_cmd); proc.pid but it won't even stop the container.

ACD
  • 1,431
  • 1
  • 8
  • 24
  • 1
    Remove the `-d` from the command. Or use a docker cimmand to stop the container. – Klaus D. Jun 04 '20 at 02:38
  • @KlausD. didn't work – ACD Jun 04 '20 at 02:47
  • Which? And how exactly did you implement it? – Klaus D. Jun 04 '20 at 02:48
  • i removed -d.. how do I "use a docker cimmand to stop the container"? – ACD Jun 04 '20 at 03:35
  • @KlausD. oh actually it's working. now the problem is the subprocess is interfering with the parent. Example I'm having weird spaces on my print lines and if i ctrl+c, the docker-compose reads it rather than the main script.. – ACD Jun 04 '20 at 05:07
  • @KlausD. its working, just have to update/add `stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE,`! Thanks man! – ACD Jun 04 '20 at 05:30

0 Answers0