I use frankenphp default docker-compose .yaml to develop on local with a bit MySQL database as an addition. but even though I have exposed the DB port, my laravel app still could not connect to the DB.
compose.yaml
# compose.yaml
services:
db:
image: mysql:8.1.0
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
php:
# uncomment the following line if you want to use a custom Dockerfile
build: .
# uncomment the following line if you want to run this in a production environment
# restart: always
ports:
- "80:80" # HTTP
- "443:443" # HTTPS
- "443:443/udp" # HTTP/3
volumes:
- ./:/app
- caddy_data:/data
- caddy_config:/config
depends_on:
- db
# comment the following line in production, it allows to have nice human-readable logs in dev
tty: true
# Volumes needed for Caddy certificates and configuration
volumes:
caddy_data:
caddy_config:
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=root
is there’s something I miss?
2
Answers
It’s probably because you don’t have the pdo_mysql extension installed in your container.
You will have to do something like this in your Dockerfile:
You have two services in your
compose.yaml
file:db
php
In this case, you should use the service name (
db
) instead of IP in theDB_HOST
in.env
file.So, final look will be
DB_HOST=db
.I’m not sure, but you also might need to add username and password under the
environment
in yourcompose.yaml
file, like: