I'm working on some auto-scaling Kubernetes infrastructure to run Magento 2, and I noticed that when I start new containers, I can't have the generated/
directory automatically compiled as part of the container image build—it seems like it needs a working Magento installation (which I'm not guaranteed to have when building the container) to compile.
So currently what I'm doing is, when a new container starts up (e.g. when autoscaling, or when updating code, and new containers are replacing old ones):
- Check if Magento is installed.
- If it is installed, run:
bin/magento module:enable --all
bin/magento setup:di:compile
This seems to work okay, and the new container works just like the old/existing ones—if I don't do the compilation step, I just get lots of fatal exceptions until I do the compilation.
But my question is this: is there some other/better way of handling the generated/
directory when you want to run Magento on multiple hosts/containers?
Should I share the generated/
directory via NFS like I do for var/
? It seems that reading code out of that directory would be a lot slower with NFS... and maybe the extra minute or so it takes to start a new container is worth the tradeoff to have each container compile/optimize it's own DI code (vs. sharing the directory with other existing containers).