Docker Laravel Mysql: could not find driver
When running docker-compose up and executing composer commands, you might encounter this specific error:
In Connection.php line 664: could not find driver (SQL: select id, name from users ... In Connector.php line 68: could not find driver
This message is confusing because your MySQL connection logic seems valid. You can successfully run mysql -h db inside the container via docker-compose exec web bash, and your .env file correctly points to the database service. The root cause lies in how PHP interacts with extensions versus how Docker Compose resolves network names.
The error could not find driver indicates that the PHP extension required for the MySQL database connection (typically pdo_mysql) is not installed or enabled in your PHP environment. While standard PHP images include this by default, custom builds often strip it out to minimize footprint. Your Dockerfile for the webserver uses FROM php:7.2.19-apache-stretch. Unless you explicitly install the MySQL client libraries and PDO drivers during the build process, the PHP runtime cannot communicate with the database, even if the Apache module is present.
A critical detail often overlooked in these setups involves network resolution. In your Docker Compose configuration, the web service references the database using the service name db. This works perfectly fine if you are making HTTP requests from within the web container to the DB container. However, when installing PHP extensions or running Composer to install packages like laravel/framework, the build process might attempt to verify network connectivity or resolve hostnames differently depending on the environment variables set during the build.
To resolve this, you must ensure that the necessary PHP development and database driver libraries are installed before Apache starts. Simply installing mysql-client is not enough; you need the PDO MySQL extension. The fix involves modifying your Dockerfile to install php-pdo, php-mysql, or the specific package names provided by the Debian/Stretch repositories used by the base image.
Here is an updated snippet of the installation step in your Dockerfile:
RUN apt-get update -y && \
apt-get install -y \
curl nano libapache2-mod-geoip git zip unzip mysql-client \
php-pdo php-mysql php-mbstring php-curl php-gd \
&& docker-php-ext-install pdo_mysql
After installing the packages, it is essential to ensure the extension is enabled. You can check this by listing active extensions:
php -m | grep pdo
If pdo_mysql does not appear in the output, the installation did not register the extension correctly. You may need to enable it via an ini file or a symlink if the package manager didn't handle it automatically.
Another potential pitfall is the order of operations in your startup script. Your web service runs /usr/local/bin/apache2_install_composer_dependencies.sh. If this script installs Composer dependencies before ensuring the PHP environment is fully configured, you might encounter issues where the application tries to connect to the database immediately upon boot but fails because the driver isn't loaded yet. Ensure that any custom initialization scripts wait for the necessary system services to be ready or that the extension installation happens before any Apache restarts.
Finally, verify that your Docker Compose network configuration allows the web container to resolve the db hostname. While you are using the default network, which should handle this by design, explicitly defining a custom network can sometimes help avoid DNS resolution ambiguities in complex setups. However, in most standard cases, ensuring the PHP driver is installed and enabled is the primary solution to the "could not find driver" error.