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

CLI Not respecting .env file when using Docker #430

Open
boskos-q opened this issue Feb 9, 2024 · 1 comment
Open

CLI Not respecting .env file when using Docker #430

boskos-q opened this issue Feb 9, 2024 · 1 comment

Comments

@boskos-q
Copy link

boskos-q commented Feb 9, 2024

I have local project that I'm setting up that uses docker for DB.
I've updated .env for DATABASE_URL env var to reflect what db url is but after serving that project with symfony serve errors appear Unknown database 'root'
After digging a bit in what's happening, it appears that symfony CLI is not using .env file from the project as it should, but it rather appears that it's using some other .env file when my docker is up.

Steps to reproduce:

  1. install default symfony app
  2. use docker for DB with provided yaml file
    docker-compose.yaml
version: '3.7'
services:
    database:
        image: 'mariadb'
        environment:
            MYSQL_ROOT_PASSWORD: password
        ports:
            - '3306:3306'
  1. start docker docker-compose up -d
  2. update your .env file to use DB that you want (in my case it's DATABASE_URL="mysql://root:password@127.0.0.1:3306/symfony?serverVersion=mariadb-11.2.2"
  3. run command to create DB php bin/console doctrine:schema:create and your db symfony should be created
  4. (optional) if you run symfony console doctrine:schema:create you'll see message that db root was created

Findings:
Now where does this error comes from as my db name is symfony and not root.
After checking symfony console debug:container --env-vars I've got this result:
DATABASE_URL "mysql://root:password@127.0.0.1:3306/root?sslmode=disable&charset=utf8mb4&serverVersion=1:11.2.2+maria~ubu2204" which means Symfony CLI didn't actually use my local .env file but rather something else.

Then it came to my mind to check what will happen if I stop docker DB, and re-run previous command. Response was different and I got what was actually in my defined DATABASE_URL: DATABASE_URL "mysql://root:password@127.0.0.1:3306/symfony?serverVersion=mariadb-11.2.2"

What should be expected behaviour?
Symfony CLI should use projects defined .env file

Symfony CLI Version:
Symfony CLI version 5.8.7 (c) 2021-2024 Fabien Potencier (2024-02-08T11:42:34Z - stable)

This issue could be related to #425 and #152

@boskos-q
Copy link
Author

boskos-q commented Feb 9, 2024

So after reading CLI docs and codebase it looks like this is expected behaviour.
I would strongly suggest that this should be disabled by default, and enabled with some flag or config param e.g. in .symfony.local.yaml

Using Symfony CLI in Symfony project with Docker DB is somewhat now default scenario for some local development etc., unless you go full docker setup for your Symfony project. By default it's expected from developer to configure it's .env file for DB params etc, and with this "magic" it makes a lot of trouble and drawback, especially debugging what is going on, and why is it doing this when I didn't define it at all.
This took me couple hours from my coding session to debug (RTFM lesson for me I guess in this case), but regardless, this was not expected behaviour of the CLI.

For those who are looking for solution and disabling this, as documentation says, you should add these two lines in your docker-compose.yaml

        labels:
            com.symfony.server.service-ignore: true

complete yaml with this implemented:

version: '3.7'
services:
    database:
        image: 'mariadb'
        environment:
            MYSQL_ROOT_PASSWORD: password
        ports:
            - '3306:3306'
        labels:
            com.symfony.server.service-ignore: true

Now restart your docker servise and start symfony server, you'll be using your defined docker env database url.

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

No branches or pull requests

1 participant