PRACTICAL USE OF DOCKER Development environment with docker on a Php project example

Vasilache Veaceslav | [email protected]

Sr. Php Developer, Amdaris

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 PRACTICAL USE OF DOCKER |

Agenda

Docker. Short introduction

Overview of usage in different areas

Why do we need it on development machine

Sample implementation / Demo app

Questions / Discussion

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 PRACTICAL USE OF DOCKER |

Docker. Short introduction

Image

Containers

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 PRACTICAL USE OF DOCKER |

Docker. Short introduction (images)

Image

Containers

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 PRACTICAL USE OF DOCKER |

Docker. Short introduction

Client

Hosts

Registries

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 PRACTICAL USE OF DOCKER |

Usages

Development In Development environment

Live environments

Scale up Deploy & Scale up Live environment, instances in and deployment swarms

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 PRACTICAL USE OF DOCKER |

Usages. Environments

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 PRACTICAL USE OF DOCKER |

… as development environment

● Simple configuration & Usage ● Configure once & replicate as needed ● Keep developers machine clean ● Easy upgrade or apply changes

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 PRACTICAL USE OF DOCKER |

… as development environment

Simple configuration & Usage

● Dockerfile ● Docker-compose file

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 Sample of Dockerfile

FROM centos:centos7

# Prepare the remi repos for php7.2 RUN install -y epel-release deltarpm RUN yum update -y epel-release

# Install remi repos & required packages RUN yum install -y httpd php-gd php-mbstring php-mysql php-xml php-xdebug php-gmp php-opcache php-apcu ghostscript ImageMagick cronie mod_ssl openssl sendmail sendmail-cf mailx \ git mc wget nano python- curl RUN easy_install supervisor

# Crontab related files ADD crontab /etc/cron.d/website_crontab_dev RUN chmod 0644 /etc/cron.d/website_crontab_dev

# PHP pdf lib related files ADD pdflib/* /pdflib

# HTTPD related files ADD httpd.conf/*.conf /etc/httpd/conf.d/ ADD xdebug/xdebug.ini /etc/php.d/xdebug.ini

COPY ssl/ca.crt /etc/pki/tls/certs/ca.crt

# Startup Scripts COPY supervisord.conf /etc/supervisor/supervisord.conf COPY scripts/*.sh /scripts/ RUN chmod 775 /scripts/*.sh

EXPOSE 80 443 9001 25 587 RUN /run-create-user.sh

CMD ["/startup.sh"]

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 Sample of Dockerfile [xdebug.ini]

; Enable xdebug extension module zend_extension=xdebug.so

xdebug.profiler_enable = off # to use xdebug profiler add XDEBUG_PROFILE=1 as GET or POST or in COOKIE xdebug.profiler_enable_trigger = on xdebug.profiler_output_name = cachegrind.out.%t.%p xdebug.profiler_output_dir ="/var/www/html/tin/var/logs/"

# PHP Storm Tracefile analyzer works with format=1 xdebug.trace_format=1

xdebug.show_local_vars=0 xdebug.force_display_errors=off xdebug.force_error_reporting=E_ALL xdebug.idekey="PHPSTORM-MYWEBSITE--DEV"

xdebug.remote_enable = On xdebug.remote_host=172.17.0.1 xdebug.remote_port=9001 xdebug.remote_autostart=on xdebug.remote_connect_back=off xdebug.remote_handler=dbgp

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 Sample of Dockerfile [prepare.sh]

#!/bin/bash

rm -rf /run/httpd/* /tmp/httpd*

mkdir -p /var/log/supervisor

chmod +x /usr/bin/

mv /etc/php.d/15-xdebug.ini /etc/php.d/15-xdebug.ini.bak

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" php composer-setup.php --install-dir=/usr/sbin --filename=composer php -r "unlink('composer-setup.php');"

Sample of Dockerfile [run-create-user.sh]

#!/bin/bash

useradd websiteuser echo "passtobechanged" | passwd tinstudio --stdin gpasswd -a websiteuser wheel gpasswd -a websiteuser apache

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 Sample of docker-compose.yml

; Enable xdebug extension module

version: '2'

volumes: logs: driver: local docker_scripts: driver: local

services: address_book_demo: image: php:7.2-alpine working_dir: /var/www/address_book_demo command: php -S 0.0.0.0:8080 -t web environment: docker: "true" PHP_XDEBUG_REMOTE_HOST: "172.18.0.1" PHP_XDEBUG_REMOTE_PORT: "9001" PHP_XDEBUG_IDEKEY: PHPSTORM PHP_IDE_CONFIG: "serverName=address_book_demo" ports: - 8080:8080 - 9000 volumes: - .:/var/www - logs:/var/logs - docker_scripts:/var/docker_scripts

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 Sample of docker-compose.yml

#!/bin/sh [restore-container-packages.sh]

apk update apk add mc autoconf gcc g++ make pecl channel-update pecl.php.net pecl install xdebug pecl install apcu-stable # XDEBUG echo "zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so" > /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \ echo "xdebug.default_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \ echo "xdebug.idekey=PHPSTORM" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \ echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \ echo "xdebug.remote_autostart=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \ echo "xdebug.remote_host=172.18.0.1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \ echo "xdebug.remote_port=9000" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \ echo "xdebug.profiler_enable=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \ echo "xdebug.profiler_enable_trigger=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \ echo "xdebug.profiler_output_dir=\"/tmp\"" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini # OPCACHE echo "zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/opcache.so" > /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini # APCU echo "extension=apcu.so" > /usr/local/etc/php/conf.d/docker-php-ext-apcu.ini && \ echo "apc.enable_cli=1" >> /usr/local/etc/php/conf.d/docker-php-ext-apcu.ini

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php composer-setup.php --install-dir=/usr/local/bin --filename=composer php -r "unlink('composer-setup.php');"

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 PRACTICAL USE OF DOCKER |

… as development environment

Configure once & replicate as needed FROM centos:latest RUN yum install php php-mysql php-xdebug # other commands ... ● Prepare the dockerfile manually by WORKDIR /var/www/project/ supplying all commands CMD ["php", "-S 0.0.0.0:8080", "-t web"]

● Use a image (docker hub) to create FROM centos:latest WORKDIR /var/www/project/ a container, configure it and commit CMD ["php", "-S 0.0.0.0:8080", "-t web"]

changes. # … then $ docker commit

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 PRACTICAL USE OF DOCKER |

… as development environment

Keep developers machine clean Php5 Php5.6 ● Every project a docker image+container Php7.1 ● Every docker image own version of php & MySql8

project config mariaDB

Php7.2

MySql5.5

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 PRACTICAL USE OF DOCKER |

… as development environment

Easy upgrade or apply changes

● When new feature that require additional packages is being implemented ● When project is being migrated to another version of package that implies architecture changes ● Hardware change

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 PRACTICAL USE OF DOCKER |

Sample implementation / Demo app

Prepare the docker-compose.yml file

Install and configure system packages

Configure php and required packages

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 PRACTICAL USE OF DOCKER |

Sample implementation / Demo app

Configure php package manager and required packages

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 PRACTICAL USE OF DOCKER |

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 PRACTICAL USE OF DOCKER |

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001 PRACTICAL USE OF DOCKER |

© 2017 Amdaris Group LTD All rights reserved. Amdaris are Microsoft Gold Partners in Application Development and are certified under ISO 9001 and ISO 27001