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.