Pathros
Pathros

Reputation: 10730

Laravel with docker database error: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name does not resolve

I know there're similar questions but the answers do not help.

I have this server where I have previously installed the following two repositories:

Now I have added a Laravel Application. The Laravel application is now working, the domain is correctly resolved by the Nginx proxy and Letsencrypt is working properly.

However, the problem is now with the database connection. When I run php artisan migrate --seed the following errors show up:

In Connection.php line 671:

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

In Connector.php line 70: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name does not resolve

In Connector.php line 70: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name does not resolve

This is my docker-compose.yml file:

version: '3'

networks:
    default:
       external:
         name: ${NETWORK}

services:
  nginx:
    image: nginx:stable-alpine
    container_name: app-webserver
    expose:
      - "80"
      - "443"
    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    environment:
      - VIRTUAL_HOST=${VIRTUAL_HOST}
      - LETSENCRYPT_HOST=${LETSENCRYPT_HOST}
      - LETSENCRYPT_EMAIL=${LETSENCRYPT_EMAIL}
    depends_on:
      - php
      - mysql

  mysql:
    image: mysql:5.7.29
    container_name: app-mysql
    restart: unless-stopped
    tty: true
    expose:
      - "3306"
    environment:
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql

  php:
    build:
      context: .
      dockerfile: php.dockerfile
    container_name: app-php
    volumes:
      - ./src:/var/www/html
    environment:
      - VIRTUAL_HOST=${VIRTUAL_HOST}
      - LETSENCRYPT_HOST=${LETSENCRYPT_HOST}
      - LETSENCRYPT_EMAIL=${LETSENCRYPT_EMAIL}
    expose:
      - "9000"

  composer:
    image: composer:latest
    container_name: composer
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
    depends_on:
      - php

  npm:
    image: node:13.7
    container_name: npm
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
    entrypoint: ['npm']

  artisan:
    build:
      context: .
      dockerfile: php.dockerfile
    container_name: artisan
    volumes:
      - ./src:/var/www/html
    depends_on:
      - mysql
    working_dir: /var/www/html
    entrypoint: ['php', '/var/www/html/artisan']

Inside the Laravel .env file I have tried all possible values for DB_HOST:

DB_HOST=localhost
DB_HOST=127.0.0.1
DB_HOST=mysql
DB_HOST=app-mysql

Currently, I have it set up for the MySQL container name:

DB_HOST=app-mysql

Other values are:

DB_CONNECTION=mysql
DB_PORT=3306

And I still get that error.

I have tried the ping command from the app-php container to the app-mysql container as follows:

$ docker exec -ti app-php ping app-mysql

I do get response:

64 bytes from 172.18.x.x: seq=0 ttl=64 time=0.132 ms
64 bytes from 172.18.x.x: seq=1 ttl=64 time=0.240 ms
64 bytes from 172.18.x.x: seq=2 ttl=64 time=0.207 ms
64 bytes from 172.18.x.x: seq=3 ttl=64 time=0.208 ms

However, if I run:

$ docker-compose run --rm artisan ping app-mysql

I get the following error:

Command "ping" is not defined.

Now if I run docker-compose up in order to see what's going on (without -d), I see the following, as for MySQL:

app-mysql | 2020-07-29T15:43:24.159355Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
app-mysql | 2020-07-29T15:43:24.159523Z 0 [Note] IPv6 is available.
app-mysql | 2020-07-29T15:43:24.159582Z 0 [Note]   - '::' resolves to '::';
app-mysql | 2020-07-29T15:43:24.159688Z 0 [Note] Server socket created on IP: '::'.

Does anybody out there know how to fix this? Why can't Artisan or the PHP container connect to the database container?

workaround

I solved this using Laradock instead

Upvotes: 2

Views: 7227

Answers (3)

Amir Rakhmonov
Amir Rakhmonov

Reputation: 49

In short: add Sail before PHP command.

Detailed:

When you prompt a command, you use the local PHP library. That's why you got an error because your local PHP is addressing to docker-mysql connection, on local machine you don't haven a connection like this. When you are using Laravel Sail, you should prompt the command

sail PHP artisan migrate

which means, you are prompting from Docker's PHP library

Upvotes: 3

vins
vins

Reputation: 539

I hope this could be helpful for those who have gone through the solution and not yet succeeded. I had similar problem and tried to change the DB_HOST localhost to 127.0.0.1 also with the name of the container/db host (as mentioned in docker-compose), yet I could not get this error resolved. So I updated my .env file like this:

DB_CONNECTION=mysql
DB_HOST=host.docker.internal
DB_PORT=3308 or any other port
DB_DATABASE=your_database_name
DB_USERNAME=your_user_name
DB_PASSWORD=your_password

which started working. I am not assuring it works 100% but you could give a try for this in case other option are not working.

Cheers!

Upvotes: 5

user2932688
user2932688

Reputation: 1704

Since you are doing container_name: app-mysql inside of docker compose network you should use host name app-mysql

the easies way to check is to connect to php container and try ping app-mysql and then use mysql console to test connection. In this way you at least can find where is the problem.

by the way, since it is docker compose - you must do docker compose down and docker compose up -d each time you are doing changes in docker-compose.yml

Upvotes: 2

Related Questions