Laravel Sail - SQLSTATE[HY000] [2002] No such file or directory
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Solving the Docker Migration Headache: Why Laravel Sail Gets SQLSTATE[HY000] [2002]
As a senior developer working with containerized applications, I frequently encounter frustrating issues when moving setup scripts or migrations between environments. The scenario you've described—where Laravel Sail works perfectly on one machine but throws a SQLSTATE[HY000] [2002] No such file or directory error when running migrations on another machine—is a classic symptom of misconfigured Docker networking or incorrect path resolution within the container environment.
This post will dive deep into why this happens, analyze your configuration, and provide robust solutions to ensure your Laravel application's database operations succeed, regardless of where you execute them.
Understanding the Error: The Root Cause in Docker
The error SQLSTATE[HY000] [2002] No such file or directory during a database operation (like creating the migrations table) almost always points to one of two core problems within a containerized setup:
- Missing Socket File: The MySQL client cannot find the necessary socket file (
/var/run/mysqld/mysqld.sock) required to communicate with the MySQL server running inside the Docker container. - Network Misconfiguration: The application inside the container cannot successfully resolve the hostname or IP address of the database service, meaning it can't establish a connection to the MySQL server at all.
When you run docker-compose up, Sail sets up an isolated network. If you clone the repository and try to execute commands manually (or via a different Docker context), the internal networking assumptions might break down if the environment variables aren't perfectly mirrored or if permissions are subtly different.
Analyzing Your Configuration
Let’s examine the configuration you provided to see where the disconnect might be:
Database Configuration Review
Your database.php file correctly pulls connection details from environment variables, which is the Laravel standard and highly recommended:
// In config/database.php snippet
'url' => env('DATABASE_URL'),
// ... other settings
And your .env file defines the connection:
DB_CONNECTION=mysql
DB_HOST=mysql <-- This is the critical line
DB_PORT=3306
DB_DATABASE=code_challenge
DB_USERNAME=root
DB_PASSWORD=
The use of DB_HOST=mysql is correct if you are running the command from within a container that is part of the same Docker network as the MySQL service (which Sail handles automatically). However, if you are trying to run commands outside the standard Sail execution flow or in an environment where the host machine's networking interferes, this internal hostname resolution can fail.
Practical Solutions for Cross-Machine Consistency
Since your provided configuration looks logically sound for a typical Sail setup, the issue is likely environmental consistency between machines. Here are the steps to ensure stability:
1. Rely Solely on docker-compose (The Sail Way)
The most robust way to handle database operations in a Laravel/Sail environment is never to run migration commands directly outside of the orchestrated environment. Always execute commands via Docker Compose:
# Execute migrations within the context of the running services
docker-compose exec app php artisan migrate
This forces the command to run inside the container where all necessary networking and permissions are correctly established by Sail, bypassing external host configuration issues entirely. As we discussed on Laravel Company, leveraging these tools ensures consistency across development, staging, and production environments.
2. Verify Docker Networking and Volume Mounts
If you are attempting to run commands outside the Sail context (e.g., using a separate Docker container manually), ensure that any necessary volumes or network aliases are correctly mapped. Sometimes, running the command as the root user inside the container might encounter permission issues related to socket access if not handled properly by the base image setup.
3. Check MySQL Service Status
Before attempting migrations, confirm the database service is actually running healthily on the target machine:
docker-compose ps
# Ensure the mysql service is 'Up'
If the service is down or failing to start, no application inside the container will be able to connect, leading to connection errors that can manifest as file/socket errors.
Conclusion
The SQLSTATE[HY000] [2002] error in a Dockerized Laravel setup usually signals a failure in internal service communication rather than an error in your PHP code or database credentials themselves. By adhering to the principle of running application logic within the orchestrated environment provided by Laravel Sail—specifically using docker-compose exec for tasks like migrations—you eliminate external environmental variables and networking ambiguities, ensuring that your setup remains consistent and reliable across any machine. Focus on leveraging Docker Compose as your single source of truth for execution environments.