Setup DEV environment with Docker for PHP/PostgreSQL project
Steps listed below are specific to PHP / PostgreSQL project which also needs memcached. That is not ideal flow because each component should be run separately and run using Docker Compose however that is my first attempt to summarize steps I have performed to make it work.
In order to run PostgreSQL db server run the following command:
1 |
docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=password --name="projectname-db" postgres:9.3 |
This will start server at port 5432. Default credentials will be postgres/password. «projectname-db» is container’s name — you may change this to anything else more specific to your project or application.
You may also run apache server with php using the similar command
1 |
docker run -d -p 80:80 --name="projectname-web" php:5.4-apache |
But in most cases you will probably need to install extra extensions first. So let’s remove that test container by running the following command:
1 2 |
docker stop projectname-web docker rm projectname-web |
Then create folder for your own configuration and create Dockerfile there
1 2 3 |
mkdir projectname-web cd projectname-web vi Dockerfile |
Add the following content to Dockerfile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
FROM php:5.4-apache RUN apt-get update && apt-get install -y \ php-pear libicu-dev curl git libmemcached-dev zlib1g-dev libncurses5-dev libfreetype6-dev libjpeg62-turbo-dev libmcrypt-dev libpng12-dev libpq-dev --no-install-recommends RUN docker-php-ext-install pdo pgsql pdo_pgsql zip mbstring RUN curl -L http://pecl.php.net/get/memcached-2.2.0.tgz >> /usr/src/php/ext/memcached.tgz && \ tar -xf /usr/src/php/ext/memcached.tgz -C /usr/src/php/ext/ && \ rm /usr/src/php/ext/memcached.tgz && \ docker-php-ext-install memcached-2.2.0 RUN docker-php-ext-install iconv mcrypt \ && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \ && docker-php-ext-install gd \ && docker-php-ext-install intl RUN mkdir /var/www/html/project RUN mkdir /var/www/html/project/web RUN sed -i 's/DocumentRoot \/var\/www\/html/DocumentRoot \/var\/www\/html\/project\/web/g' /etc/apache2/apache2.conf RUN echo "" >> /etc/apache2/apache2.conf RUN echo "<Directory /var/www/html/project/>" >> /etc/apache2/apache2.conf RUN echo " Options FollowSymLinks" >> /etc/apache2/apache2.conf RUN echo " AllowOverride All" >> /etc/apache2/apache2.conf RUN echo " Order allow,deny" >> /etc/apache2/apache2.conf RUN echo " Allow from all" >> /etc/apache2/apache2.conf RUN echo "</Directory>" >> /etc/apache2/apache2.conf RUN echo "" >> /etc/apache2/apache2.conf RUN cd /usr/local \ && curl -O https://getcomposer.org/composer.phar \ && curl -O http://www.phing.info/get/phing-latest.phar # APC RUN pear config-set php_ini /usr/local/etc/php/php.ini RUN pecl config-set php_ini /usr/local/etc/php/php.ini RUN pecl install apc RUN echo "extension=apc.so" > /usr/local/etc/php/conf.d/apc.ini RUN a2enmod rewrite RUN usermod -u 1000 www-data |
Save Dockerfile and run the following command to build image based on the created configuration:
1 |
docker build -t projectname-mywebserver-image . |
After that you should be able to run Docker container using new image that has been created.
To make sure that your image has been created you may execute the following command:
1 |
docker images |
Before running container you will need to get path to your project so that you can mount that to container right away. To run the container proceed with the following:
1 |
docker run -d -p 80:80 -v /Users/myname/projects/testproject:/var/www/html/project --name="projectname-web" projectname-mywebserver-image |
That command will launch container and will mount your local ~/projects/testproject folder to the container (and will make ~/projects/testproject/web your DirectoryRoot for apache).
To make sure your container is up and running you may run the following command:
1 |
docker ps |
P.S. Dockerfile listed above is intended to be used when you need postgres,apc,curl and memcached modules for php and you have /web folder under your project directory which is used as DocumentRoot (that is default structure for Symfony based applications)