I'm unable to shut down Docker containers that were launched by supervisor through supervisorctl stop all. Even through supervisorctl status shows the containers are down, docker ps and ps indicate that they are in fact still running.
Consulting the supervisor documentation on the action for supervisorctl stop <name> reveals that SIGTERM is sent to processes followed by SIGKILL if still running after some grace period. I tried to do this manually and found that
SIGTERMsent to adocker runprocess doesn't do anythingSIGKILLdoes kill the process, but doesn't actually update docker.docker psshows this container is still running- Supervisor's
SIGKILLdoesn't shut down the container
The questions is: How do I properly shut down a Docker container by supervisor?
Here's the result of my experiments simulating supervisor:
Starting position: foo-1 and bar-1 are running (I left the GCE containers in, in case they make a difference). ps aux and docker ps are in sync.
me@devenv:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ba70bf8937f me/app:foo "/bin/sh -c 'supervi 5 minutes ago Up 5 minutes foo-1
e1a684bcfceb me/app:bar "/bin/sh -c 'supervi 5 minutes ago Up 5 minutes bar-1
fce5db0517df google/cadvisor:0.8.0 "/usr/bin/cadvisor" 35 minutes ago Up 35 minutes bbbb
db677eed47ef kubernetes/pause:go "/pause" 35 minutes ago Up 35 minutes 0.0.0.0:4194->8080/tcp aaaa
me@devenv:~$ ps aux | grep "docker run"
root 23358 0.0 0.1 124092 11856 pts/0 Sl 02:05 0:00 docker run --rm --name foo-1 ... -i me/app:foo
root 23365 0.0 0.1 124092 11928 pts/0 Sl 02:05 0:00 docker run --rm --name bar-1 ... -i me/app:bar
Simulate supervisorctl stop foo-1 by sending SIGTERM to the process. Result: process still active.
me@devenv:~$ sudo kill -SIGTERM 23358
... <waiting> ...
me@devenv:~$ ps aux | grep "docker run"
root 23358 0.0 0.1 124092 11856 pts/0 Sl 02:05 0:00 docker run --rm --name foo-1 ... -i me/app:foo
root 23365 0.0 0.1 124092 11928 pts/0 Sl 02:05 0:00 docker run --rm --name bar-1 ... -i me/app:bar
me@devenv:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ba70bf8937f me/app:foo "/bin/sh -c 'supervi 6 minutes ago Up 6 minutes foo-1
e1a684bcfceb me/app:bar "/bin/sh -c 'supervi 6 minutes ago Up 6 minutes bar-1
fce5db0517df google/cadvisor:0.8.0 "/usr/bin/cadvisor" 36 minutes ago Up 36 minutes bbbb
db677eed47ef kubernetes/pause:go "/pause" 36 minutes ago Up 36 minutes 0.0.0.0:4194->8080/tcp aaaa
Next thing supervisor would do is issuing SIGKILL. Result: Process is killed (ps aux), but still showing as running docker process (docker ps).
me@devenv:~$ sudo kill -SIGKILL 23358
me@devenv:~$ ps aux | grep "docker run"
root 23365 0.0 0.1 124092 11928 pts/0 Sl 02:05 0:00 docker run --rm --name bar-1 ... -i me/app:bar
me@devenv:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ba70bf8937f me/app:foo "/bin/sh -c 'supervi 19 minutes ago Up 19 minutes foo-1
e1a684bcfceb me/app:bar "/bin/sh -c 'supervi 19 minutes ago Up 19 minutes bar-1
fce5db0517df google/cadvisor:0.8.0 "/usr/bin/cadvisor" 49 minutes ago Up 49 minutes bbbb
db677eed47ef kubernetes/pause:go "/pause" 49 minutes ago Up 49 minutes 0.0.0.0:4194->8080/tcp aaaa
Supervisor was shut down during the above experiment (to avoid its autostart behaviour to interfere). The result for explicitly sending SIGKILL to the process could not be achieved by supervisor; the process was still alive (even though the supervisor logs state otherwise).
docker stop <container_id> did however stop the container.
Update
Inside the Docker containers is also a supervisord process running which manages certain processes. Perhaps the problem is that signals aren't propagated and thus it won't shut down...
Update 2
I narrowed down the problem. I was able to directly start a containers process from the Dockerfile instead of going through starting supervisord and it makes a difference. I'm able to control this container through supervisor (the one outside of the docker containers which controls the containers).
Update 3
Setting stopasgroup=true as suggested here does not change anything for me.
Update 4
I was able to solve one of the problems: supervisorctl not being able to shut down the process. The problem was that I was launching the docker containers in the supervisor configuration file with command=sudo docker run... which created a sudo docker run... and a docker run... process. supervisorctl stop... just terminated the sudo docker run... process while the actual docker process was still running. When I omit the sudo command, only 1 process per supervisor program is started and supervisorctl stop terminates the process.
One problem remains which is that docker ps shows the container is still running while ps aux doesn't. Oddly enough the containers still seem to be active as they respond to requests. A quick look at the process list confirms that all processes spawned by the docker container are still active, yet the docker run... process is missing from the process list.
Update 5
Sending SIGTERM, SIGHUP or SIGQUIT to the docker run process doesn't seem to do anything to the process. Only SIGKILL terminates the docker process properly. Supervisor gets updated properly, but docker ps still shows the docker process running.