symfony-docker
symfony-docker copied to clipboard
Improve or comment out the suggested DATABASE_URL configuration
Recently I installed a Symfony 6 app with Symfony Docker. I needed Doctrine DBAL but did not want to add the entire ORM.
Unfortunately the DBAL and ORM are currently coupled: https://github.com/doctrine/DoctrineBundle/issues/973
So I installed Doctrine DBAL and the bundle. The Symfony recipe created the following file:
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
And adds the following parameters to .env:
###> doctrine/doctrine-bundle ###
DATABASE_URL="postgresql://app:[email protected]:3306/app?serverVersion=8&charset=utf8mb4"
###< doctrine/doctrine-bundle ###
I edited the DATABASE_URL using some pre-defined POSTGRES parameters:
POSTGRES_HOST = NameOfMyDockerDBImage
DATABASE_URL="postgresql://%env(resolve:POSTGRES_USER)%:%env(resolve:POSTGRES_PASSWORD)%@%env(resolve:POSTGRES_HOST)%/%env(resolve:POSTGRES_DB)%?serverVersion=%env(resolve:POSTGRES_VERSION)%&charset=utf8"
But got: An exception occurred in the driver: SQLSTATE[08006] [7] could not translate host name "database" to address: Try again
For some reason the doctrine.yaml was not accepting my .env parameters. I copied my DATABASE_URL directly there to see if my connection works at all.
doctrine:
dbal:
url: "postgresql://%env(resolve:POSTGRES_USER)%:%env(resolve:POSTGRES_PASSWORD)%@%env(resolve:POSTGRES_HOST)%/%env(resolve:POSTGRES_DB)%?serverVersion=%env(resolve:POSTGRES_VERSION)%&charset=utf8"
And indeed it did work. I looked up the "database" host name. It is defined in (docker-)compose.yaml
version: '3.9'
services:
db:
container_name: NameOfMyDockerDBImage
php:
...
environment:
# Run "composer require symfony/orm-pack" to install and configure Doctrine ORM
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-!ChangeMe!}@database:5432/${POSTGRES_DB:-app}?serverVersion=${POSTGRES_VERSION:-14}${POSTGRES_VERSION:-14}
https://github.com/dunglas/symfony-docker/blob/main/docker-compose.yml#L21
Maybe it would be better to comment out or completely remove this line. Users who only use DBAL and NOT the ORM will wonder why they cannot change the DATABASE_URL via .env.
In addition: The part "@database:5432" could directly hint to a image name.
I can't say why we declare this variable here.
I think if it is for a production case, we could move this variable to the docker-composer.prod.yml file.
Otherwise yes we could probably remove it.
@dunglas, do you have more information about this?
Also this issue https://github.com/dunglas/symfony-docker/issues/381 is related to the DATABASE_URL
I think there are several issues here related to DATABASE_URL, spent half a day for :-) To summarise, its a bit confusing when expecting to set only these .env vars in dev:
POSTGRES_USER=app
POSTGRES_PASSWORD=!ChangeMe!
POSTGRES_DB=app
POSTGRES_VERSION=14
... relying only on the docker-compose:
environment:
# Run "composer require symfony/orm-pack" to install and configure Doctrine ORM
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-!ChangeMe!}@database:5432/${POSTGRES_DB:-app}?serverVersion=${POSTGRES_VERSION:-14}
# Run "composer require symfony/mercure-bundle" to install and configure the Mercure integration
... but then having the docker compose build (at composer dump-env prod;) failed, where environment vars are not available and the .env.local is .dockerignored through COPY or the override linked volume.
So still keeping the DATABASE_URL in .env as a required placeholder:
###> doctrine/doctrine-bundle ###
DATABASE_URL="postgresql://app:[email protected]:3306/app?serverVersion=8&charset=utf8mb4"
###< doctrine/doctrine-bundle ###