dunglas / symfony-docker

A Docker-based installer and runtime for Symfony. Install: download and `docker compose up`.
https://dunglas.dev/2021/12/symfonys-new-native-docker-support-symfony-world/
2.58k stars 767 forks source link

Can't get doctrine to work #370

Closed bakedbird closed 1 year ago

bakedbird commented 1 year ago

Hi all, I did a clean installation, and added doctrine, then run build. The build fails because I commented out DATABASE_URL in .env, since it exists on docker-compose.

After uncommenting the DATABASE_URL in .env, the build worked but Symfony can't connect to the database because it's using the DATABASE_URL defined in the .env file.

Since DATABASE_URL in the docker-compose file should have precedence, I edited that one, and commented out the one in .env After running up, symfony can't connect to the DB as it can't find the DATABASE_URL.

So, my question is, what is the proper way to install doctrine?

maxhelias commented 1 year ago

Hi!

The proper way is to used the DATABASE_URL with the correct configuration. To keep a consistency between the app and docker, I personally use this configuration line DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:5432/${POSTGRES_DB}?serverVersion=${POSTGRES_VERSION}&charset=utf8" and configure the environment variables depending on it on the .env file.

In the other case, if you don't want to use the DATABASE_URL variable, them you have to configure the connection in the doctrine.yaml file. You will find more information in the documentation : https://www.doctrine-project.org/projects/doctrine-bundle/en/latest/configuration.html#doctrine-dbal-configuration

bakedbird commented 1 year ago

Hi @maxhelias, so, I should add the correct DATABASE_URL in the env file as well as the docker-compose file?

Also, what is the proper way to install doctrine?

thanks again, I'm new to this :)

maxhelias commented 1 year ago

There is no DATABASE_URL in the docker-compose file and no need to have it. DATABASE_URL is needed for doctrine configuration with the database and not for docker. It just has to match with the connection configuration with the docker service.

I recommend rather the first command (docker compose exec php composer require) and those for any package, because it will allow to install the last package available for the version of php configured for the docker container and not the php installed on your host.

bakedbird commented 1 year ago

Hmm, I see. Since the variable is there when creatingt the project, I thought it was needed to be configured only there and not in the .env file. I'm talking about this. line here

services:
  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}

So, I can remove this one, and configure it in the .env file. Did I understand correctly? Thank you for the suggestion for installing the packages :)

bedfir commented 1 year ago

Here are the steps to follow.

in the .env

PS: you can also set up the 4 environment variables in a new .env.local file, that are specific to your local machine.

bakedbird commented 1 year ago

Hi @bedfir, this is exactly what I have done and am having the issue.

Step by step:

POSTGRES_USER=db_user POSTGRES_PASSWORD=db_password POSTGRES_DB=db POSTGRES_VERSION=15


- `docker compose build --pull --no-cache`. After some time, I get this error:

0 2.188 Generated optimized autoload files (authoritative) containing 2524 classes

0 2.201 + composer dump-env prod

0 2.379 Successfully dumped .env files in .env.local.php

0 2.388 + composer run-script --no-dev post-install-cmd

0 2.566

0 2.566 Run composer recipes at any time to see the status of your Symfony recipes.

0 2.566

0 2.569 Executing script cache:clear [KO]

0 2.915 [KO]

0 2.915 Script cache:clear returned with error code 1

0 2.915 !!

0 2.915 !! In EnvVarProcessor.php line 189:

0 2.915 !!

0 2.915 !! Environment variable not found: "DATABASE_URL".

0 2.915 !!

0 2.915 !!

0 2.915 !!

0 2.915 Script @auto-scripts was called via post-install-cmd


failed to solve: executor failed running [/bin/sh -c set -eux; mkdir -p var/cache var/log; if [ -f composer.json ]; then composer dump-autoload --classmap-authoritative --no-dev; composer dump-env prod; composer run-script --no-dev post-install-cmd; chmod +x bin/console; sync; fi]: exit code: 1

bedfir commented 1 year ago

My apologies, yes Symfony is unable to find the "DATABASE_URL" environment variable, which is used to configure the database connection for Doctrine ORM.

The PDO is missing in the Dockerfile, there is only pdo_pgsql, as mentioned in the documentation (link below) pdo_pgsql implements PDO. _PDOPGSQL is a driver that implements the PHP Data Objects (PDO) interface to enable access from PHP to PostgreSQL databases.

To solve the problem you just have to add in the Dockerfile the PDO extension

RUN set -eux; \
    install-php-extensions \
    intl \
    zip \
    apcu \
    opcache \
    pdo \             <==== Add this line
    ;

Then run docker compose build --pull --no-cache And docker compose up

For the configuration of the database please follow the steps I have edited

bakedbird commented 1 year ago

Thank you @bedfir, adding PDO in Dockerfile fixed the issue and everything seems to be working correctly now.

My question now is, is this addition mentioned somewhere and I missed it? I've been trying to find a solution for days and I did not see this specific change mentioned somewhere.

dunglas commented 1 year ago

This should be handled automatically by Flex. It's maybe a bug in the Flex recipe for Doctrine ORM?

maxhelias commented 1 year ago

https://github.com/dunglas/symfony-docker/issues/370#issuecomment-1405063475

I'm talking about this. line here

Oh yes, I forgot that.

This should be handled automatically by Flex. It's maybe a bug in the Flex recipe for Doctrine ORM?

I don't think so, I did an installation last week and had no problems. Flex did add these lines on the Dockerfile :

###> doctrine/doctrine-bundle ###
RUN apk add --no-cache --virtual .pgsql-deps postgresql-dev; \
    docker-php-ext-install -j$(nproc) pdo_pgsql; \
    apk add --no-cache --virtual .pgsql-rundeps so:libpq.so.5; \
    apk del .pgsql-deps
###< doctrine/doctrine-bundle ###

I am confused, I do not understand what is the real issue Environment variable not found: "DATABASE_URL". or could not find driver ?

bedfir commented 1 year ago

Yes you're right, it's handled automatically by Flex. No need to add the PDO in the Dockerfile and it should work with the right config.

bakedbird commented 1 year ago

Yes, when I install doctrine, the recipe part in Dockerfile is updated, exactly like @maxhelias mentioned. When running the build command after that, it fails with the error mentioned.

Adding the PDO in the Dockerfile fixed the error, so, maybe as @dunglas mentioned, it can be an issue on the recipe?

Update I created a new instance and followed the steps again, without adding PDO in the Dockerfile.

If you guys have nothing to add, this issue can be closed.

maxhelias commented 1 year ago

The charset=utf8 can be add in the docker-compose.yaml file, you can submit a PR 😉

though I am not sure it may have caused the issue previously

Yes, I agree.