3

I have the following environment on my php container like so:

DATABASE_URL:mysql://root:${MYSQL_ROOT_PASSWORD}@db:3306/${MYSQL_DATABASE}

I tried to echo it inside my container, and do a db migration. All is good. Now I used it on my .env file on symfony like so:

DATABASE_URL=${DATABASE_URL}

When i tried to login, the app says:

Authentication request could not be processed due to a system problem.

When i try to manually put everything on .env DATABASE_URL all is good.

I suspect that when I tried to use the container's ENV it doesn't get it right.

My question is how can I use the actual Containers' environment variable?

Thanks!

Note:

I on dev environment.

2 Answers 2

2

I am not quite familiar with symfony, but it seems like symfony never overwrites existing environment variables. (Ref: https://symfony.com/doc/current/components/dotenv.html)

What if you remove that line in your .env file? Since DATABASE_URL is already an environment variable, calling getenv('DATABASE_URL') in symfony should return you the correct value even if you did not define it in .env. All dotenv does it to write those key value pairs as environment variables in your system. You don't need to define it again if it is already present.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you try printing the value from getenv('DATABASE_URL') somewhere in your application and see what it shows? Also, when you said you tried echo-ing, I suppose you did echo $DATABASE_URL?
2

note that there is separate environments when you run php from cli and when it gets run by webserver (when you access it from your browser).

For example, in case of nginx-fpm, the variables that you see in your cli by running printenv are not avaiable in php script run by nginx when you call getenv(). In order to set environment variable for php fpm you can edit php-fpm.conf:

....
[www]
env[DATABASE_URL] = 'mysql://...'
....

If you use another webserver than you should find out how to make env vars available in php script.

Your DATABASE_URL=${DATABASE_URL} in .env file didn't work bacause DATABASE_URL was not set for php fpm.

Hope this helps.

P.S. Note, that this construction VAR=${VAR} makes nothing because DotEnv will not override VAR as it is already defined.

P.P.S. It is advised that you use .env file for your dev server and "real" env variables in staging/production.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.