Docker MySQL Environment

Docker is an essential tool for modern development, providing a consistent environment for building, shipping, and running applications. Prisma PHP leverages Docker with MySQL to create a seamless development workflow, ensuring that applications run reliably across different environments. This documentation outlines the steps to set up a Docker environment for Prisma PHP with MySQL, including Docker configuration files and instructions for setting up a local development environment with Apache and MySQL.

Docker Configuration Files

docker-compose.yml

version: "3.8"
  services:
    web:
      build:
        context: .
        dockerfile: Dockerfile
      ports:
        - "80:80"
      volumes:
        - .:/var/www/html
      depends_on:
        - db
    db:
      image: mysql:5.7
      environment:
        MYSQL_ROOT_PASSWORD: prisma_php
        MYSQL_DATABASE: prisma_php
      ports:
        - "3306:3306"

The docker-compose.yml file defines the services for the Docker environment. It includes a web service for the Apache server and a db service for the MySQL database, configured with environment variables for the root password and database name.

Updated .env File

# For Prisma ORM
  DATABASE_URL="mysql://root:prisma_php@localhost:3306/prisma_php"
  # For Prisma PHP ORM
  PRISMA_PHP_ORM_DATABASE_URL="mysql://root:prisma_php@db:3306/prisma_php"

Update the .env file with the following database URLs for Prisma ORM and Prisma PHP ORM. The URLs specify the MySQL database connection details, including the root user, password, host, and port.

Updated Prisma Schema

Change the provider in your Prisma schema from postgresql to mysql:

datasource db {
    provider = "mysql"
    url      = env("DATABASE_URL")
  }

Updated Prisma.php Configuration

Generate the Prisma PHP ORM configuration file using the following command: npx php generate class and update the Prisma.php file in the Lib/Prisma/Classes directory with the following configuration:

$databaseUrl = $_ENV['PRISMA_PHP_ORM_DATABASE_URL'];
  if (!$databaseUrl) {
      throw new \Exception('PRISMA_PHP_ORM_DATABASE_URL not set in .env file.');
  }

Update the Prisma.php file to use the PRISMA_PHP_ORM_DATABASE_URL environment variable for the database URL. This ensures that Prisma PHP ORM connects to the correct MySQL database when running in a Docker environment.