Dockerfile Cookbook Recipes Part 03: apache / php

Dockerfile Cookbook Recipes Part 03: apache / php

The goal is to always make these guides very simple - but to build from there, and to always try to include debugging information.

Prerequisites: If you need a quick startup script for installing docker / docker-ce:

0x0002 - New VM Docker / Docker-Compose Quick Start
New VM Docker / Docker-Compose Quick Start
  • apache images are for serving static content.
  • php images are for serving php code which can generate pages and also poll from databases for dataset presentations

Step 1: Apache Container Basics Calling a basic apache docker image and running it to see how it works.

docker run -it --name my-apache-app -p 8080:80 httpd:latest
  • -it  Interactive terminal mode
  • --name name of running container
  • -p 8080:80 port 8080 on host will map to port 80 on container
  • httpd:latest the image to pull and run
  • Naturally we have not mapped any data. Adding data and going into 'daemon' mode only requires changing your command line to the following
docker run -dit --name htttp -p 8080:80 -v /data:/usr/local/apache2/htdocs/ httpd:latest
  • The pages root dir in a apache2 are served from /usr/local/apache2/htdocs/
  • The pages root dir in otherapache server are served from /var/www/html

A docker-compose.yaml example:

version: '3'
services:
    apache2:
        image: httpd:latest
        volumes:
            - /data:/usr/local/apache2/htdocs/
        ports:
            - 8080:80

Which is then stood up with (diagnostic mode)

docker-compose up

Or in full continuance mode:

docker-compose up -d

Step 2: PHP Container Basics

  • The process of standing up a php html server is identical, and will enable you to run and process php code along with serving content.
  • The internal root is /var/www/html

Running in diagnostic mode:

docker run -it -p 8080:80 --name php_app -v /data:/var/www/html php:7.2-apache
  • Running in permanent daemon mode one adds -d to above

Checking the capabilities of the php running container:

  • Make a file in the /data named index.php:
<?php 
 phpinfo(); 

 phpinfo(INFO_MODULES);
?>

And then call it with:

http://<local ip>/index.php

It will show up as (if it is working)

Docker-compose.yaml example:

version: '3.1'

services:
  php:
    image: php:7.2-apache
    ports:
      - 8080:80
    volumes:
      - /data:/var/www/html/
Linux Rocks Every Day