Docker Community Forums

Share and learn in the Docker community.

How to kill/stop a container on SSH disconnect

I’m on ubuntu 18.04 and running a docker container on a remote host.

From my local machine I run the following to get a python GUI running on the container:

ssh -X -t user@remotehost docker exec -it container bash -ci ‘python’

This is with the assumption that the container had already been started previously:

touch $XAUTH
xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge -
chmod 777 $XAUTH
sudo docker run --name hb_cont --gpus all -it --privileged --network=host -e DISPLAY=$DISPLAY -v $XSOCK:$XSOCK -v $XAUTH:$XAUTH -e XAUTHORITY=$XAUTH  cont`

all the xauth stuff allows me to use X11 forwarding over ssh.

Now, when running the python GUI, if I close the terminal window, thus cutting the ssh connection, I find that the python script is still running. The same python code can pick up SIGHUP and kill itself, but SIGHUP isn’t getting passed.Alternatively I’m fine with SSH disconnect just stopping the container, since that kills the python code too.

Is either of those possible? How do I make either happen with my setup?

The final option for stopping a running container is to use the --force or -f flag in conjunction with the docker rm command. Typically, docker rm is used to remove an already stopped container, but the use of the -f flag will cause it to first issue a SIGKILL.