The best place for that is within the Dockerfile generated by the fly launch command.
#...
### Find this part:
# clear Laravel cache that may be left over
RUN composer dump-autoload \
&& php artisan optimize:clear \
&& chmod -R ug+w /var/www/html/storage \
&& chmod -R 755 /var/www/html
### Add this right below it:
### (Use whatever commands you'd like!)
USER app
RUN php artisan config:cache \
&& php artisan route:cache \
&& php artisan view:cache
### Be sure to add it before this stuff:
# Multi-stage build: Build static assets
FROM node:14 as node_modules_go_brrr
# ...
I think the problem is that Laravel is not able to get the Fly.iosecret environmental variables and it is caching all the Laravel environmental variables as NULL;
In this case the Laravel env variable APP_KEY is cached as NULL.
This only happens when I add the code to the Dockerfile.
USER app
RUN php artisan config:cache \
&& php artisan route:cache \
&& php artisan view:cache
USER root
OK yep - Secrets aren’t available during build time, and it’s caching null there. Ick!
(Thanks for your patience there).
What we can do is move these commands to the entrypoint script, which will make run at run-time (instead of build-time!) and thus have that secret available.
So rather than edit the Dockerfile, let’s edit docker/run.sh:
#!/usr/bin/env sh
# Start Octane with the roadrunner binary if we find
# that dependency in the composer.json file
# This is a noop if octane is not used
if [ -f /var/www/html/composer.json ]; then
if grep -Fq "spiral/roadrunner" /var/www/html/composer.json
then
sed -i 's/;rr command/command/g' /etc/supervisord.conf
else
sed -i 's/;swoole command/command/g' /etc/supervisord.conf
fi
fi
### CHANGES HERE:
if [ $# -gt 0 ];then
# If we passed a command, run it as root
exec "$@"
else
# Otherwise start supervisord
/bin/su -c "/usr/bin/php /var/www/html/artisan config:cache" - app
/bin/su -c "/usr/bin/php /var/www/html/artisan route:cache" - app
/bin/su -c "/usr/bin/php /var/www/html/artisan view:cache" - app
exec supervisord -c /etc/supervisord.conf
fi
On my end, I’ll see about updating the official Laravel “stuff” with this and get it in the right spot.
Try logging into into (ssh): fly ssh console and run printenv or just env to see what environment variables are set. APP_KEY should be set. You can also run a config:cache command there as well to see if it helps.
I’ll try this out myself (finally have some time) to see what’s happening as well.
Running su -c "command here" - app runs the command as user app, but the environment variables (including the APP_KEY secret) aren’t available using that method.
So we can go back to the much-less-fancy method of running as root and then running chown to set file ownership afterwards.
Also be sure your Dockerfile isn’t still running these cache commands
Update your docker/run.sh file one more time:
### Update the commands here
if [ $# -gt 0 ];then
# If we passed a command, run it as root
exec "$@"
else
# Otherwise start supervisord
/usr/bin/php /var/www/html/artisan config:cache
/usr/bin/php /var/www/html/artisan route:cache
/usr/bin/php /var/www/html/artisan view:cache
chown -R app:app /var/www/html
exec supervisord -c /etc/supervisord.conf
fi