I'm using docker and my container is build over php:5.6-fpm image from php official repo. Is it somehow possible to restart/reload php-fpm from inside a container?
Asked
Active
Viewed 8.3k times
5 Answers
105
php-fpm
is a process manager which supports the USER2 signal, which is used to reload the config file.
From inside the container:
kill -USR2 1
Outside:
docker exec -it <mycontainer> kill -USR2 1
Complete example:
docker run -d --name test123 php:7.1-fpm-alpine
docker exec -it test123 ps aux
docker exec -it test123 kill -USR2 1
docker exec -it test123 ps aux

Enrico Stahn
- 2,967
- 1
- 23
- 23
-
Works great when I'm in the container. But calling if from outside errors: `rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:247: starting container process caused "exec: \"kill\": executable file not found in $PATH"` Any idea why? – panepeter Mar 12 '18 at 07:01
-
1@panepeter If you provide the way you run it I could provide more advice but the error message says it can't find the kill command in the path. Depending on your image you need to run it in certain ways, e.g. /bin/sh -c kill -USR2 1 ... That's my best guess without further info. – Enrico Stahn Mar 12 '18 at 21:48
-
Thanks @Encrico, you were right! `/bin/bash -c kill -USR2 1` did the trick! – panepeter Mar 13 '18 at 06:25
-
I had to use `kill -s USR2 1`. – Seth Apr 19 '18 at 08:12
-
What does 1 mean? How does know which process it kills? – Gherman Jun 28 '18 at 11:43
-
@Gherman `1` is the process ID (PID). As its the only process started in that image it will be always 1. – Enrico Stahn Aug 14 '18 at 07:32
-
12For me PID 1 is not always correct (especially after killing it once). What helps is `pkill -o -USR2 php-fpm`, because the option `-o` searches for the oldest process (the master) and kills it. – olidem Mar 19 '19 at 09:12
-
@olidem If you kill PID 1 in any way that actually kills the process (eg. the famous `kill -9`), a new process will be spawned with a new PID. This solution only works repeatedly if you use the `-USR2` flag since it doesn't actually kill the process, it just tells it to reload the config (similar to a restart as the original question asked for). – Joel Mellon Aug 04 '20 at 02:41
-
Interestingly enough, locally I could send SIGHUP to my php-fpm master process to restart it, but when working with php-fpm in docker I needed to use the USR2 signal as described in this answer for it to work. – Etienne Bruines May 16 '22 at 12:28
-
Handy one-liner: `pgrep php-fpm | xargs kill -USR2` – rangfu Jul 21 '22 at 10:15
5
You don't have to go inside the container
on your host
ps -ef|grep fpm // find master pid
kill -USR2 <master_pid>

too
- 51
- 1
- 4
-
You might also need to install the procps package (Ubuntu/Debian image) which contains the ps util – Peter Kionga-Kamau Apr 19 '22 at 16:01
-
PIDPHPFPM=$(ps -ef | grep fpm | grep master | awk '{print $2}') && kill -USR2 $PIDPHPFPM – Dado May 05 '23 at 02:21
0
This works for me:
If the command fpm restart fails run this inside the Docker container -> www#:
root@...:/var/www# **ps -ef|grep fpm**
www-data 160 1 0 10:02 ? 00:00:00 php-fpm: pool www
www-data 161 1 0 10:02 ? 00:00:00 php-fpm: pool www
root 1111 170 0 10:04 pts/0 00:00:00 grep --color=auto fpm
root@...:/var/www# **kill -USR2 170**
root@...:/home/user/Docker# **docker-compose stop**
Stopping docker_nginx_1 ... done
Stopping docker_oracle_1 ... done
root@...:/home/user/Docker# **docker-compose up -d**
Starting docker_oracle_1 ... done
Starting docker_nginx_1 ... done
root@...:/home/user/Docker# **docker-compose exec oracle bash**
root@...:/var/www# **/etc/init.d/php7.2-fpm restart**
* Restarting PHP 7.2 FastCGI Process Manager php-fpm7.2 **[ OK ]**

NickCoder
- 1,504
- 2
- 23
- 35

user2652810
- 27
- 2
-1
docker container kill --signal USR2 php_container_name
Details: https://docs.docker.com/engine/reference/commandline/container_kill/

Александр Карабанов
- 11
- 1
- 4
-3
You can also just restart the container..
sudo docker restart <container>

ricardorover
- 156
- 1
- 4

Fractalf
- 5,228
- 3
- 23
- 26