In this example, we will demonstrate how to perform a graceful shutdown of Swoole servers within a Docker container using the phpswoole/swoole
image. Key points to note include:
- The
supervisord
program is utilized for managing Swoole servers within Docker containers of thephpswoole/swoole
image. - Graceful shutdown capabilities are supported starting from Swoole version 5.1.2 and later in the
phpswoole/swoole
Docker image.
As we can see in the entrypoint.sh script, command exec
is used to forward signals to supervisord
processes:
exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf -n # Run supervisord in the foreground.
Also, please make sure that the stopsignal
option is commented out or set to TERM
(the default value), as we can see
in the Supervisord configuration file swoole.conf:
stopasgroup=true
; stopsignal=QUIT ; DON'T set this; it prevents graceful shutdown of Supervisord programs.
docker compose up # Start the Docker container and monitor the logs.
# You will need a separate terminal to run the following commands.
docker compose exec -ti app supervisorctl status # Check the status of Supervisord programs.
docker compose exec -ti app curl -i http://127.0.0.1:9501 # Check the status of the Swoole HTTP server.
docker compose exec -ti app supervisorctl signal SIGTERM swoole # Send SIGTERM signal to the Swoole HTTP server.
docker compose kill --signal=SIGTERM # Send SIGTERM signal to the Docker container.
docker compose stop # Stop the Docker container.