laravel - Redis + Docker : PDOException: could not find driver

标签 laravel docker redis docker-compose laravel-horizon

我刚开始使用 Redis。我在 Docker 中运行 Laravel、MariaDB 和 Redis。我似乎无法让 redis 正常工作。我在 Laravel Horizo​​n 中收到以下错误:

PDOException: could not find driver in /var/www/api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:46

我的猜测是代码正在 redis 容器中执行,它无法访问 PHP 容器。

这是我的 docker-compose.yml:

# Web server
nginx:
    image: nginx:latest
    restart: always
    links:
    - socketio-server
    ports:
    - "3000:3001"
    - "8081:80"
    volumes:
    - ./api:/var/www/api
    - ./docker/nginx/conf.d/:/etc/nginx/conf.d
    - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
    links:
    - php

# PHP
php:
    build: ./docker/php-fpm
    volumes:
    - ./api:/var/www/api
    links:
    - mariadb

# Redis
redis:
    image: redis:latest
    depends_on:
    - php
    expose:
    - "6379"

# Database
mariadb:
    image: mariadb:latest
    restart: always
    ports:
    - "3306:3306"
    volumes:
    - ./database/mariadb/:/var/lib/mysql

# PHP workers
php-worker:
    build:
    context: ./docker/php-worker
    args:
        - PHP_VERSION=7.2
        - INSTALL_PGSQL=false
    volumes:
    - ./:/var/www
    - ./docker/php-worker/supervisor.d:/etc/supervisor.d
    extra_hosts:
    - "dockerhost:10.0.75.1"
    links:
    - redis

有人有什么想法吗?

最佳答案

您关于容器彼此无法访问的假设是正确的。

您的 PHP 容器执行 PHP 代码,因此它必须有权访问 redis 容器和 mariadb 容器才能使用它们。为此,您可以将它们添加到 links 数组中。我看到您已经为 mariadb 完成了此操作,但您还应该添加 redis。

# PHP
php:
    build: ./docker/php-fpm
    volumes:
    - ./api:/var/www/api
    links:
    - mariadb
    - redis

通过将 redis 添加到 links 数组,您可以在 PHP 容器中使用主机名 redis 访问它。

关于laravel - Redis + Docker : PDOException: could not find driver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50564932/

相关文章:

mysql - SQL 查询到 Eloquent

docker - 连接 dockerized 应用程序网络以进行 api 调用

python - psycopg2.OperationalError : could not connect to server: Connection refused

php - Laravel mysql 多个操作与一个数据库实例

laravel - 在 Laravel 中使用 method_field ('PUT' ) 或 <input type ="hidden"name ="_method"value ="PUT"/> 的目的是什么?

python - Docker容器网络偶尔失败

redis - 在 Redis 中保存用户 ID 的最佳实践

mysql - 分布式爬虫和一致性

docker - 在 Docker 中安装后 Redis 服务器无法启动

php - 你如何使用 Laravel 在 PHP 中处理 paypal webhook 事件?