Docker Community Forums

Share and learn in the Docker community.

Killing a process inside a container other than PID1

Hi There,

Description: Ubuntu 20.04.2 LTS
Release: 20.04

i am running the container with /bin/bash command i.e PID1. I have installed MySQL and ngnix inside that container and the processes are started too.

root@docker-ubuntu:~# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ef65adf3f1d8 db25d2007d0f “/bin/bash” 2 hours ago Up About an hour happy_lumiere
root@docker-ubuntu:~#

root@docker-ubuntu:~# docker top ef65adf3f1d8
UID PID PPID C STIME TTY TIME CMD
root 3639 3613 0 18:57 ? 00:00:00 /bin/bash
root 3703 3639 0 18:57 ? 00:00:00 nginx: master process /usr/sbin/nginx
www-data 3704 3703 0 18:57 ? 00:00:00 nginx: worker process
www-data 3705 3703 0 18:57 ? 00:00:00 nginx: worker process
www-data 3706 3703 0 18:57 ? 00:00:00 nginx: worker process
www-data 3707 3703 0 18:57 ? 00:00:00 nginx: worker process
root 3748 3639 0 18:57 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe
systemd+ 4691 3748 0 19:38 ? 00:00:02 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
root@docker-ubuntu:~#

Now if i try to stop the container that doesn’t work:

root@docker-ubuntu:~# docker stop ef65adf3f1d8
Error response from daemon: cannot stop container: ef65adf3f1d8: Cannot kill container ef65adf3f1d89819c3ef84e128ce0631b951cbe83eb0e279fb2f1b5add99f5d9: unknown error after kill: runc did not terminate sucessfully: container_linux.go:392: signaling init process caused “permission denied”
: unknown
root@docker-ubuntu:~#

Now the reason behind its not stopping is that there are two processes i.e mysqld and ngnix are running. If docker tries to stop the PID1 i.e /bin/bash. It doesn’t work as others are running.

So i tried to kill those two process:
root@docker-ubuntu:~# docker exec ef65adf3f1d8 kill -9 3703
root@docker-ubuntu:~# docker exec ef65adf3f1d8 kill -9 4691
root@docker-ubuntu:~#

Conceptually this should work however it not working at all:

root@docker-ubuntu:~# docker exec ef65adf3f1d8 kill -9 3703
root@docker-ubuntu:~# docker exec ef65adf3f1d8 kill -9 4691
root@docker-ubuntu:~# docker top ef65adf3f1d8
UID PID PPID C STIME TTY TIME CMD
root 3639 3613 0 18:57 ? 00:00:00 /bin/bash
root 3703 3639 0 18:57 ? 00:00:00 nginx: master process /usr/sbin/nginx
www-data 3704 3703 0 18:57 ? 00:00:00 nginx: worker process
www-data 3705 3703 0 18:57 ? 00:00:00 nginx: worker process
www-data 3706 3703 0 18:57 ? 00:00:00 nginx: worker process
www-data 3707 3703 0 18:57 ? 00:00:00 nginx: worker process
root 3748 3639 0 18:57 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe
systemd+ 4691 3748 0 19:38 ? 00:00:03 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306

thought of killing the process from the host machine whose PID is 4691 i.e mysqld:

root@docker-ubuntu:~# kill -9 4691
root@docker-ubuntu:~# docker top ef65adf3f1d8
UID PID PPID C STIME TTY TIME CMD
root 3639 3613 0 18:57 ? 00:00:00 /bin/bash
root 3703 3639 0 18:57 ? 00:00:00 nginx: master process /usr/sbin/nginx
www-data 3704 3703 0 18:57 ? 00:00:00 nginx: worker process
www-data 3705 3703 0 18:57 ? 00:00:00 nginx: worker process
www-data 3706 3703 0 18:57 ? 00:00:00 nginx: worker process
www-data 3707 3703 0 18:57 ? 00:00:00 nginx: worker process
root 3748 3639 0 18:57 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe
systemd+ 5236 3748 2 20:28 ? 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
root@docker-ubuntu:~#

see process mysqld is there with another pid.

Can anyone help me with this how-to stop a container from the HOST machine without entering into the container shell?
Is there any way out?

Hi there,
Let me ask you a question.
Why are you using Bash as Init process?
You need build and image and then create the container based on that image.
Another thing is that, as best practice, must be one service per container.
For example, one for MySQL, and another one for NGinX with the site.
If you really need run two or more services in the same container. I recommend you to use Supervisord with Dumb-Init ( as initial process )

Here I let you some best practice to build an Image.

Thanks Manuel for your reply.

I am new to docker and learning it from nigelpoulton vedios/tutorials. Side by side i am doing some experiments beyond the course.
I would try Dumb-Init… for running multiple process. however dont know how to use it. Can you please send me any link where i can learn it. running multiple processes in a single container and then managing that container.

thanks again… cherrsssssssssssssssssss…