php docker link apache docker
You can separate Apache and PHP with PHP-FPM. It is however that the DocumentRoot must be mounted on both containers.
Apache must be able to access the files locally (inside its container) as well as the PHP-FPM server.
I am currently working on the same, have a look at my docker-compose.yml here
https://github.com/cytopia/devilbox/blob/master/docker-compose.yml
Both volumes (in PHP and apache) are mounted to /shared/httpd
I would say its not possible to run seperate containers for php as apache module. I guess this is what Wolfgang meant.
If you want to seperate apache and php in two different containers you need to run php as fpm.
Have a look here for inspiration: How to correctly link php-fpm and Nginx Docker containers together?
If you need to run apache and php as apache_mod use a combined container like this: https://github.com/docker-library/php/blob/fec7f537f049aafd2102202519c3ca9cb9576707/5.5/apache/Dockerfilefrom: https://hub.docker.com/_/php/
If you don't specifically need to separate Apache from PHP, then you might be good to go with an official php:5.6-apache image which comes with Apache out of the box.
For example, your docker-compose.yml
might look something like this:
version: '3'services: web: image: php:5.6-apache ports: - "8080:80" # Map container port 80 to host machine port 8080 volumes: - ".:/var/www/html" # Mount current folder as volume to container at /var/www/html
Or, for a more real-life example, if you also need at least one of the following:
- A custom web root (for Laravel, Symfony, etc)
- Other Apache modules installed
- Other PHP extensions installed
You might do something more like this:
version: '3'services: web: build: context: . dockerfile: Dockerfile ports: - "8080:80" # Map container port 80 to host machine port 8080 environment: APACHE_DOCUMENT_ROOT: "/var/www/yourapp.com/public" volumes: - ".:/var/www/yourapp.com" # Mount current folder as volume to container at /var/www/yourapp.com
And then your Dockerfile
(which we reference from the docker-compose.yml
above):
FROM php:5.6-apache# Declare an environment variable with a default value for changing Apache's document root# We will override this in docker-compose.ymlENV APACHE_DOCUMENT_ROOT /var/www/html# Configure web rootRUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.confRUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf# Install additional Apache modules# This example: mod_rewrite & mod_headersRUN a2enmod rewrite headers# Install additional PHP extensions# This example: memcached & mysqli# For other extensions see official docs:# https://hub.docker.com/_/php (section: How to install more PHP extensions)RUN apt-get update && apt-get install -y libmemcached-dev zlib1g-dev \ && pecl install memcached-2.2.0 \ && docker-php-ext-enable memcached \ && docker-php-ext-install -j$(nproc) mysqli