Search code examples
laraveldockerlaravel-sail

Laravel Sail work migrate or contact with database


I run larvae 8 sail he work fine

my file content is docker-compose.yml

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    rami.dev:
        build:
            context: ./vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mariadb
    mariadb:
        image: 'mariadb:10'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmariadb:/var/lib/mysql'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}" , "-h", "localhost"]
          retries: 3
          timeout: 5s
    phpmyadmin:
        image: phpmyadmin
        restart: always
        ports:
            - 8080:80
        environment:
            PMA_ARBITRARY: 1
            PMA_HOST: 'mariadb'
            PMA_USER: '${DB_USERNAME}'
            PMA_PASSWORD: '${DB_PASSWORD}'
        networks:
            - sail
    selenium:
        image: 'selenium/standalone-chrome'
        volumes:
                - '/dev/shm:/dev/shm'
        networks:
            - sail
    mailhog:
        image: 'mailhog/mailhog:latest'
        ports:
            - '${FORWARD_MAILHOG_PORT:-1025}:1025'
            - '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
        networks:
            - sail
networks:
    sail:
        driver: bridge
volumes:
    sailmariadb:
        driver: local

if I put .env DB_HOST=mariadb

I put mariadb because i used mariadb not mysql

put I cant run php artisan migrate if .env DB_HOST=mariadb must change to DB_HOST=127.0.0.1

and get this error with DB_HOST=mariadb

php artisan migrate

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: select * from information_schema.tables where table_schema = ramiyusu_live and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:692
    688▕         // If an exception occurs when attempting to run a query, we'll format the error
    689▕         // message to include the bindings with SQL, which will make this exception a
    690▕         // lot more helpful to the developer instead of just the database's errors.
    691▕         catch (Exception $e) {
  ➜ 692▕             throw new QueryException(
    693▕                 $query, $this->prepareBindings($bindings), $e
    694▕             );
    695▕         }
    696▕     }

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

if I change to DB_HOST=127.0.0.1 migrate work and site stop

Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `users` where `id` = 1 limit 1)
http://localhost/

i can run both if return DB_HOST my site work okay and to run migrate use sail shell then php artisan migrate


Solution

  • Instead of calling php artisan migrate from your local machine, call it from the container. As per documentation you can use sail artisan migrate.