How to Dockerize a PHP Project

The article demonstrates how to dockerize and execute an existing PHP project.

You can arrange your PHP development work (LAMP stack) using virtual containers. In this manner, you can keep a project folder on the host computer and mimic the environment of a production server inside of a Docker container.

Make sure that Docker Desktop is on your computer. Follow the link to find out how to install Docker Desktop.

How to Dockerize an Exiting PHP Project

This section will walk you through the process of dockerizing an existing PHP project.

Create the subfolders app/public and config in the selected directory. Put in the app/public folder your PHP projects and in the config folder your configuration files for development.

Create the file docker-compose.yml In the root directory with content:

version: '3'
services:
    webserver:
        image: nginx:latest
        ports:
            - "8080:80"
        volumes:
            - ./nginx.conf:/etc/nginx/conf.d/nginx.conf
            - ./app:/app
    php:
        build:
            context: .
            dockerfile: PHP.dockerfile
        expose:
            - 9000
        ports:
            - "9000:9000"
        volumes:
            - ./app:/app
            - ./config/php/php.ini:/usr/local/etc/php/php.ini
    mysql:
        image: mariadb:latest
        environment:
            MYSQL_ROOT_PASSWORD: psw123'
            MYSQL_USER: 'user123'
            MYSQL_PASSWORD: 'psw123'
            MYSQL_DATABASE: 'my_php_db'
        volumes:
            - mysqldata:/var/lib/mysql
        ports:
            - 3306:3306
volumes:
    mysqldata: {}

The docker-compose.yml file specifies the following:

  • Port mapping (inner port 80 to outer port 8080).
  • Volumes (binds local folder app to the container’s folder app, as well as files nginx.conf and php.ini).
  • Builds a PHP image according to the requirements specified in the file PHP.dockerfile.
  • It creates the latest version of the MariamDb image with the connection parameters and port 3306.

Create a Docker file named PHP.dockerfile in the same directory with the following contents:

FROM php:7.4-fpm

RUN docker-php-ext-install pdo pdo_mysql

# Install the 2.4 version of xdebug that's compatible with php7
#RUN pecl install -o -f xdebug-3.0.4
 RUN pecl install xdebug && docker-php-ext-enable xdebug
#RUN docker-php-ext-install xdebug xdebug-3.0.4

# COPY config/php.ini /usr/local/etc/php/

RUN mkdir /app

The PHP.dockerfile file specifies how to install MySQL and the debugger.

Run the docker compose file (with a reserving development environment):

docker-compose up

If you want to run a temporary container, run the command:

docker run -it --rm -p 8080:80 --name php_temp testproject_php

Create the file index.php with the content:

<?php
$pdo = new PDO('mysql:dbname=tutorial;host=mysql', 'tutorial', 'secret', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);

$query = $pdo->query('SHOW VARIABLES like "version"');

$row = $query->fetch();

echo 'MySQL version:' . $row['Value'];

echo "<br> ------------------------------ <br>";
phpinfo();

echo "<br> ------------------------------ <br>";

xdebug_info();

and put it into the root folder app/public.

Type http://127.0.0.1:8080/ in the browser’s address field. You anticipate the following outcome:

PHP Info

Note: do not enter the address http://localhost:8080/, it does not work.

How To Execute a PHP Script

This section demonstrates how to run a PHP script on the fly and then immediately remove the container when the task is complete.

Create a folder and the file test.php in it with the content:

<?php 
	echo "One-time script with docker is working";

Run a CLI tool and execute the command:

docker container run --rm -v ${PWD}:/app/ php:7.4-cli php /app/test.php

You will receive the success response:

PS c:\docker-host\TestProject> docker container run --rm -v ${PWD}:/app/ php:7.4-cli php /app/test.php
Unable to find image 'php:7.4-cli' locally
7.4-cli: Pulling from library/php
a2abf6c4d29d: Already exists
c5608244554d: Already exists
2d07066487a0: Already exists
1b6dfaf1958c: Already exists
4810cc4e8244: Already exists
4ec287290423: Already exists
33988ba9489d: Pull complete
54c472921d60: Pull complete
b2098cb97a50: Pull complete
Digest: sha256:136a3482b37c29a48b605a59ea132d4b3f3a46e0250a08e74814591c170d898c
Status: Downloaded newer image for php:7.4-cli
One-time script with docker is working

The response shows the process of pulling down the PHP image. If you try the same script once more, you will get a short response: 

PS c:\docker-host\TestProject> docker container run --rm -v ${PWD}:/app/ php:7.4-cli php /app/test.php
One-time script with docker is working

because only the container is created and removed. without pulling down the image.


Was this helpful?

3 / 0

Leave a Reply 1

Your email address will not be published. Required fields are marked *


How to Install and Execute PHP in Localhost - Experienced Knowledge

How to Install and Execute PHP in Localhost - Experienced Knowledge

[…] you can install PHP in Docker. Follow the link, which discuses how to do […]