Coder Social home page Coder Social logo

Comments (2)

carakas avatar carakas commented on June 16, 2024

Seems to work here for me, can you try again?
I added it in my .env.local

from forkcms.

jessedobbelaere avatar jessedobbelaere commented on June 16, 2024

The env vars (e.g. db credentials) are set in the runtime of the container but not in the .env files. It works perfectly in general in the CMS, but some queries that SpoonDB executes in the Kernel bootup time will fail silently (try catch wrapped around it on Settings > Modules page).

Steps to reproduce:

  • Clone a fresh repo: git clone [email protected]:forkcms/forkcms.git fork-cms-issue-3322
  • cd fork-cms-issue-3322
  • Make some changes to the docker-compose.yml file to avoid some errors on startup.
services:
    app:
        build:
            context: .
            cache_from: [ghcr.io/forkcms/forkcms:latest]
        working_dir: /var/www/html
        ports:
            - "80:80"
            - "443:443"
        depends_on:
            - db
-       env_file: ./var/docker/.env
+       environment:
+           - DB_HOST=db
+           - DB_NAME=forkcms
+           - DB_USER=forkcms
+           - DB_PASSWORD=forkcms
        volumes:
-           - .:/var/www/html:cached
+           #- .:/var/www/html:cached
            - ./src/Frontend/Files:/var/www/html/src/Frontend/Files:cached
            - fork-cms-var:/var/www/html/var
  • Start docker-compose: docker-compose build && docker-compose up -d
  • Wait 5-10min to build the image :troll:
  • Run docker-compose exec app bash -c 'echo -e "FORK_ENV=dev\nFORK_DEBUG=1" > .env && composer install -o && chown -R www-data:www-data /var/www/html' otherwise it will complain about a missing .env in the root and throw errors (I should make a PR to make this more smooth 😭).
  • Visit http://localhost/ and complete the installer. Use DB hostname "db" and dbname/username/password set to forkcms.
  • Once the installer is finished, and you can visit the website, use docker-compose exec app bash to step into the container and install vim: apt-get update && apt-get install -y vim (or nano)
  • vi app/config/parameters.yml and switch it to these values:
    database.host:			'%env(DB_HOST)%'
    database.name:			'%env(DB_NAME)%'
    database.user:			'%env(DB_USER)%'
    database.password:			'%env(DB_PASSWORD)%'
  • Run printenv to check that these DB_HOST, DB_NAME, DB_USER and DB_PASSWORD values are correctly set.
  • Reload the page and login to the CMS
  • Everything still works 👍
  • Go to Settings > Modules --> all modules are installed? That's not correct.

image

  • The issue is in app/Kernel.php in getInstalledModules. There's a big try/catch around the SpoonDB query SELECT name FROM modules. https://github.com/forkcms/forkcms/blob/master/app/Kernel.php#L166-L174
  • Try to print the PDO connection string: vi vendor/spoon/library/spoon/database/database.php and go to line 164. Enter var_dump($dsn);. Then go to vi app/Kernel.php and on line 174 inside the catch (SpoonDatabaseException $e) place this: var_dump($e->getMessage());. Now save and reload the page.
/var/www/html/vendor/spoon/library/spoon/database/database.php:165:string 'mysql:host=env_DB_HOST_e93a13b12f8a11b61d2543b705df8f47;dbname=env_DB_NAME_c7754016e01b949d9226a29c3748622b;user=env_DB_USER_5aa8d02d6f073b416daf37e3a31d75d6;password=env_DB_PASSWORD_61ae438821ee6d4b4669861c2986cc5c;port=3306;charset=utf8' (length=238)

/var/www/html/app/Kernel.php:174:string 'A database connection could not be established.' (length=47)

/var/www/html/vendor/spoon/library/spoon/database/database.php:165:string 'mysql:host=db;dbname=forkcms;user=forkcms;password=forkcms;port=3306;charset=utf8' (length=81)

So, somehow the env vars are not resolved in the SpoonDB PDO object when the Kernel.php is booting up. But we did not use a die() statement in the SpoonDatabaseException... And we see the SpoonDB got initialized again, but this time with the correct credentials! Only shortly after the Kernel booted, the env vars are properly set in the database object?

🕵️ Ideally, we can initialize the modules later in the Kernel, or use a Doctrine raw connection with PDO to do this query, and move away from SpoonDB? However, I tried and I could not grab the doctrine service at that place in the Kernel, probably not initialized yet? Maybe you have more experience in the kernel area... 🙏

from forkcms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.