Skip to content

Latest commit

 

History

History

24-supervisord-gracefully-shutdown

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Gracefully Stop Swoole Servers In Docker Containers

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 the phpswoole/swoole image.
  • Graceful shutdown capabilities are supported starting from Swoole version 5.1.2 and later in the phpswoole/swoole Docker image.

How To Gracefully Stop Supervisord Programs In Docker Container

1. Use command exec to run supervisord in the foreground.

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.

2. Set option stopasgroup to true in the supervisord configuration file(s).

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 Commands To Play With This Example

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.