Symfony,Docker 容器 fpm,apache:找不到驱动程序 [doctrine:database:create]

Symfony, Docker container fpm, apache : could not find driver [doctrine:database:create]

我正在尝试使用 docker 和 apache、php fpm、symfony、mysql db 的容器创建 symfony 5.4 项目。 但实际上我没有成功创建我的数据库,我得到这个错误:

In AbstractMySQLDriver.php line 128:
                                                          
  An exception occurred in driver: could not find driver  
                                                          

In Exception.php line 18:
                         
  could not find driver  
                         

In PDOConnection.php line 40:
                         
  could not find driver  

这个学说 conf : config/packages/doctrine.yaml:

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'
        
        # IMPORTANT: You MUST configure your server version,
        # either here or in the DATABASE_URL env var (see .env file)
        #server_version: '13'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

这是已激活的扩展程序:

;extension=bz2
extension=curl
;extension=ffi
;extension=ftp
;extension=fileinfo
extension=gd2
;extension=gettext
;extension=gmp
;extension=intl
;extension=imap
;extension=ldap
extension=mbstring
;extension=exif      ; Must be after mbstring as it depends on it
extension=mysqli
;extension=oci8_12c  ; Use with Oracle Database 12c Instant Client
;extension=odbc
;extension=openssl
;extension=pdo_firebird
extension=pdo_mysql
;extension=pdo_oci
;extension=pdo_odbc
;extension=pdo_pgsql
;extension=pdo_sqlite
;extension=pgsql
;extension=shmop

; The MIBS data available in the PHP distribution must be installed.
; See http://www.php.net/manual/en/snmp.installation.php
;extension=snmp

;extension=soap
extension=sockets
;extension=sodium
extension=sqlite3
;extension=tidy
;extension=xmlrpc

这是我的 Docker-compose-stack-file:

version: '3.8'

services:
   myApp-back-apache:
    image: localhost:5000/apache
    working_dir: /var/www
    volumes:
      - type: bind
        source: myApp-back
        target: /var/www
     
    ports:
      - 8081:80
    environment:
      HTTPD_FPM_HOST: myApp-back-fpm
      HTTPD_FPM_PORT: 9000

  myApp-back-fpm:
    image: php:7.4-fpm
    working_dir: /var/www/html
    volumes:
      - type: bind
        source: myApp-back
        target: /var/www/html
  
  myApp-back-db:
    image: "mariadb:10.6.5"
    environment:  
      MYSQL_ROOT_USER: root
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: root
    volumes:
      - myApp-back-db:/var/lib/mysql
    ports:
      - "3309:3306"

volumes:
  myApp-back-db:
    external: true

和我的文件 .env:

DATABASE_URL="mysql://root:root@127.0.0.1:3309/db_name?serverVersion=5.7&charset=utf8mb4"
  1. 你确定你是 运行 php -m 在 docker 容器上而不是你的主机?(很常见的错误)

  2. 在您的 symfony(fpm) docker 容器中 127.0.0.1localhost 解析为容器本身,而不是您的主机 machine数据库在端口 3309 上公开。尝试将 DATABASE_URL 更改为 mysql://root:root@myApp-back-db:3306。 此外,如果您使用 mac 或 windows.

    ,您可以尝试将 url 设置为 host.docker.internal
  3. 为 fpm 创建一个 docker 文件并安装 mysql ext:

docker-composer:

myApp-back-fpm:
    build:
        context: .
    working_dir: /var/www/html
    volumes:
      - type: bind
        source: myApp-back
        target: /var/www/html

Docker 文件:

FROM php:7.4-fpm

RUN docker-php-ext-install pdo pdo_mysql;