Communication of PHP and Apache HTTPD web server with the help of Docker Compose

Note

Clone the git repository if you haven’t done it yet.

Go to Project 5 from the git repository root:

cd projects/p05

Project structure:

.
├── Dockerfile
├── docker-compose.yml
└── www
    └── index.php

The content of index.php

<?php

file_put_contents(__DIR__ . '/access.txt', date('Y.m.d. H:i:s') . "\n", FILE_APPEND);

echo 'P05: ' . getenv('HOSTNAME') . '<br/>';
echo nl2br(file_get_contents(__DIR__ . '/access.txt'));

the compose file

volumes:
  www:

services:
  php:
    image: localhost/p05_php
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - www:/var/www/html
  httpd:
    image: itsziget/httpd24:2.0
    volumes:
      - www:/var/www/html
    environment:
      SRV_PHP: "true"
      SRV_DOCROOT: /var/www/html
    ports:
      - "8080:80"

and the Dockerfile

FROM itsziget/php:7.4-fpm

LABEL hu.itsziget.ld.project=p05

COPY www /var/www/html

# The scripts interpreted by PHP-FPM executes on behalf of "www-data" user.
RUN chown www-data:www-data -R /var/www/html

Build PHP image and start the containers:

docker compose up -d

Start multiple container for PHP:

docker compose up -d --scale php=2

List the containers to see PHP has multiple instance:

docker compose ps

Open the page in your browser and you can see the hostname in the first line is not constant. It changes but not every time, although the data is permanent.

Delete everything created by Docker Compose for this project:

docker compose down --volumes