Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve or comment out the suggested DATABASE_URL configuration #379

Open
webdevilopers opened this issue Feb 10, 2023 · 3 comments
Open

Comments

@webdevilopers
Copy link

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:
doctrine/DoctrineBundle#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:!ChangeMe!@127.0.0.1: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.

@maxhelias
Copy link
Collaborator

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?

@maxhelias
Copy link
Collaborator

Also this issue #381 is related to the DATABASE_URL

@novelnet
Copy link

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:!ChangeMe!@127.0.0.1:3306/app?serverVersion=8&charset=utf8mb4"
###< doctrine/doctrine-bundle ###

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants