ryoakg
3/7/2017 - 4:31 PM

run-docker-apache-mod_php-multi-processes-app.sh

#!/bin/sh

# This is useful for running a app parallelly that consists of a single file.

# more info: https://github.com/docker-library/docs/tree/master/php

# config
CONTAINER=php-test
PORT=8888
PHP_SCRIPT=test.php
IMAGE=php:apache
DOCKER=docker

# ensure the image exists
if [ -z "$(${DOCKER} images -q ${IMAGE} 2> /dev/null)" ] ;then
  ${DOCKER} pull ${IMAGE}
fi

# # create and run the container
if [ -z "$(${DOCKER} ps -a -q -f name=${CONTAINER} 2> /dev/null)" ] ;then
  mkdir -p docroot
  QUOTED_PHP_SCRIPT=$(printf ${PHP_SCRIPT} | sed 's/\./\\./g')
  cat <<EOF > docroot/.htaccess
RewriteEngine on
RewriteCond \$1 !^(${QUOTED_PHP_SCRIPT}|images|robots\.txt)
RewriteRule ^(.*)$ /${PHP_SCRIPT}/$1 [L]
EOF

  ${DOCKER} run \
            --name ${CONTAINER} \
            -v $(pwd)/docroot:/var/www/html:ro \
            -p ${PORT}:80 \
            -d \
            ${IMAGE}

  ${DOCKER} exec ${CONTAINER} a2enmod rewrite

  ${DOCKER} exec ${CONTAINER} apt-get update
  ${DOCKER} exec ${CONTAINER} \
            apt-get install -y \
            libfreetype6-dev \
            libjpeg62-turbo-dev \
            libpng12-dev
  ${DOCKER} exec ${CONTAINER} \
            docker-php-ext-configure gd \
            --with-freetype-dir=/usr/include/ \
            --with-jpeg-dir=/usr/include/
  ${DOCKER} exec ${CONTAINER} docker-php-ext-install gd

  # ${DOCKER} exec ${CONTAINER} kill -HUP 1
  ${DOCKER} exec ${CONTAINER} /etc/init.d/apache2 restart
fi

# ensure the container is running
if [ "$(${DOCKER} inspect -f '{{.State.Running}}' ${CONTAINER} 2> /dev/null)" != "true" ] ;then
  ${DOCKER} start ${CONTAINER}
fi