首页 > 解决方案 > 无法在 Docker 中连接到 Laravel 中的 pgsql

问题描述

我正在使用 Laravel 开发一个 Web 应用程序。我是一位经验丰富的 Laravel 开发人员。但是,现在我正在尝试使用 Docker 作为我的开发环境。但我对 Docker 很陌生。现在我正在尝试连接到 Postgres 数据库。我也在 docker-composer.yml 中添加了 Postgres docker 镜像。但是当我运行迁移时,我收到错误并且它没有连接到数据库。

这是我的 docker-compose.xml 文件。

version: '2'
services:
  web:
    build:
      context: ./
      dockerfile: docker/web.docker
    volumes:
    - ./:/var/www
    ports:
    - "80:80"
    - "443:443"
    - "9000:9000"
    links:
    - app
  app:
    build:
      context: ./
      dockerfile: docker/app.docker
    volumes:
    - ./:/var/www
    links:
    - mysql
    - redis
    - beanstalk
    - cache
    environment:
    - "DB_PORT=3306"
    - "DB_HOST=mysql"
    - "REDIS_PORT=6379"
    - "REDIS_HOST=redis"
  mysql:
    image: mysql:5.7.18
    environment:
    - "MYSQL_ROOT_PASSWORD=secret"
    - "MYSQL_DATABASE=docker"
    ports:
    - "3306:3306"
  pgsql:
    image: postgres:10.1
    restart: always
    environment:
    - POSTGRES_DB=docker
    - POSTGRES_USER=root
    - POSTGRES_PASSWORD=secret
    ports:
    - 5431:5431
    volumes:
    - ./.docker/conf/postgres/:/docker-entrypoint-initdb.d/
  redis:
    image: redis:3.0
    ports:
    - "6379:6379"
  beanstalk:
    image: schickling/beanstalkd
    ports:
    -  "11300:11300"
  cache:
    image: memcached:alpine
    ports:
    - "11211:11211"

我知道我也添加了 Mysql 图像。当我连接到 Mysql 映像时,它正在工作。当我连接到 Postgres 时,出现错误。

这是 env 文件中用于连接 Postgres 的数据库设置。

DB_CONNECTION=pgsql
DB_HOST=pgsql
DB_PORT=5431
DB_DATABASE=docker
DB_USERNAME=root
DB_PASSWORD=secret

当我像这样在终端中运行迁移时

docker-compose exec app php artisan migrate --seed

我得到了这个错误。

In Connection.php line 647:

  could not find driver (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations)  




In PDOConnection.php line 50:

  could not find driver  


In PDOConnection.php line 46:

  could not find driver  

我的安装有什么问题?

标签: laravelpostgresqldockerdocker-composephp-pgsql

解决方案


您没有添加 docker/app.docker 详细信息,但如果这是运行 PHP,则需要确保在此容器上安装了 php7-pgsql 和 php7-pdo。驱动程序通常是 phpX-mysql 或 phpX-pgsql。X 是可选的版本号,具体取决于您为 php 使用的存储库。即使用默认的ubuntu repo php-pgsql 就可以了,但是对于高山图像,您必须使用php7-pgsql。使用sury repo,您​​必须使用特定版本,即php7.2-pgsql。


推荐阅读