LARAVEL SAIL 出错 [php_network_getaddresses: getaddrinfo failed]

Posted

技术标签:

【中文标题】LARAVEL SAIL 出错 [php_network_getaddresses: getaddrinfo failed]【英文标题】:Error with LARAVEL SAIL [php_network_getaddresses: getaddrinfo failed] 【发布时间】:2021-04-30 14:18:21 【问题描述】:

我遇到了laravel sail 的问题,碰巧我刚刚克隆了存储库并且它没有改变,docker-compose.yml 完全完好无损,但是,在执行 ./vendor/bin/sail up 之后,我启动了容器,当我尝试进行迁移会引发以下错误

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: 名称解析暂时失败(SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')

Docker-compose.yml

    # For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
        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:
            - mysql
            # - pgsql
            - redis
            # - selenium
    # selenium:
    #     image: 'selenium/standalone-chrome'
    #     volumes:
    #         - '/dev/shm:/dev/shm'
    #     networks:
    #         - sail
    mysql:
        image: 'mysql:8.0'
        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:
            - 'sailmysql:/var/lib/mysql'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "mysqladmin", "ping"]
#    pgsql:
#        image: postgres:13
#        ports:
#            - '$FORWARD_DB_PORT:-5432:5432'
#        environment:
#            PGPASSWORD: '$DB_PASSWORD:-secret'
#            POSTGRES_DB: '$DB_DATABASE'
#            POSTGRES_USER: '$DB_USERNAME'
#            POSTGRES_PASSWORD: '$DB_PASSWORD:-secret'
#        volumes:
#            - 'sailpostgresql:/var/lib/postgresql/data'
#        networks:
#            - sail
    redis:
        image: 'redis:alpine'
        ports:
            - '$FORWARD_REDIS_PORT:-6379:6379'
        volumes:
            - 'sailredis:/data'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "redis-cli", "ping"]
    # memcached:
    #     image: 'memcached:alpine'
    #     ports:
    #         - '11211:11211'
    #     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:
    sailmysql:
        driver: local
#    sailpostgresql:
#        driver: local
    sailredis:
        driver: local

.env 文件

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:DXP6IaYhvLqI2fOLq672Bm2xilfehXgjTyrxj+8dcR8=
APP_DEBUG=true
APP_URL=http://ficha.test

LOG_CHANNEL=stack
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=ficha_caractetizacion
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=memcached

REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="$APP_NAME"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="$PUSHER_APP_KEY"
MIX_PUSHER_APP_CLUSTER="$PUSHER_APP_CLUSTER"

【问题讨论】:

DB_HOST=mysql 这是一个有效的主机名吗? 嗯...这是容器的名称,我认为这是有效的。我尝试使用其他主机,但无法正常工作 使用sail时DB HOST是正确的。我也遇到了这个 redis 问题,但它似乎不会影响我的应用程序,只是在错误日志中乱扔垃圾。我仍然可以使用 redis。 【参考方案1】:

你确定你在容器里吗? 如果您尝试在容器外运行命令,则可以运行 docker exec -it <container_id> php artisan migratesail shell,然后运行你的命令

【讨论】:

以上是关于LARAVEL SAIL 出错 [php_network_getaddresses: getaddrinfo failed]的主要内容,如果未能解决你的问题,请参考以下文章

Laravel Sail 不会在 Ubuntu 20.04 上构建 - groupadd:无效的组 ID 'sail'

带有 Laravel Sail 和版本控制的 Laravel 8 开发项目

在 Laravel 上使用 Sail 时没有匹配的清单错误

laravel、sail shell 和sails artisan 命令停止工作

无法在 .env 上为 Laravel Sail 设置 APP_PORT

Laravel Sail 数据库和用户未创建