3

I've setup my docker to connect with redis. But upon running the application i'm getting this error

Fatal error: Uncaught CredisException: 
Connection to Redis redis:0 failed after 2 failures.
Last Error : (111) Connection refused in /var/www/html/vendor/colinmollenhour/credis/Client.php:475 
Stack trace: 
#0 /var/www/html/vendor/colinmollenhour/credis/Client.php(471): Credis_Client->connect() 
#1 /var/www/html/vendor/colinmollenhour/credis/Client.php(784): Credis_Client->connect() 
#2 /var/www/html/vendor/colinmollenhour/credis/Client.php(637): Credis_Client->__call('select', Array) 
#3 /var/www/html/vendor/colinmollenhour/cache-backend-redis/Cm/Cache/Backend/Redis.php(382): Credis_Client->select(1) 
#4 /var/www/html/vendor/colinmollenhour/cache-backend-redis/Cm/Cache/Backend/Redis.php(243): Cm_Cache_Backend_Redis->_applyClientOptions(Object(Credis_Client)) 
#5 /var/www/html/vendor/magento/zendframework1/library/Zend/Cache.php(153): Cm_Cache_Backend_Redis->__construct(Array) 
#6 /var/www/html/vendor/magento/zendframework1/library/Zend/Cache.php(94): Zend_Cache::_makeBackend('Cm_Cache_Backen...', Array, true, true) 
#7 /var/www/ht in /var/www/html/vendor/colinmollenhour/credis/Client.php on line 475

Not sure where to modify. Maybe someone here can help me? This is the configuration I have for my docker-compose.yml

version: '3.0'
services:
  web:
    image: alexcheng/magento2
    ports:
      - "8080:80"
      - "443:443"
      - "32823:22"
    env_file:
      - env
    environment:
      - WEB_ALIAS_DOMAIN=local.domain.com
      - WEB_DOCUMENT_ROOT=/var/www/html
      - PHP_DATE_TIMEZONE=EST
      - PHP_DISPLAY_ERRORS=1
      - PHP_MEMORY_LIMIT=2048M
      - PHP_MAX_EXECUTION_TIME=300
      - PHP_POST_MAX_SIZE=500M
      - PHP_UPLOAD_MAX_FILESIZE=1024M
    volumes:
      - "./project:/var/www/html"
    container_name: web
    links:
      - db
      - redis
  db:
    image: mariadb:10
    container_name: mariadb
    volumes:
      - db-data:/var/lib/mysql/data
    env_file:
      - env
    restart: always
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=my_local_db
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - "8580:80"
    links:
      - db     
    container_name: phpmyadmin    
  redis:
    image: redis:latest
    container_name: redis
    restart: always
    ports:
      - "6379:6379"
    volumes:
      - redis-data:/data
volumes:
  db-data: 
    external: false 
  redis-data:
    external: false 

And this is the content of my env.php file under app/etc/

<?php
return [
    'backend' => [
        'frontName' => 'admin'
    ],
    'crypt' => [
        'key' => 'MY_KEY_HERE'
    ],
    'db' => [
        'table_prefix' => '',
        'connection' => [
            'default' => [
                'host' => 'db',
                'dbname' => 'my_local_db',
                'username' => 'user',
                'password' => 'password',
                'active' => '1'
            ]
        ]
    ],
    'resource' => [
        'default_setup' => [
            'connection' => 'default'
        ]
    ],
    'x-frame-options' => 'SAMEORIGIN',
    'MAGE_MODE' => 'developer',
    'cache' => [
        'frontend' => [
            'default' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' => [
                    'server' => 'redis',
                    'database' => '0',
                    'port' => ''
                ]
            ],
            'page_cache' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' => [
                    'server' => 'redis',
                    'port' => '',
                    'database' => '1',
                    'compress_data' => '0'
                ]
            ]
        ]
    ],
    'session' => [
        'save' => 'redis',
        'redis' => [
            'host' => 'redis',
            'port' => '',
            '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'
        ]
    ],
    'cache_types' => [
        'config' => 1,
        'layout' => 1,
        'block_html' => 1,
        'collections' => 1,
        'reflection' => 1,
        'db_ddl' => 1,
        'compiled_config' => 1,
        'eav' => 1,
        'customer_notification' => 1,
        'config_integration' => 1,
        'config_integration_api' => 1,
        'full_page' => 1,
        'config_webservice' => 1,
        'translate' => 1,
        'vertex' => 1
    ],
    'install' => [
        'date' => 'Wed, 09 Jan 2019 12:54:30 +0000'
    ]
];

Hope someone can help me with this...

Update: I tried removing this code in app/etc/env.php and it was running

'cache' => [
    'frontend' => [
        'default' => [
            'backend' => 'Cm_Cache_Backend_Redis',
            'backend_options' => [
                'server' => 'redis',
                'database' => '0',
                'port' => ''
            ]
        ],
        'page_cache' => [
            'backend' => 'Cm_Cache_Backend_Redis',
            'backend_options' => [
                'server' => 'redis',
                'port' => '',
                'database' => '1',
                'compress_data' => '0'
            ]
        ]
    ]
],

I'm not sure what this does or how important this is because I just copied this in one of the tutorial here

  • I just created an answer below. Btw, this looks like a modified version of the docker recipe I offer on my blog. Just curious, is it? magemodule.com/all-things-magento/magento-2-tutorials/… – Shawn Abramson Mar 17 at 1:59
  • What if you go inside your php-fpm and try curl to redis:6379? What will it say? Your docker-compose file looks fine, maybe try to add port into your env.php? – Toan Nguyen Mar 17 at 9:41
  • With a link in the docker-compose.yml you shouldn't need to specify the port for redis. I don't and my setup is very similar. – Shawn Abramson Mar 17 at 11:08
  • If you've already removes the additional "links" node can you update the docker-compose sample in your question to more accurately reflect the current state of your setup? – Shawn Abramson Mar 17 at 11:09
  • @ShawnAbramson yes it's a modified version as I wanted to make use of your sample and another one from other sources that I found. Will update the docker-compose.yml – MadzQuestioning Mar 17 at 13:21
0

In your web service, you have two links nodes. Delete the first one sitting under the ports mapping. Everything else looks correct.

What's happening is that the first links node is being used and so docker never realizes you're creating a link to your redis service.

Delete that and then docker-compose down && docker-compose up -d --build and you should be golden.

Further to our discussion in the comments section, I am posting the full contents of the env.php file from the 2.3 CE site I happen to be working on at the moment. Please note that port is not actually required with this setup but I created a cli script to generate this and it puts the port in there.

<?php
return [
    'backend' => [
        'frontName' => 'admin'
    ],
    'crypt' => [
        'key' => '67c754d8850edfa7204d7b3c545b9d24'
    ],
    'db' => [
        'table_prefix' => '',
        'connection' => [
            'default' => [
                'host' => 'mysql',
                'dbname' => 'qbo',
                'username' => 'root',
                'password' => 'root',
                'model' => 'mysql4',
                'engine' => 'innodb',
                'initStatements' => 'SET NAMES utf8;',
                'active' => '1'
            ]
        ]
    ],
    'resource' => [
        'default_setup' => [
            'connection' => 'default'
        ]
    ],
    'x-frame-options' => 'SAMEORIGIN',
    'MAGE_MODE' => 'developer',
    'session' => [
        'save' => 'redis',
        'redis' => [
            'host' => 'redis',
            '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',
            'sentinel_master' => '',
            'sentinel_servers' => '',
            'sentinel_connect_retries' => '5',
            'sentinel_verify_master' => '0'
        ]
    ],
    'cache' => [
        'frontend' => [
            'default' => [
                'backend_options' => [
                    'server' => 'redis',
                    'database' => '0',
                    'port' => '6379'
                ]
            ],
            'page_cache' => [
                'backend_options' => [
                    'server' => 'redis',
                    'database' => '1',
                    'port' => '6379',
                    'compress_data' => '0'
                ]
            ]
        ]
    ],
    'cache_types' => [
        'config' => 1,
        'layout' => 1,
        'block_html' => 1,
        'collections' => 1,
        'reflection' => 1,
        'db_ddl' => 1,
        'compiled_config' => 1,
        'eav' => 1,
        'customer_notification' => 1,
        'config_integration' => 1,
        'config_integration_api' => 1,
        'full_page' => 1,
        'config_webservice' => 1,
        'translate' => 1,
        'vertex' => 1
    ],
    'install' => [
        'date' => 'Fri, 15 Mar 2019 15:24:56 +0000'
    ],
    'system' => [
        'default' => [
            'dev' => [
                'debug' => [
                    'debug_logging' => '0'
                ]
            ]
        ]
    ]
];

docker-compose.yml

version: '3'
services:
    web:
        image: webdevops/php-apache-dev:7.2
        container_name: qbo_web
        user: application
        restart: always
        volumes:
          - "/Users/shawn/Websites/Magento2/qbo:/app:cached"
          - "root-ssh-files:/root/.ssh/"
          - "user-ssh-files:/home/application/.ssh/"
        environment:
          - WEB_ALIAS_DOMAIN=local.qbo.com
          - WEB_DOCUMENT_ROOT=/app/pub
          - PHP_DATE_TIMEZONE=EST
          - PHP_DISPLAY_ERRORS=1
          - PHP_MEMORY_LIMIT=2048M
          - PHP_MAX_EXECUTION_TIME=300
          - PHP_POST_MAX_SIZE=500M
          - PHP_UPLOAD_MAX_FILESIZE=1024M
          - PHP_DEBUGGER=xdebug
          - XDEBUG_REMOTE_CONNECT_BACK=0
          - XDEBUG_REMOTE_HOST=10.200.10.1
          - XDEBUG_REMOTE_PORT=9000
          - php.xdebug.remote_enable=1
          - php.xdebug.idekey=PHPSTORM
        ports:
          - "443:443"
          - "32834:22"
        links:
          - mysql
          - redis
    mysql:
        image: mariadb:10
        container_name: qbo_mysql
        restart: always
        volumes:
          - db-data:/var/lib/mysql
        environment:
          - MYSQL_ROOT_PASSWORD=root
          - MYSQL_DATABASE=qbo
        ports:
          - "33067:3306"
        command: [
            '--wait_timeout=28800',
            '--max_allowed_packet=32505856'
        ]
    redis:
        image: redis:latest
        container_name: qbo_redis
        restart: always
        ports:
          - "6379:6379"
        volumes:
          - redis-data:/data
volumes:
    db-data:
        external: false
    root-ssh-files:
        external: false
    user-ssh-files:
        external: false
    redis-data:
        external: false
  • Already did that and the error is a connection refused. I think this is not the problem because if web service does not realize there is a redis then "I think" the error should not be connection refused? – MadzQuestioning Mar 17 at 7:30
  • Have you tried my answer? Just type Redis port and let us know – x86fantini Mar 19 at 19:39
0

I can read from the error log "unable to connect to redis:0". In my opinion your env.php is lacking default redis port, 6379. Just update your app/etc/env.php with default redis port (6379) instead of a blank ''..this should fix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.