Docker Environment

Docker is an essential tool for modern development, providing a consistent environment for building, shipping, and running applications. Prisma PHP leverages Docker 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, including Docker configuration files and instructions for setting up a local development environment with Apache and MySQL.

Setting Up the Docker Environment

Follow these steps to set up the Docker environment for Prisma PHP:

  1. Ensure you have Docker and Docker Compose installed on your machine. You can download them from the official Docker website: Docker Desktop.
  2. Prisma PHP provides the necessary Docker configuration files in the project directory, when you choose the Docker option while creating the project. If you don't have these files, you can create them manually based on the examples provided above or you can run the npx php update project command to update your project then choose the Docker option.
  3. When you run for the first time the npm run dev command, the Docker containers will be built and started automatically, Note: if the image is not found, it will be downloaded from the Docker Hub and this can take some time.
  4. Once the containers are up and running, you can access your application at http://localhost:3000 and the MySQL database at localhost:3306 for external connections and for internal connections use the service name db:3306.

Docker Configuration Files

To set up the Docker environment for Prisma PHP, you need the following configuration files:

.dockerignore

node_modules/
  vendor/

The .dockerignore file specifies which files and directories should be ignored by Docker. In this case, we ignore the node_modules and vendor directories.

apache.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory "/var/www/html">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
  </VirtualHost>

The apache.conf file configures the Apache web server. It sets the document root to /var/www/html and allows overriding settings in .htaccess 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.

Dockerfile

# Use an official PHP image with the version you need
  FROM php:8.1-apache
  
  # Install system dependencies for Composer and PHP extensions
  RUN apt-get update && apt-get install -y \
      git \
      unzip \
      libzip-dev \
      zip \
      && docker-php-ext-install pdo_mysql zip
  
  # Enable Apache mods
  RUN a2enmod rewrite headers
  
  # Install Composer globally
  COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
  
  # Set the working directory in the container
  WORKDIR /var/www/html
  
  # Copy the application's composer.json and lock file
  COPY composer.json composer.lock ./
  
  # Install PHP dependencies
  RUN composer install --no-scripts --no-autoloader
  
  # Copy the rest of the application
  COPY . .
  
  # Finish composer
  RUN composer dump-autoload --optimize
  
  # Apache config
  COPY ./apache.conf /etc/apache2/sites-available/000-default.conf
  
  # Expose port 80 to access the container
  EXPOSE 80
  
  # Command to run when starting the container
  CMD ["apache2-foreground"]

The Dockerfile defines the instructions to build the Docker image for the Prisma PHP application. It uses the official PHP 8.1 Apache image, installs necessary dependencies, sets up Composer, copies the application code, and configures Apache.