I have a docker env set up with Symfony (5.x) & MariaDB – the docker-compose.yaml
file contains the db configuration env variables.
The .env.local
file in the Symfony app uses the configuration variables from the docker-compose
file to build out the DATABASE_URL
– the url value appears as expected in the bin/console debug:container --env-vars
command within the container.
The doctrine.yaml
file for the Symfony app uses url: '%env(resolve:DATABASE_URL)%'
– this does not appear to be working as expected, as the app is throwing a 500 error on the front end saying it cannot connect to the db.
Dumping the $_ENV
and $params
variables from the Connection
Doctrine class shows the $_ENV
vars correctly, but the passed $params
value contains the default pdo_mysql
protocol and none of the vars from the docker-composer.yaml
file.
If I hardcode the distinct values to the doctrine.yaml
dbal
fields (user
, host
etc) the app works as expected.
I’m fairly new to Symfony, so it’s highly likely that I’m missing something pretty obvious.
2
Answers
Not exactly the answer you’re looking for, but since you are new to Symfony. Its important to understand this :
you should not use simple env var to store your database url but symfony secret system.
Read this for setup and understand how it work.
https://symfony.com/doc/current/configuration/secrets.html
In your case it would be
Then it will asked you to enter the value.
Then you can easily register different value for each env.
Will store a different value for prod env.
The key point is to understand this :
Symfony secret are made for this reason.
In local you can check their value easily because you can do
But this
--reveal
require to have decrypt.private.php file in local WHICH SHOULD NOT BE PUSHED TO GIT FOR TEST AND PROD ENV SECRETSYou have one decrypt file for each env.
Then, manually upload this file to your test / prod server and when you deploy your app you can do
And
Then, all secret will be available as env var. Excactly the same way than if you put it directly inside .env file but in a protected way.
Please set all values in the .env file and copy this environment file into a docker container. Please add this line to your docker file.
COPY ./.env /var/www/html/application/.env
Here the first path is local and application/.env is the docker container path. Please change accordingly.