当我尝试使用 postgres 在 laravel 中迁移,而在 docker 中迁移 运行 时,我遇到了一个错误,如找不到驱动程序

When i try to migrate in laravel with postgres while running in docker i got an error like could not find driver

当我尝试 运行 php artisan 在我的 运行ning 容器中迁移时,出现错误,如找不到我认为没有正确安装 postgres 的驱动程序,我包含了代码和下面的错误帮助我找到这个。

我的docker文件

FROM php:fpm-alpine

RUN set -ex \
  && apk --no-cache add \
    postgresql-dev

RUN docker-php-ext-install pdo pgsql

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

COPY . .

RUN composer install

WORKDIR /var/www/html

CMD php artisan serve --host=0.0.0.0 --port=8080

EXPOSE 8080

我的docker-compose.yml文件

version: '3'

services:
  php_3:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./:/var/www/html
    ports:
      - "8080:8080"

  postgres_3:
    image: postgres:12.3-alpine
    restart: unless-stopped
    tty: true
    ports:
      - "5431:5432"
    volumes:
      - ./docker/postgres:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: root
      POSTGRES_DB: lara_3

.env 文件

DB_CONNECTION=pgsql
DB_HOST=postgres_3
DB_PORT=5432
DB_DATABASE=lara_3
DB_USERNAME=root
DB_PASSWORD=root

我的容器是 运行ning 并且 laravel 也是 运行ning 但是如果我试图用我的 docker 容器迁移表 docker-compose exec php_3 sh 我得到像这样的错误。

/var/www/html # php artisan migrate

   Illuminate\Database\QueryException 

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

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:703
    699▕         // If an exception occurs when attempting to run a query, we'll format the error
    700▕         // message to include the bindings with SQL, which will make this exception a
    701▕         // lot more helpful to the developer instead of just the database's errors.
    702▕         catch (Exception $e) {
  ➜ 703▕             throw new QueryException(
    704▕                 $query, $this->prepareBindings($bindings), $e
    705▕             );
    706▕         }
    707▕     }

      +33 vendor frames 
  34  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

请帮我解决这个问题。

运行 命令并安装 postgres 扩展:

docker-php-ext-install pdo_pgsql pgsql