Search code examples
laravel-artisanlaravel-8laravel-sail

can't use migrate with sail laravel


Hi I've just tried and to install Laravel 8 with sail and I've run into a problem

What I've done is to first run the curl one-liner from laravel's own page

curl -s https://laravel.build/sail-test | bash

once that finished I did as it said and run

cd sail-test && ./vendor/bin/sail up

Everything started as it should, I can connect to the database, I can see the site on http://localhost however I get the following error when trying to migrate:

$ sail artisan migrate:install

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002] No route to host (SQL: create table `migrations` (`id` int unsigned not null auto_increment primary key, `migration` varchar(255) not null, `batch` int not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:678
    674▕         // If an exception occurs when attempting to run a query, we'll format the error
    675▕         // message to include the bindings with SQL, which will make this exception a
    676▕         // lot more helpful to the developer instead of just the database's errors.
    677▕         catch (Exception $e) {
  ➜ 678▕             throw new QueryException(
    679▕                 $query, $this->prepareBindings($bindings), $e
    680▕             );
    681▕         }
    682▕ 

      +29 vendor frames 
  30  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

Do i need to change anything in the .env file or do I need to do any other changes first?


Solution

  • First (this command will return your mysql container IP address):

    $ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name_or_container_ID>
    

    Second in your .env file:

    DB_HOST=<ip_address_returned_from_command_above>
    

    And then:

    $ sail artisan migrate
    

    Edit:

    Just add to your DB_HOST env var the name of your database container:

    ...
    DB_HOST=mysql
    ...