Fixing Laravel Migration Failure in Windows Docker: Connecting to MySQL Container

When running php artisan migrate in a Laravel project on Windows with Docker, the command cannot reach the MySQL container, producing a SQLSTATE[HY000] [2002] error, and the issue is resolved by wrapping the artisan command in a custom shell script that executes inside the container.

php Courses
php Courses
php Courses
Fixing Laravel Migration Failure in Windows Docker: Connecting to MySQL Container

Problem: When running php artisan migrate in a Laravel project on Windows using Docker containers for PHP and MySQL, the command cannot find the database, resulting in a

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed

error.

Illuminate\Database\QueryException  : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: 未知  (SQL: select * from ***)
  at E:\project\vendor\laravel\framework\src\Illuminate\Database\Connection.php:669
  > 669|             throw new QueryException(
  670|                 $query, $this->prepareBindings($bindings), $e    671|             );
  672|         }

Exception trace:

1   Doctrine\DBAL\Driver\PDOException::("SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: 知 ")
    E:\project\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:31
2   PDOException::("PDO::__construct(): php_network_getaddresses: getaddrinfo failed: 知 ")
    E:\project\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:27
Please use the argument -v to see more details.

Cause: In Windows git‑bash the hostname of the MySQL container cannot be resolved, so the Laravel application cannot connect to the database.

Solution: Write a small shell wrapper (named phpartisan) that runs the desired php artisan command inside the running container, then invoke this wrapper instead of calling php artisan directly.

#!/usr/bin/env sh

# 进入容器 执行相应php artisan命令
docker exec -i 容器名称 sh -c "cd /项目的容器路径 && php artisan $*"

Usage: Replace commands like php artisan migrate with phpartisan migrate (or phpartisan * for any artisan command) to execute the migration successfully.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

migrationDockermysqlPHP
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.