I know there're similar questions but the answers do not help.
I have this server where I have previously installed the following two repositories:
Now I have added a Laravel Application. The Laravel application is now working, the domain is correctly resolved by the Nginx proxy and Letsencrypt is working properly.
However, the problem is now with the database connection. When I run php artisan migrate --seed the following errors show up:
In Connection.php line 671:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name does not resolve (SQL: select * from information_schema.tables where table_schema = mydb and table_name = migrations and table_type = 'BASE TABLE')
In Connector.php line 70: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name does not resolve
In Connector.php line 70: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name does not resolve
This is my docker-compose.yml file:
version: '3'
networks:
default:
external:
name: ${NETWORK}
services:
nginx:
image: nginx:stable-alpine
container_name: app-webserver
expose:
- "80"
- "443"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
environment:
- VIRTUAL_HOST=${VIRTUAL_HOST}
- LETSENCRYPT_HOST=${LETSENCRYPT_HOST}
- LETSENCRYPT_EMAIL=${LETSENCRYPT_EMAIL}
depends_on:
- php
- mysql
mysql:
image: mysql:5.7.29
container_name: app-mysql
restart: unless-stopped
tty: true
expose:
- "3306"
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
php:
build:
context: .
dockerfile: php.dockerfile
container_name: app-php
volumes:
- ./src:/var/www/html
environment:
- VIRTUAL_HOST=${VIRTUAL_HOST}
- LETSENCRYPT_HOST=${LETSENCRYPT_HOST}
- LETSENCRYPT_EMAIL=${LETSENCRYPT_EMAIL}
expose:
- "9000"
composer:
image: composer:latest
container_name: composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- php
npm:
image: node:13.7
container_name: npm
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
entrypoint: ['npm']
artisan:
build:
context: .
dockerfile: php.dockerfile
container_name: artisan
volumes:
- ./src:/var/www/html
depends_on:
- mysql
working_dir: /var/www/html
entrypoint: ['php', '/var/www/html/artisan']
Inside the Laravel .env file I have tried all possible values for DB_HOST:
DB_HOST=localhost
DB_HOST=127.0.0.1
DB_HOST=mysql
DB_HOST=app-mysql
Currently, I have it set up for the MySQL container name:
DB_HOST=app-mysql
Other values are:
DB_CONNECTION=mysql
DB_PORT=3306
And I still get that error.
I have tried the ping command from the app-php container to the app-mysql container as follows:
$ docker exec -ti app-php ping app-mysql
I do get response:
64 bytes from 172.18.x.x: seq=0 ttl=64 time=0.132 ms
64 bytes from 172.18.x.x: seq=1 ttl=64 time=0.240 ms
64 bytes from 172.18.x.x: seq=2 ttl=64 time=0.207 ms
64 bytes from 172.18.x.x: seq=3 ttl=64 time=0.208 ms
However, if I run:
$ docker-compose run --rm artisan ping app-mysql
I get the following error:
Command "ping" is not defined.
Now if I run docker-compose up in order to see what's going on (without -d), I see the following, as for MySQL:
app-mysql | 2020-07-29T15:43:24.159355Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
app-mysql | 2020-07-29T15:43:24.159523Z 0 [Note] IPv6 is available.
app-mysql | 2020-07-29T15:43:24.159582Z 0 [Note] - '::' resolves to '::';
app-mysql | 2020-07-29T15:43:24.159688Z 0 [Note] Server socket created on IP: '::'.
Does anybody out there know how to fix this? Why can't Artisan or the PHP container connect to the database container?
workaround
I solved this using Laradock instead