我刚开始使用 Redis。我在 Docker 中运行 Laravel、MariaDB 和 Redis。我似乎无法让 redis 正常工作。我在 Laravel Horizon 中收到以下错误:
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/