In the last line I want to save environment variables to .env file for further usage
but it seem it doesnāt work
How can I achieve this goal I mean saving environment variables to .env file
Thanks in advance
You have to create the .env file during build or in an entrypoint or custom command. You just ran a bash command which starts when you start the container and it would exist immediately, but if cron or php-fpm runs in the foreground, the env command will never.
You can read about the entrypoint, cmd and shell instructions in my blogpost (planning a video, but I didnāt have time yet):
And if you want to know how you can run multiple daemons in a container, you can read my other tutorial on GitHub:
Hi Rimelek,
thanks for you answer but Iām not sure I understand it
Could you please say what should be changed in my Dockerfile to create .env filed filled with output of env or printenv command I thinks there is also could be some permission issues but I canāt figure out what exactly
Because I donāt know exactly what your goal was, I canāt fix it for you.
It seems you want to run a cron job and PHP FPM in one container. This is not the way. The correct way is using an init porcess because in a container only one process can run and that process an run other processes. An init process on a physical machine or virtual machine is usually Systemd. Running Systemd in a Docker container is not a good idea, but supervisor can work similarly. This is not something I can show you in a single post thatās why I linked tutorials and I have just added a video to the blog post if you want to learn about the very basics of the CMD and the ENTRYPOINT instruction which could be used to create a .env file before running php-fpm in a custom CMD shell script.
After trying to start cron and php-fpm, you try to do something else, but that will never run. It canāt run because php-fpm is running in foreground (I checked and cron will run in the background so php-fpm can start).
If you try to run php-fpm in the background (donāt do it), the env command runs, creates the .env file and the bash script just stops because there is nothing in the foreground to keep the container alive so that will stop too.
If you want a quick āsolutionā which is still a bad idea but should work, this is it:
CMD bash -c "cron && env > .env && php-fpm"
However, I have no idea why you would save the already existing environment variables to a dotenv file. My guess is that the PHP interpreter canāt read the original environment variables but that behavior can be changed in the PHP configuration.
Note that if you just run commands the way you did, when you run docker stop containername you wil need to wait 10 seconds before the Docker will kill your container forfefully. Normally the stop signal would be sent to the main process in the container and that would handle the signal, finish every task and stop safely. If you just kill a container that could lead to data corruption, although the chance is smaller in case of a PHP container.
Thank you very much!
I struggled with this for 3 days last week and now it works
CMD bash -c ācron && env > .env && php-fpmā
It creates the .env file as needed !!
But now I think that better solution for cron would be to use another Alpine image with cron as a service and ping my web app by url on schedule from itās container. In this case I donāt need to run two services in one container and I donāt need to store env variables to a file