skip to Main Content

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


  1. 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:

    FROM dunglas/frankenphp
    
    # add additional extensions here:
    RUN install-php-extensions 
        pdo_mysql 
        gd 
        intl 
        zip 
        opcache
    
    Login or Signup to reply.
  2. 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 the DB_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 your compose.yaml file, like:

    environment:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_USER: '${DB_USERNAME}'
          MYSQL_PASSWORD: '${DB_PASSWORD}'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search