Closed bakedbird closed 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
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?
docker compose exec php composer require symfony/orm-pack
composer require symfony/orm-pack
thanks again, I'm new to this :)
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.
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 :)
Here are the steps to follow.
DATABASE_URL="postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=15&charset=utf8"
POSTGRES_USER=YourPostgresUser
POSTGRES_PASSWORD=yourPassword
POSTGRES_DB=yourDbName
POSTGRES_VERSION=postgresVersion(current 15)
DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:5432/${POSTGRES_DB}?serverVersion=${POSTGRES_VERSION}
PS: you can also set up the 4 environment variables in a new .env.local file, that are specific to your local machine.
Hi @bedfir, this is exactly what I have done and am having the issue.
Step by step:
docker compose build --pull --no-cache
docker compose up
docker cp $(docker compose ps -q caddy):/data/caddy/pki/authorities/local/root.crt /tmp/root.crt && sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain /tmp/root.crt
docker compose exec php composer require symfony/orm-pack
docker compose down --remove-orphans
services:
php:
...
environment:
# Run "composer require symfony/orm-pack" to install and configure Doctrine ORM
DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:5432/${POSTGRES_DB}?serverVersion=${POSTGRES_VERSION}
...
###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
# DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=8&charset=utf8mb4"
# DATABASE_URL="postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=15&charset=utf8"
###< doctrine/doctrine-bundle ###
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
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
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
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.
This should be handled automatically by Flex. It's maybe a bug in the Flex recipe for Doctrine ORM?
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
?
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.
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.
DATABASE_URL
in the .env file, I am now able to build normally.charset=utf8
is also included in the default DATABASE_URL
, though I am not sure it may have caused the issue previously.If you guys have nothing to add, this issue can be closed.
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.
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?