Laravel Docker - SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
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/
Related videos on Youtube
MelbDev
Updated on June 08, 2022Comments
-
MelbDev almost 2 years
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?
-
David Maze over 4 yearsYou need neither
container_name:
nor a network other than the Compose-provideddefault:
for inter-container communications to work. In thedocker-compose.yml
file from the original question,database
should be usable as a DNS name without any modifications. -
Fida over 2 yearsCorrect, they are all on default network if network not specified
-
Harsha D Wijendra over 2 yearsperfect. thanks @rami worked for me