I have setup my Laravel application using Laravel Sail (Docker based). Everything's working fine except the MySQL. MySQL server is behaving differently for web (local.mysite.com:8080
) and for CLI (ex: php artisan migrate
).
Configruation (1)
If I use the following configuration in my .env
file,
...
APP_DOMAIN=local.mysite.com
...
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=root
FORWARD_DB_PORT=3307
the web works, not the CLI. When I run php artisan migrate
command I get SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
Configuration (2)
And if I use the following configuration,
...
APP_DOMAIN=local.mysite.com
...
DB_CONNECTION=mysql
DB_HOST=local.mysite.com
DB_PORT=3307
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=root
FORWARD_DB_PORT=3307
The CLI works fine, not the web. I get a Connection Refured
error by MySQL on web.
I've been banging my head with different tries, no luck...
What am I doing wrong?
Here's my docker-compose.yml
for reference (phpmyadmin
is working real good btw):
# For more information: https://laravel.com/docs/sail
version: '3'
services:
phpmyadmin:
depends_on:
- mysql
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8081:80'
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: password
networks:
- sail
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
extra_hosts:
- '${APP_DOMAIN}:127.0.0.1'
hostname: '${APP_DOMAIN}'
domainname: '${APP_DOMAIN}'
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
mysql:
image: 'mysql:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local