themystery
themystery

Reputation: 63

Docker + Laravel issue [SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution]

I have docker installed on ubuntu machine and I'm trying to run a laravel app.

MySQL service has service_name: mysql in docker-compose.yml file and .env file has DB_HOST=mysql.

As I remember .env file should figure out that DB_HOST=mysql points to the mysql docker service IP. However this isn't happening and after running migrations I get:

Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')

First I ran docker-compose build, after which I ran docker-compose up -d and all of my 3 services are up and running.

If I extract the IP of MySQL service and use it in .env file like this:

DB_HOST=172.18.0.2

I can then run migrations successfully and in this case everything works fine.

However, I consider this as bad practice since IP address could be changed if MySQL service is restarted. Am I missing something here, why using service_name in my .env file for DB_HOST fails resolving db host name?

docker-compose.yml:

version: '3'

networks:
  laravel:

services:
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - mysql
    networks:
      - laravel
  mysql:
    image: mysql:5.7.22
    container_name: mysql
    restart: unless-stopped
    tty: true
    ports: 
      - "3306:3306"
    volumes: 
      - ./mysql:/var/lib/mysql
    environment:
      MYSQL_DATABASE: laraone
      MYSQL_USER: laraone_user
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    networks:
      - laravel
  php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php
    volumes: 
      - ./src:/var/www/html
    depends_on:
      - mysql
    ports:
      - "9000:9000"
    networks:
      - laravel

.env:

APP_NAME=Laraone
APP_ENV="local"
APP_KEY=base64:PMwGrcSu2ioPEj75dv5gcdWAogESOtt8UCr/gs0nOtw=
APP_DEBUG=true
APP_URL=http://localhost:8080

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laraone
DB_USERNAME=laraone_user
DB_PASSWORD=secret

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

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=

[email protected]
MAIL_FROM_NAME="${APP_NAME}"

MAIL_SENDMAIL="/usr/sbin/sendmail -bs"

Upvotes: 6

Views: 15161

Answers (5)

Don Ejeh
Don Ejeh

Reputation: 551

SOLUTION 1:

Always run docker ps to check if your ports are well mapped. below you can see what I mean.

enter image description here

to fix this you need to check if you have another instance of MySQL running with this port number and stop it or try a different port enter image description here

SOLUTION 2:

change your credentials to

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

here is mysql yml enter image description here

Upvotes: 1

bcag2
bcag2

Reputation: 2439

Similar issue, with console command I created, with docker containers to mysql and php
(just entrypoint for dev runnig php artisan serve --host 0.0.0.0)
and I tried to run on host
php artisan my:console:command
which return :
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution (SQL: select * from `mytable` limit 5)

By running :
docker exec -it myPhpContainerName php artisan my:console:command
it works fine

Upvotes: 0

Tuncay Elvanagac
Tuncay Elvanagac

Reputation: 1342

I think you try to run "php artisan migrate" command outside php docker container.

You should run php artisan migrate command inside php container.

Try following;

docker ps --> list running containers

docker exec -it <your_container_id> bash

and now you can run;

php artisan migrate

Edit: You can also write artisan command without entering container bash as following;

docker exec -i <your_container_id> php artisan migrate

Upvotes: 10

Daniel Dewhurst
Daniel Dewhurst

Reputation: 2603

For anyone still wanting a dockerized Laravel enviroment, check out Laravel Sail. It lets you route commands to the docker instance easily with an alias sail. Like sail artisan migrate.

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.

Upvotes: 1

themystery
themystery

Reputation: 63

I resolved the issue by installing laravel app inside a php container. Simple 1-line command which helped me solve this problem: docker exec -it php php artisan app:install

Upvotes: 0

Related Questions