Public Repository

Last pushed: 2 years ago
Short Description
Apache and PHP on Docker
Full Description

Setting up the project
Set up a project with the following structure:

./Dockerfile
./apache-config.conf
./www/index.php

Dockerfile:
FROM ubuntu:latest

MAINTAINER Dan Pupius dan@pupi.us

RUN apt-get update
RUN apt-get -y upgrade

Install apache, PHP, and supplimentary programs. curl and lynx-cur are for debugging the container.

RUN DEBIAN_FRONTEND=noninteractive apt-get -y install apache2 libapache2-mod-php5 php5-mysql php5-gd php-pear php-apc php5-curl curl lynx-cur

Enable apache mods.

RUN a2enmod php5
RUN a2enmod rewrite

Update the PHP.ini file, enable <? ?> tags and quieten logging.

RUN sed -i "s/short_open_tag = Off/short_open_tag = On/" /etc/php5/apache2/php.ini
RUN sed -i "s/error_reporting = .*$/error_reporting = E_ERROR | E_WARNING | E_PARSE/" /etc/php5/apache2/php.ini

Manually set up the apache environment variables

ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid

EXPOSE 80

Copy site into place.

ADD www /var/www/site

Update the default apache site with the config we created.

ADD apache-config.conf /etc/apache2/sites-enabled/000-default.conf

By default, simply start apache.

CMD /usr/sbin/apache2ctl -D FOREGROUND

apache-config.conf:

<VirtualHost *:80>
ServerAdmin me@mydomain.com
DocumentRoot /var/www/site

<Directory /var/www/site/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order deny,allow
Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

www/index.php:

<? echo "<p>Hello?</p>"; ?>

Building the container
Once you have setup the project run:

docker build -t mysite .

Getting it running
To run apache in a background process, simply start the container using the following command:

docker run -p 8080:80 -d mysite
-p 8080:80 publishes port 80 in the container to 8080 on the host machine.
-d detaches from the process, use docker ps and docker stop to … stop.

Note: on OSX use boot2docker ip to get the right IP to use.

Sometimes you’ll want to debug issues with the container; maybe there are PHP configuration issues or you want to view error logs. To do that you can start the container in interactive mode and then start apache manually:

docker run -i -t -p 8080:80 mysite /bin/bash
apachectl start
Making changes
If you’re actively developing you want to be able to change files in your usual editor and have them reflected within the container without having to rebuild it. The -v flag allows us to mount a directory from the host into the container:

docker run -p 8080:80 -d mysite -v /Users/dan/site:/var/www/site
Note: This doesn’t work with the default boot2docker install on OSX, but here are some instructions for how to make it work.

https://medium.com/dev-tricks/apache-and-php-on-docker-44faef716150

Docker Pull Command
Owner
xokvictor

Comments (0)