Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variables are not recognized by Laravel in Docker

I'm having trouble working with environment variables in Docker.

For some reason Laravel can't see the environment variables I set within Dockerfile. Like APP_KEY, APP_ENV, etc. But when I SSH into the docker container:

docker exec -it [container_name] bash

and run the following:

php artisan tinker
> print_r($_ENV)

The environment variable exists and is set to the proper value. However, when I tried to do the same within the Blade template, I don't see the APP_KEY, APP_ENV variables at all. I've edited the php.ini file in the Docker container so that it shows

variables_order = 'EGPCS'

Is .env file the only way I can pass environment variables into Laravel?

I appreciate all your help.

Cheers

like image 552
bert_teng Avatar asked Oct 18 '22 13:10

bert_teng


1 Answers

I solved it! Actually a friend of mine gave me the clue.

Turns out it is the FPM configuration that is removing the environment variables set by Elastic Beanstalk.

Based on this document: http://php.net/manual/en/install.fpm.configuration.php

clear_env boolean

Clear environment in FPM workers. Prevents arbitrary environmentvariables from reaching FPM worker processes by clearing the environment in workers before env vars specified in this pool configuration are added. Since PHP 5.4.27, 5.5.11, and 5.6.0. Default value: Yes.

So in your php-fpm.conf file look for that variable and set it as follows:

clear_env = no

And you're are good to go! Your Laravel application can then pull the environment variables set in Elastic Beanstalk by using env()

I hope that helps someone. Cheers!

like image 200
bert_teng Avatar answered Oct 30 '22 23:10

bert_teng