Lately I faced some problems working with PHP consumers on AWS ECS and I thought would be good sharing it.
Be sure your Dockerfile is configured to get the proper signal
Could happen that your Dockerfile is based on another that has a
STOPSIGNAL configured that it’s not
SIGTERM, see for example the official php-fpm image.
To avoid problems I strongly recommend setting your desired signal in you Dockerfile, usually
SIGTERM it’s what you’d expect since it’t the default one that
docker stop command would send:
See the official Docker documentation to know more about it.
Entry point using
ECS will send a
SIGTERM signal to the process with
PID 1. If we want our worker to have it we must use
exec to execute it in the entrypoint bash script file.
SIGTERM signal from you code
You need to have a proper strategy in your code to handle
SIGTERM signals. If you use Symfony Messenger this is done by default. In case you use your own solution I’d recommend once you receive the stop signal let the last received messages to process and after that stop the loop so the process can stop without any problem or interruption.
pcntl PHP extension to do so, see here how to do it.
Setup a proper ECS
ECS by default will send first a
SIGTERM signal and will give 30 seconds until it finally sends a
If you need to increase the timeout setup
stopTimeout in the container definition file.