Dockerizing a PHP project

Setting up a Dev environment with docker for kumbiaPHP.

This guide outlines the steps to containerize a KumbiaPHP application using Docker, specifically with PHP 8.2, and how to enable the mysqli extension required for MySQL database operations.

Requirements

  • Docker installed on your machine
  • KumbiaPHP project ready for dockerization

Step 1: Create a Dockerfile

Create a Dockerfile in the root of your KumbiaPHP project. This file defines the Docker image configuration.

# Use an official PHP runtime as a parent image
FROM php:7.4-apache

# Set working directory inside the container
WORKDIR /var/www/html
	
# Install extensions
# You might need additional PHP extensions based on your project
RUN docker-php-ext-install pdo_mysql

# Enable Apache mod_rewrite for KumbiaPHP friendly URLs
RUN a2enmod rewrite

# Copy your KumbiaPHP project into the container
COPY . .

# Expose port 80 to access the Apache server
EXPOSE 80

# Start Apache server in the foreground
CMD ["apache2-foreground"]

Step 2: Add a .dockerignore File

To improve build performance and reduce image size, create a .dockerignore file to exclude files not needed in the container.

.git
vendor

Step 3: Build the Docker Image

With the Dockerfile configured, build the Docker image using the following command:

docker build -t kumbiaphp-app .

Step 4: Run Your Docker Container

Start a container from your image, mapping port 8080 on your host to port 80 on the container.

docker run -d -p 8080:80 kumbiaphp-app

Step 5: Enable Live Code Updates (Optional)

For development, you might want live updates in your container as you modify the project files. Use Docker volumes to achieve this:


docker run -d -p 8080:80 -v $(pwd):/var/www/html kumbiaphp-app

Troubleshooting: Fixing Common Errors

Permission Denied Error

If encountering a permission error when accessing the site, ensure Apache has access to the project files:

# Add these lines to your Dockerfile before CMD
RUN chown -R www-data:www-data /var/www/html
RUN chmod -R 755 /var/www/html

PHP mysqli Extension Error

If your application requires the mysqli extension, ensure it's installed by including the RUN docker-php-ext-install mysqli command in your Dockerfile.

Creating a Custom Apache Configuration File

  1. Create the Apache Configuration File: In your project directory, create a file named myapp.conf. This file will contain your custom Apache virtual host configuration.
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

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

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

This configuration enables .htaccess override capabilities and ensures that all requests are allowed, which is necessary for KumbiaPHP's routing to function correctly; make sure you use this config in you Dockerfile.


Click here to share this article with your friends on X if you liked it.