How to start Nginx (or listen a http port) after PHP-FPM startup in a single container?
For now, I ended up with the following entrypoint script that ensures that PHP-FPM is running before starting nginx:
#!/usr/bin/env sh
set -e
# Start PHP-FPM as a daemon in the background
php-fpm -D
# Wait until PHP-FPM is up and accepts connections. Fail if not started in 10 secs.
for run in $(seq 20)
do
if [ "$run" -gt "1" ]; then
echo "Retrying..."
fi
RESPONSE=$(
SCRIPT_NAME=/fpm-ping \
SCRIPT_FILENAME=/fpm-ping \
REQUEST_METHOD=GET \
cgi-fcgi -bind -connect 127.0.0.1:9000 || true)
case $RESPONSE in
*"pong"*)
echo "FPM is running and ready. Starting nginx."
# Run nginx without exiting to keep the container running
nginx -g 'daemon off;'
exit 0
;;
esac
sleep .5
done
echo "FPM has failed to start on-time, exiting"
exit 1
The apk add fcgi
command is required (as for Alpine linux).
I also have a supposition that the php-fpm -D
command always exits after FPM is ready so no loops required, just run commands one after another. But I've not tested it.