Laravel in Docker giving 'php_network_getaddresses: getaddrinfo failed: Name does not resolve'
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Solving the Docker Networking Nightmare: When Laravel Fails to Resolve Hostnames
As a senior developer working with containerized applications, we often run into frustrating networking issues when deploying frameworks like Laravel within Docker environments. The scenario you've described—where external tools can connect successfully, but an internal command fails with a Name does not resolve error—is a classic symptom of misconfigured container communication or timing issues.
This post will dive deep into why this happens in your specific setup and provide concrete solutions to ensure seamless database migrations within your Laravel Docker stack.
Understanding the Error: php_network_getaddresses: getaddrinfo failed
The error you are encountering, SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name does not resolve, indicates a failure in DNS resolution inside the PHP container. When your Laravel application attempts to connect to the database using the hostname defined in your .env file (e.g., DB_HOST=docker_database), the system cannot translate that service name into an IP address.
Crucially, you can see the database exists externally via Sequel Pro, confirming the MySQL service is running and reachable on the Docker network. The problem lies specifically in the communication path between the PHP container and the MySQL container.
Why Docker Networking Gets Tricky
In a standard Docker Compose setup, services communicate by resolving hostnames to internal IP addresses provided by Docker's internal DNS. When this fails, it usually points to one of three potential causes:
- Race Condition (The Most Common Culprit): The PHP container starts up and tries to connect to the database before the MySQL server inside the
databasecontainer is fully initialized and listening for connections. - Network Misconfiguration: While your
docker-compose.ymllooks sound, sometimes explicit network definitions or volume mounts can inadvertently disrupt the default bridge networking setup. - PHP/MySQL Version Differences: Less common, but subtle differences in how PHP handles hostname resolution across different base images can introduce instability.
Practical Solutions for Reliable Migrations
To resolve this connectivity issue and ensure your Laravel migrations run successfully every time, we need to focus on making the services aware of each other's readiness.
1. Implement Service Readiness Waiting
The most effective fix is to introduce a delay or a health check mechanism to ensure the database container is fully operational before the PHP container attempts to execute commands like migrate. We can achieve this using a simple sleep command in your entrypoint script or, more robustly, by setting up dependency waiting.
A simpler approach for initial testing is adding an explicit wait:
# In your terminal, wait a few seconds before running migrations
docker-compose run --rm php bash -c "sleep 10 && php artisan migrate"
For production reliability, we look at using depends_on correctly, but often, explicitly waiting for the service to be ready is necessary. While Docker Compose handles basic startup order, complex application initialization requires more finesse. When building robust Laravel applications, understanding these dependencies is crucial, as emphasized by best practices in modern application deployment strategies found on platforms like laravelcompany.com.
2. Verify Network Connectivity Internally
Before assuming a timing issue, let's confirm the network path inside the PHP container. You can execute a simple network diagnostic command inside your running PHP container:
docker exec -it my_app ping docker_database
If this command fails to resolve the name, it confirms the DNS lookup failure is happening within the PHP environment itself, pointing back to a networking configuration issue that needs deeper investigation, perhaps involving custom network drivers or host file adjustments if you were running outside of standard Docker Compose.
3. Review Your docker-compose.yml (Best Practice)
Your provided setup is generally correct for service discovery:
services:
database:
image: mysql:8.0
# ... configuration ...
php:
# ... configuration ...
depends_on:
- database # This ensures MySQL starts first
This structure correctly establishes the dependency order. If the issue persists, ensure that your Dockerfile for PHP is not introducing any custom networking limitations. For complex deployments involving many interdependent services, leveraging a dedicated, persistent network (as you have done with networks: laravel) is vital for stable communication.
Conclusion
The Name does not resolve error during Laravel migrations inside Docker is almost always a symptom of a timing issue or a subtle DNS resolution failure between service containers rather than a fundamental flaw in the docker-compose.yml. By implementing explicit waiting strategies and rigorously verifying internal network connectivity, you can eliminate this frustrating hurdle. Focus on readiness checks, and your Laravel application will run smoothly within its containerized environment.