Laravel Sail is a great all in one development environment that allows developers to spin up Laravel applications quickly and easily using Docker. However, after picking up a recent project that I hadn’t worked on for a couple months I encountered a strange error:
Code language: CSS (css)
[Illuminate\Database\QueryException] could not find driver (SQL: select *...
It seemed Laravel was complaining about now being able to find the MySQL driver needed to access the database, which was weird since this project had been running previously.
A couple Stack Overflow searches led me to an answer that recommended running
composer update and
composer require doctrine/dbal to no effect.
However, some of the other answers I found led me to believe that the extension in PHP itself might be the problem. Because Sail uses Docker, the default answer of modifying
php.ini doesn’t work in this case. This is where Sail’s publish option comes in handy.
Sail:publish to the rescue
Like many other Laravel packages and services, Sail comes with a publish subcommand, which exposes Sail’s configuration files.
After running this command, the Dockerfiles and other configuration files used by Laravel Sail will be placed within a /docker directory in your application’s root directory.https://laravel.com/docs/9.x/sail#sail-customization
This means we can navigate to the root directory of our project, into the
/docker directory and then find the PHP version that we’re currently using (for me it was 8.1) and navigate into that directory. Inside that directory you should find a file called
php.ini and adding
extension = pdo_mysql.so to this file will make a change to the php.ini file inside the Docker image. However, that change does not happen automatically.
Rebuilding your Sail Docker images
Now that we’ve updated the Dockerfiles that power the Sail images, we need to rebuild them for our changes to take effect. With Sail not currently running, you can run
sail build --no-cache. After you come back from getting a cup of coffee, your Docker images will be rebuilt and you can start Sail with a working database connection!