I am using docker to run nginx, mariadb and php (8.1) on a Mac with M1. I installed Laravel on the host, using composer and set up my database credentials in the .env file.
When I ran php artisan migrate
on the host I get:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: nodename nor servname provided, or not known (SQL: select * from information_schema.tables where table_schema = api and table_name = migrations and table_type = 'BASE TABLE')
I already searched and found, that this could be related to the DB_HOST
setting.
Mine is: mysql
, what is the container_name
. I tried 127.0.0.1
or 0.0.0.0
or localhost
as well with the same result.
Within mysql I get:
select * from information_schema.tables where table_schema = "api" and table_name = "migrations" and table_type = 'BASE TABLE';
Empty set (0,03 sec)
This is my manifest:
version: '3'
services:
nginx:
container_name: nginx
image: nginx:alpine
volumes:
- "./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf"
- "./etc/ssl:/etc/ssl"
- "./web:/var/www/html"
ports:
- "80:80"
- "433:443"
environment:
- NGINX_HOST=${NGINX_HOST}
command: /bin/sh -c "nginx -g 'daemon off;'"
restart: always
depends_on:
- php
- mysql
php:
container_name: php
image: nanoninja/php-fpm:${PHP_VERSION}
platform: linux/x86_64
restart: always
volumes:
- "./etc/php/php.ini:/usr/local/etc/php/conf.d/php.ini"
- "./web:/var/www/html"
phpmyadmin:
container_name: phpmyadmin
image: phpmyadmin/phpmyadmin
platform: linux/x86_64
container_name: phpmyadmin
ports:
- "8080:80"
environment:
- PMA_ARBITRARY=1
- PMA_HOST=${MYSQL_HOST}
restart: always
depends_on:
- mysql
mysql:
image: mariadb:${MYSQL_VERSION}
container_name: ${MYSQL_HOST}
restart: always
env_file:
- ".env"
environment:
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
ports:
- "3306:3306"
volumes:
- "./data/db/mysql:/var/lib/mysql"
CodePudding user response:
The hostname 'mysql' only works if you run it in a docker-compose or when the container is linked other ways or if you specify that in your /etc/hosts file. I guess the issue is that you need to run that artisan command inside that php container (instead of running it from the host itself) with docker exec something like this:
docker exec -it <php-containers-name> php artisan migrate
The other way is to expose the db containers port to the host so you can access it with 0.0.0.0:3306.
Using the docker-compose instead of pure docker to relay to the right containers then. What is the output of this command?
docker-compose exec -w="/var/www/html" php php artisan migrate
(It runs a 'php artisan migrate' inside the php container using /var/www/html as a working directory)