Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console PHP script runs differently when executes through docker exec or through PhpStorm

I'm trying to run console run Doctrine 2's console script through the PhpStorm. Docker is set up as Deploy server.

If I run this:

$ docker exec container_name /var/www/vendor/bin/doctrine-module orm:schema-tool:create

it prints:

No Metadata Classes to process.

But when I run PHP Run/Debug configuration in PhpStorm: File: /home/username/PhpstormProjects/proj/vendor/bin/doctrine-module

it prints:

docker://image_name/container_name /var/www/vendor/bin/doctrine-module

Fatal error: Uncaught PDOException: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php on line 765

Zend\ServiceManager\Exception\ServiceNotCreatedException: Service with name "doctrine.connection.orm_default" could not be created. Reason: An exception occured in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php on line 765

Call Stack: 0.0001 349368 1. {main}() /var/www/vendor/doctrine/doctrine-module/bin/doctrine-module:0 0.0268 360480 2. include('/var/www/vendor/doctrine/doctrine-module/bin/doctrine-module.php') /var/www/vendor/doctrine/doctrine-module/bin/doctrine-module:4 0.9376 4076096 3. Zend\ServiceManager\ServiceManager->get() /var/www/vendor/doctrine/doctrine-module/bin/doctrine-module.php:61 0.9376 4076096 4. Zend\ServiceManager\ServiceManager->doCreate() /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php:200

Process finished with exit code 255

I checked that when PDO is creating it receives absolutely the same arguments

new PDO("mysql:host=db;port=3306;dbname=dbname", "user", "pass", [])

docker-compose.yml:

version: '2'
services:
    nginx:
        container_name: nginx
        build:
            context: .
            dockerfile: DockerfileNginx
        ports:
            - "80:80"
        depends_on:
            - php
        working_dir: /var/www
        links:
            - php
        volumes:
            - .:/var/www
        links:
            - db
    php:
        container_name: php
        build:
            context: .
            dockerfile: DockerfilePhp
            - db
        volumes:
            - .:/var/www
        expose:
            - "9000"
        depends_on:
            - db
    db:
        container_name: db
        image: "mysql:5.6"
        ports:
            - "3306:3306"
        environment:
            MYSQL_ROOT_PASSWORD: pass
            MYSQL_DATABASE: dbname
            MYSQL_USER: user
            MYSQL_PASSWORD: pass
like image 590
You Care Avatar asked Dec 19 '22 10:12

You Care


1 Answers

I had the same problem and I found out, that PHPStorm starts a new, dedicated Docker container for your PHP calls. So, all your docker-compose configuration is missing at this point.

I did the following:

  • start your Docker stack with compose
  • after that find your network name with docker network ls (see Cannot link to a running container started by docker-compose for help)
  • inside PHPStorm goto your preferences for your Docker container (Languages & Frameworks -> PHP -> PHPUnit: Docker container ...) and add a link to your db (something like: name = your_containter_name_1, alias = db) and change the network mode from bridge to your network name
like image 61
Kevin Avatar answered Jan 13 '23 12:01

Kevin