我正在开发Laravel应用程序。我正在使用docker-compose安装我的环境。我正在使用postgres作为数据库。
这是我的docker-compose.yml
version: '3'
services:
apache:
container_name: easy_eat_apache
image: webdevops/apache:ubuntu-16.04
environment:
WEB_DOCUMENT_ROOT: /var/www/public
WEB_ALIAS_DOMAIN: easy-eat.localhost
WEB_PHP_SOCKET: php-fpm:9000
volumes: # Only shared dirs to apache (to be served)
- ./public:/var/www/public:cached
- ./storage:/var/www/storage:cached
networks:
- easy-eat-network
ports:
- "80:80"
- "443:443"
php-fpm:
container_name: easy_eat_php
image: jguyomard/laravel-php:7.2
volumes:
- ./:/var/www/
- ./ci:/var/www/ci:cached
- ./vendor:/var/www/vendor:delegated
- ./storage:/var/www/storage:delegated
- ./node_modules:/var/www/node_modules:cached
- ~/.ssh:/root/.ssh:cached
- ~/.composer/cache:/root/.composer/cache:delegated
networks:
- easy-eat-network
db:
image: postgres
restart: always
ports:
- 5432
environment:
POSTGRES_PASSWORD: secret
volumes:
- easy-eat-data:/var/lib/postgresql
networks:
easy-eat-network:
driver: "bridge"
volumes:
easy-eat-data:
driver: "local"
在Laravel的env文件中,我放入了这样的数据库凭据
DB_CONNECTION=pgsql
DB_HOST=db
DB_PORT=5432
DB_DATABASE=postgres
DB_USERNAME=postgres
DB_PASSWORD=secret
然后我运行迁移命令以查看我的应用程序是否能够连接到postgres。我收到此错误。
Illuminate\Database\QueryException : SQLSTATE[08006] [7] timeout expired (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations)
at /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 PDOException::("SQLSTATE[08006] [7] timeout expired")
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
2 PDO::__construct("pgsql:host=db;dbname=postgres;port=5432;sslmode=prefer", "postgres", "secret", [])
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
Please use the argument -v to see more details.
请问如何解决?
答案 0 :(得分:1)
您的php-fpm
容器已附加到easy-eat-network
网络,但您的db
容器未附加,因此它将附加到Docker Compose创建的默认网络。这将导致在容器之间解决和路由问题。
这里最简单的解决方案可能就是依靠Docker Compose为您创建的默认网络。删除所有networks:
块,包括顶级服务和单个服务。
如果您确实要自己明确声明网络,则需要在networks: [easy-eat-network]
服务块中添加db:
节。