I build 2 dockers, one docker with apache, one docker with php5, and I use docker-compose to start.
apache2 Dockerfile in directoy apache2:
FROM debian:latest
RUN apt-get update && apt-get install -y apache2
ADD test.php /var/www/html
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
and test.php:
<?php
phpinfo();
?>
php5 Dorckerfile in directory php:
FROM debian:latest
RUN apt-get update && apt-get install -y php5
docker-compose.yml:
apache:
build: ./apache2
container_name: apache
ports:
- "80:80"
links:
- "php5"
php5:
build: ./php
container_name: php
then I run:
docker-compose up
apache2 server start successfully. Then I access this server by http://server_ip, then I get index of debian.But when I access http://server_ip/test.php, just occur this:
<?php
phpinfo();
?>
php just doesn't work.And I don't why.
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/Dockerfile
from: 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.yml
ENV APACHE_DOCUMENT_ROOT /var/www/html
# Configure web root
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN 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_headers
RUN 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