Create PHP-MySQL-Apache Development Environment using Docker

Meera Mary George
3 min readFeb 2, 2017

--

Here’s the set of instructions on how to create Development Environment for Docker using PHP, MySQL and Apache webserver, which worked for me, and hence sharing! Hope this helps.

Docker installation

1. Install Docker.

If you are running Windows OS, you would need Docker ToolBox installed on your system which uses docker-machine to create and attach to a VM. This machine is a Linux VM that hosts Docker for you and your Windows system.

To setup Docker on your system, click Docker Installation and follow the instructions.

2. In the Docker Quick-Start Terminal, docker-machine ip command will give the ip address of the docker host.

Setting up the project

The VM Shared folder is mounted to C drive by default. So Website workspace can be created in C drive. If you wish to create workspace in other volumes, you need to mount the specific volume into the Docker VM seperately.

3. Create Dockerfile.

FROM ubuntu:latestMAINTAINER Name<email>ENV DEBIAN_FRONTEND noninteractive# Install basicsRUN apt-get updateRUN apt-get install -y software-properties-common && \add-apt-repository ppa:ondrej/php && apt-get updateRUN apt-get install -y — force-yes curl# Install PHP 5.6RUN apt-get install -y — allow-unauthenticated php5.6 php5.6-mysql php5.6-mcrypt php5.6-cli php5.6-gd php5.6-curl# Enable apache mods.RUN a2enmod php5.6RUN 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/php/5.6/apache2/php.iniRUN sed -i “s/error_reporting = .*$/error_reporting = E_ERROR | E_WARNING | E_PARSE/” /etc/php/5.6/apache2/php.ini# Manually set up the apache environment variablesENV APACHE_LOG_DIR /var/log/apache2ENV APACHE_LOCK_DIR /var/lock/apache2ENV APACHE_PID_FILE /var/run/apache2.pid# Expose apache.EXPOSE 80EXPOSE 8080EXPOSE 443EXPOSE 3306# Update the default apache site with the config we created.ADD apache-config.conf /etc/apache2/sites-enabled/000-default.conf# By default start up apache in the foreground, override with /bin/bash for interative.CMD /usr/sbin/apache2ctl -D FOREGROUND

Following are the components installed using the Dockerfile given above.

· Ubuntu 16.04

· PHP 5.6

· Apache2

If you wish to change any versions or components the Dockerfile can be edited.

4. Copy the following apache configuration file into the document root where Dockerfile is placed.

<VirtualHost *:80>ServerAdmin me@mydomain.comDocumentRoot /var/www/Website<Directory /var/www/Website/>Options Indexes FollowSymLinks MultiViewsAllowOverride AllOrder deny,allowAllow from all</Directory>ErrorLog ${APACHE_LOG_DIR}/error.logCustomLog ${APACHE_LOG_DIR}/access.log combined</VirtualHost>

Project Hierarchy example is as below.

C: Drive----> Users -----> mgeorge  ------> Website   -------> Dockerfile, apache-config.conf

Building of Image

5. Now build the image using the Dockerfile.

Open the Docker ToolBox terminal, enter the command

docker build -t <image_name> .

To view the image created, run docker images command.

6. To create the container, and to forward the required ports for development environment and also to mount the local workspace to docker container, execute the following command.

docker run -p 8080:80 -d -v /c/Users/mgeorge/Website:/var/www/Website — name <container_name> <image_name>

-p 8080:80 publishes port 80 in the container to 8080 on the host machine.
-d detaches from the process.

Run the container bash, using the following command.

docker exec -it <container_name> bash

The workspace is now mounted to the container.

7. Test the setup by running service apache2 status command, and test the url in browser.

http://<docker-host-ip-address>:8080

This setup assumes that the Docker container is connecting to the localhost MySQL server. So in the source code edit the configuration to include localhost ip address, MySQL username and password. Also execute the grant privileges MySQL command.

grant all privileges on *. * to ‘username’@’%’ identified by ‘password’;flush privileges;

The development environment is now set up:)

--

--