Laravel Sail 工作迁移或与数据库联系

Laravel Sail work migrate or contact with database

我运行幼虫8sail他工作正常

我的文件内容是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

如果我把 .env DB_HOST=mariadb

I put mariadb because i used mariadb not mysql

我不能 运行 php artisan migrate 如果 .env DB_HOST=mariadb 必须更改为 DB_HOST=127.0.0.1

并使用 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()

如果我更改为 DB_HOST=127.0.0.1 迁移工作并停止站点

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

我可以 运行 如果 return DB_HOST 我的网站工作正常并且 运行 迁移使用 sail shell 然后 php artisan migrate

不是从本地计算机调用 php artisan migrate,而是从容器调用它。作为 per documentation 你可以使用 sail artisan migrate.