3
votes

In my container when I run php artisan migrate I keep getting this error

In Connector.php line 67:

  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known


In Connector.php line 67:

  PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known

Here's my .ENV file

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:etukbJpSLgbRsdf5uOEGtLT5Qw+XB6y06Q38HPr
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://127.0.0.1:8000/

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=project
DB_USERNAME=root
DB_PASSWORD=


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

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

I tried to change DB_HOST to mysql but still same result as well as changed it to mariadb

Here's my docker-compose file

version: '3'
services:

 # The Application
  app:
    build:
      context: ./
      dockerfile: app.dockerfile
    working_dir: /var/www
    volumes:
      - ../.:/var/www
      - ./custom.ini:/usr/local/etc/php/conf.d/custom.ini
    environment:
      - "DB_PORT=3306"
      - "DB_HOST=database"

 # The Web Server
  web:
    build:
      context: ./
      dockerfile: web.dockerfile
    working_dir: /var/www
    volumes:
      - ../.:/var/www
      - ./vhost.conf:/etc/nginx/conf.d/default.conf
    ports:
      - 8082:80

  # The Database
  database:
    image: mysql:5.7
    volumes:
      - dbdata:/var/lib/mysql
    environment:
      MYSQL_USER: ${DB_USERNAME}
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
    ports:
        - "33061:3306"

volumes:
  dbdata:

I searched for solution online but none of them helped. Does anyone have a suggestion as to why I keep getting that error?

4
Why did you write DB_HOST=localhost when using a seperate container?Nico Haase

4 Answers

7
votes

You should add a container_name to your database service

database:
image: mysql:5.7
container_name: database

And reference that name inside your .env MYSQL_HOST as:

MYSQL_CONNECTION=mysql
MYSQL_HOST=database
MYSQL_PORT=3306

rather than using localhost

you should also connect the database service and app using a network

add a networks block to the end of the docker-compose

networks:
  app-network:
    driver: bridge

and add this block to the end of your database serice and app servce

demo_db:
   .
   .
   .    
   networks:
      - app-network
  app:
   .
   .
   .
   networks:
      - app-network

here is a working demo for dockerizing a simple laravel app

https://bitbucket.org/RamiAmro/hello-laravel-docker/

0
votes

If you are connecting to mysql as an external docker container, make sure that you implemented this solution https://stackoverflow.com/a/38089080/1770571

0
votes

I had the same problem. In my case I found out that it was visible in the sail log that there was a problem with recovering the InnoDB database

InnoDB error message

!! CAUTIOUS (THIS WILL DELETE THE WHOLE DATABASE) !!

As it was my development environment, I tried deleting the existing volume

./vendor/bin/sail down -v

And then bringing up a new container again

./vendor/bin/sail up

0
votes

There's now Laravel Sail for anyone still interested.

https://laravel.com/docs/8.x/sail

Laravel Sail is a light-weight command-line interface for interacting with Laravel's default Docker development environment. Sail provides a great starting point for building a Laravel application using PHP, MySQL, and Redis without requiring prior Docker experience.