Reputation: 375
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?
Upvotes: 9
Views: 25029
Reputation: 136
I was trying to connect Pest Plugin
for PHPSTORM
with Laravel
on Sail
and I had the same problem. The problem was that on the configuration of php interpreter there was default DOCKER
network name bridge
and my sail docker containers where using different name .
To show your docker networks you can do it with
docker network ls
After that change the network mode
to the one docker is using for your environment in
Settings => PHP => Edit Docker Container Settings
.
credits to link
Upvotes: 0
Reputation: 334
I solved it by DB_HOST=0.0.0.0
in ENV file, because Docker works on that port. The other solution is in terminal open docker container docker exec -it mydockername /bin/bash
and in this bash execute php artisan migrate
.
Upvotes: 4
Reputation: 39
I had the same issue and I found in the doc that when using Sail you should run all commands using sail instead of php. for example "php artisan migrate" => "sail artisan migrate".
for more insight about this: https://laravel.com/docs/9.x/sail#executing-sail-commands
Upvotes: 0
Reputation: 539
It happens when the laravel application running in docker is not able to connect to database. 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: 2
Reputation: 2603
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.
Upvotes: 0
Reputation: 331
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
!! 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
Upvotes: 0
Reputation: 1112
If you are connecting to mysql as an external docker container, make sure that you implemented this solution https://stackoverflow.com/a/38089080/1770571
Upvotes: 0
Reputation: 902
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/
Upvotes: 20