I have docker installed on ubuntu machine and I'm trying to run a laravel app.
MySQL service has service_name: mysql
in docker-compose.yml file and .env file has DB_HOST=mysql
.
As I remember .env file should figure out that DB_HOST=mysql
points to the mysql docker service IP. However this isn't happening and after running migrations I get:
Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
First I ran docker-compose build
, after which I ran docker-compose up -d
and all of my 3 services are up and running.
If I extract the IP of MySQL service and use it in .env
file like this:
DB_HOST=172.18.0.2
I can then run migrations successfully and in this case everything works fine.
However, I consider this as bad practice since IP address could be changed if MySQL service is restarted. Am I missing something here, why using service_name
in my .env
file for DB_HOST
fails resolving db host name?
docker-compose.yml:
version: '3'
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:5.7.22
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: laraone
MYSQL_USER: laraone_user
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src:/var/www/html
depends_on:
- mysql
ports:
- "9000:9000"
networks:
- laravel
.env:
APP_NAME=Laraone
APP_ENV="local"
APP_KEY=base64:PMwGrcSu2ioPEj75dv5gcdWAogESOtt8UCr/gs0nOtw=
APP_DEBUG=true
APP_URL=http://localhost:8080
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laraone
DB_USERNAME=laraone_user
DB_PASSWORD=secret
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=
MAIL_FROM_ADDRESS=noreply@example.com
MAIL_FROM_NAME="${APP_NAME}"
MAIL_SENDMAIL="/usr/sbin/sendmail -bs"