clean-docker / Magento2

Docker Image to Magento 2 local development in Mac, Linux and Windows
https://hub.docker.com/r/rafaelcgstz/magento2/
MIT License
444 stars 251 forks source link

Redis Connection Error #93

Open dickensdjj opened 5 years ago

dickensdjj commented 5 years ago

Hi guys, I currently having a redis connection error when I try to run some scripts.

Here is the context:

There is a webscript directory which I put down the src/ directory (this directory reckons as the root directory of Magento) and the file structure will be like this

Structure: src/webscript/test.php

Code of test.php:

require __DIR__ . '/../app/bootstrap.php';

use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Filesystem\DirectoryList;

$params = $_SERVER;
$params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] = [
    DirectoryList::PUB => [DirectoryList::URL_PATH => ''],
    DirectoryList::MEDIA => [DirectoryList::URL_PATH => 'media'],
    DirectoryList::STATIC_VIEW => [DirectoryList::URL_PATH => 'static'],
    DirectoryList::UPLOAD => [DirectoryList::URL_PATH => 'media/upload'],
];
$bootstrap = Bootstrap::create(BP, $params);

// Below is my custom code

Code of env.php:

<?php
return [
    'backend' => [
        'frontName' => 'admin'
    ],
    'crypt' => [
        'key' => '5e56dc6d8f24f711bb4f289cca1beda9'
    ],
    'session' => [
        'save' => 'redis',
        'redis' => [
            'host' => 'redis_session',
            'port' => '6379',
            'password' => '',
            'timeout' => '2.5',
            'persistent_identifier' => '',
            'database' => '2',
            'compression_threshold' => '2048',
            'compression_library' => 'gzip',
            'log_level' => '1',
            'max_concurrency' => '6',
            'break_after_frontend' => '5',
            'break_after_adminhtml' => '30',
            'first_lifetime' => '600',
            'bot_first_lifetime' => '60',
            'bot_lifetime' => '7200',
            'disable_locking' => '0',
            'min_lifetime' => '60',
            'max_lifetime' => '2592000'
        ]
    ],
    'db' => [
        'table_prefix' => '',
        'connection' => [
            'default' => [
                'host' => 'db',
                'dbname' => 'magento',
                'username' => 'magento',
                'password' => 'magento',
                'active' => '1',
                'model' => 'mysql4',
                'engine' => 'innodb',
                'initStatements' => 'SET NAMES utf8;'
            ]
        ]
    ],
    'resource' => [
        'default_setup' => [
            'connection' => 'default'
        ]
    ],
    'x-frame-options' => 'SAMEORIGIN',
    'MAGE_MODE' => 'developer',
    'cache_types' => [
        'config' => 1,
        'layout' => 1,
        'block_html' => 1,
        'collections' => 1,
        'reflection' => 1,
        'db_ddl' => 1,
        'eav' => 1,
        'customer_notification' => 1,
        'full_page' => 0,
        'config_integration' => 1,
        'config_integration_api' => 1,
        'translate' => 1,
        'config_webservice' => 1,
        'compiled_config' => 1
    ],
    'install' => [
        'date' => 'Thu, 05 Jan 2017 22:49:50 +0000'
    ],
    'cache' => [
        'frontend' => [
            'default' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' => [
                    'server' => 'redis',
                    'port' => '6379',
                    'persistent' => '',
                    'database' => '0',
                    'password' => '',
                    'force_standalone' => '0',
                    'connect_retries' => '1',
                    'read_timeout' => '10',
                    'automatic_cleaning_factor' => '0',
                    'compress_data' => '1',
                    'compress_tags' => '1',
                    'compress_threshold' => '20480',
                    'compression_lib' => 'gzip',
                    'use_lua' => '0'
                ]
            ],
            'page_cache' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' => [
                    'server' => 'redis',
                    'port' => '6379',
                    'persistent' => '',
                    'database' => '1',
                    'password' => '',
                    'force_standalone' => '0',
                    'connect_retries' => '1',
                    'lifetimelimit' => '57600',
                    'compress_data' => '0'
                ]
            ]
        ]
    ]
];

Code of docker_compose.yml:

version: '2'
services:
  apache:
    image: rafaelcgstz/magento2
    # build: .
    ports:
      - "80:80" # web
      - "9001:9000" # xdebug
      # - "35729:35729" # live reload
    volumes:
      - jh-sync:/var/www/html:nocopy
      - ~/.composer:/var/www/.composer
      - ~/.npm:/var/www/.npm
      # - ~/.nvm:/var/www/.nvm
    environment:
      XDEBUG_CONFIG: "remote_host=10.254.254.254 remote_connect_back=0"      
      PHP_IDE_CONFIG: "serverName=Docker"
    depends_on:
      - db
    links:
      - db
    networks:
      - jh-network

  db:
    image: mariadb
    ports:
     - 3300:3306
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=magento
      - MYSQL_USER=magento
      - MYSQL_PASSWORD=magento
    volumes:
      - dbdata:/var/lib/mysql
    networks:
      - jh-network

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    environment:
     - PMA_HOST=db
     - PMA_USER=root
     - PMA_PASSWORD=root
     - MYSQL_ROOT_PASSWORD=root
    ports:
     - 8080:80
    networks:
      - jh-network

  redis:
    image: redis
    ports:
     - 6379
    networks:
      - jh-network

  redis-session:
    image: redis
    ports:
     - 6379
    networks:
      - jh-network

  mailhog:
    image: mailhog/mailhog
    ports:
      - 1025:1025
      - 8025:8025
    networks:
      - jh-network

networks:
  jh-network:
    driver: bridge

volumes:
  jh-sync:
    external: true
  dbdata:
    driver: local

However, when I try to run this script with my phpstorm debugger, it pops up the error message:

PHP Fatal error:  Uncaught CredisException: Connection to Redis redis:6379 failed after 2 failures.Last Error : (0) php_network_getaddresses: getaddrinfo failed: Name or service not known in /opt/project/src/vendor/colinmollenhour/credis/Client.php:463
Stack trace:
#0 /opt/project/src/vendor/colinmollenhour/credis/Client.php(459): Credis_Client->connect()
#1 /opt/project/src/vendor/colinmollenhour/credis/Client.php(792): Credis_Client->connect()
#2 /opt/project/src/vendor/colinmollenhour/credis/Client.php(619): Credis_Client->__call('select', Array)
#3 /opt/project/src/vendor/colinmollenhour/cache-backend-redis/Cm/Cache/Backend/Redis.php(369): Credis_Client->select(1)
#4 /opt/project/src/vendor/colinmollenhour/cache-backend-redis/Cm/Cache/Backend/Redis.php(242): Cm_Cache_Backend_Redis->_applyClientOptions(Object(Credis_Client))
#5 /opt/project/src/vendor/magento/zendframework1/library/Zend/Cache.php(153): Cm_Cache_Backend_Redis->__construct(Array)
#6 /opt/project/src/vendor/magento/zendframework1/library/Zend/Cache.php(94): Ze in /opt/project/src/vendor/colinmollenhour/credis/Client.php on line 463

It seems like the they cannot find the redis:6379. I have done some search about this redis connection with magento, but it is barely to find and related information.

I am appreciated if someone can put me out of this puzzle. Thanks,

JackWormUK commented 5 years ago

Redis uses 3 databases. From your code above, 2 of the databases (0,1) run on host 'redis' the session controller (database 2) runs on host 'redis_session'. I am struggling to setup PHPStorm so that is as much help as I can give until someone replies to me on here with a bit of help on setting it up.