Certainly! The error message:
SQLSTATE[HY000] [2002] No such file or directory (Connection: mariadb, SQL: ...)
means that your Laravel app inside the laravel.test container cannot connect to the MariaDB server using the configured settings.
Key points:
- You can access the database via phpMyAdmin, so MariaDB is running and accessible from outside the container.
- Your
.envis set to:DB_CONNECTION=mariadb DB_HOST=mariadb DB_PORT=3306
Solution Steps
1. Use the Correct DB_CONNECTION
Laravel's default database drivers are: mysql, pgsql, sqlite, sqlsrv.
There is no built-in mariadb driver.
You should use mysql as the connection type, even if you are running MariaDB.
Change your .env to:
DB_CONNECTION=mysql
DB_HOST=mariadb
DB_PORT=3306
2. Rebuild and Restart Sail
After changing .env, restart your containers to ensure the new environment variables are loaded:
./vendor/bin/sail down
./vendor/bin/sail up -d
3. Check Database Credentials
Make sure that the values for DB_DATABASE, DB_USERNAME, and DB_PASSWORD in your .env match those in your docker-compose.yml under the mariadb service.
Example:
DB_DATABASE=your_db
DB_USERNAME=your_user
DB_PASSWORD=your_password
And in docker-compose.yml:
environment:
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
4. Clear Config Cache
Sometimes Laravel caches the config. Run this inside your Sail container:
./vendor/bin/sail artisan config:clear
5. Test the Connection
Try running a migration to test the connection:
./vendor/bin/sail artisan migrate
Summary
Your main issue is using DB_CONNECTION=mariadb. Change it to mysql.
Final .env example:
DB_CONNECTION=mysql
DB_HOST=mariadb
DB_PORT=3306
DB_DATABASE=your_db
DB_USERNAME=your_user
DB_PASSWORD=your_password
Let me know if you still have issues after making these changes!