php development with docker

16
PHP development with Docker Introduction to PHP development using Docker containers March 23rd, 2016 - Yosh de Vos

Upload: yosh-de-vos

Post on 15-Feb-2017

403 views

Category:

Presentations & Public Speaking


0 download

TRANSCRIPT

Page 1: PHP development with Docker

PHP development with Docker

Introduction to PHP development using Docker containers

March 23rd, 2016 - Yosh de Vos

Page 2: PHP development with Docker

Setting up the development stack

Page 3: PHP development with Docker

Why Docker?• Play with new technology• Performance issues with

Vagrant on Windows• Runs native on Linux• Why not?

Page 4: PHP development with Docker

The setupSoftware

• Docker Engine• Docker Machine• Docker Compose

Stacks

• Apache• Nginx

Page 5: PHP development with Docker

The Apache stack

Apache + PHP: custom build image with Apache, mod_php and SSH access

Mysql: official Mysql or Mariadb image listening on port 3306 using volumes from data container

Memcached:official image with memory limitation listening on port 11211

Mysql

Apache-PHP

Memcached

Mysql Data

Page 6: PHP development with Docker

The Nginx stack

Nginx: official Docker image listening on port 80and using fastcgi for PHP-FPM

Mysql: official Mysql or Mariadb image listening on port 3306 using volumes from data container

Memcached:official image with memory limitation listening on port 11211

PHP-FPM:custom build image with PHP-FPM and SSH access

Mysql

Nginx

PHP-FPM

Memcached

Mysql Data

Page 7: PHP development with Docker

The PHP image

Custom build image based on phusion/baseimage:• Ubuntu 14.04 LTS• Easy to use init system• Syslog and logrotate configured correctly• Crond• Ondrej Sury PPA repository

Running services:• Postfix• Syslogd• Crond• PHP-FPM or Apache

Different images for:

• PHP 5.5, 5.6 or 7.0• PHP-FPM or Apache with mod_php• With or without development tools

Page 8: PHP development with Docker

The PHP development image

A layer on top of the PHP image to add functionality for development environment like:

• SSH server• Xdebug• Custom entrypoint.sh to create user on first boot• Various development tools

Development tools:• Phing• Phpcs• Phpmd• Phpunit• Bower• Grunt• Gulp• Npm

Page 9: PHP development with Docker

Container access

• Using SSH to access the container using agent forwarding

• The same username and userid as the host OS is available inside the container to preserve ownership and permissions

• Optimized Bash prompt and installed all required tools for development

• All sudo rights available when needed

Entrypoint.shA small script set as entrypoint that creates a new user on first boot of the container using environment variables.This makes sure that the user on the host OS is available inside the container with the same username and userid.

Page 10: PHP development with Docker

Linked volume

The /var/www volume is shared with the host OS.

● Directly linked from the host OS● No need for continuously syncing during

development● State between container lifecycles remain

• /var/www/• .ssh/• sites/

• my-application/• web/

• .bash_history• .bashrc• .gitconfig

Page 11: PHP development with Docker

Container linking

Using container linking to enable DNS aliases inside containers.In this way the DNS alias can used inside your app configuration.

● The mysql and memcached containers are linked to the PHP container and are available as hostname “mysql” and “memcached”.

● The PHP container is linked to Nginx and is available as hostname “php”.

# parameters.ymlparameters:

database_host: mysql memcached_host: memcached

# nginx.confserver { location ~ \.php(/|$) { fastcgi_pass php:9000; }}

Page 12: PHP development with Docker

Virtualhost configurationConfiguration for Nginx/Apache is stored outside the container so it easy to add virtualhosts for each application.

The default virtualhost resolves all domain names to document root: ./www/sites/<domain>/web

# apache.conf<Virtualhost *:80> ServerName sites.dev DirectoryIndex index.html index.php app.php VirtualDocumentRoot /var/www/sites/%1/web</Virtualhost>

# nginx.confserver { server_name ~^(?<domain>.+)\.dev$; root /var/www/sites/$domain/web;}

Page 13: PHP development with Docker

DNS aliasesSolutions:• Use static ip address• Use dnsmasq for wildcard

subdomains like *.dev• Add each hostname to

/etc/hosts

# /etc/dnsmasq.confaddress=/dev/192.168.99.100

# /etc/hosts192.168.99.100 application.dev

Page 14: PHP development with Docker

docker-compose.yml

nginx

mysql_data

http -> 80, https -> 443

ssh -> 2022

mysql -> 3306Docker

Machine

php-fpm

<application>.dev mysql

memcached

The setup

Page 15: PHP development with Docker

Resources

PHP docker imageshttps://github.com/yoshz/docker-phphttps://hub.docker.com/u/yoshz/

Bunch of dockerfiles to can build different types of PHP containers.

- PHP 5.5, 5.6 or 7.0- Apache + mod_php or Nginx + php-fpm- Separate images with development tools

integrated

PHP Development Stackhttps://github.com/yoshz/phpdevstack

Bunch of scripts and docker-compose files to quickly start your own development stack with PHP.

Page 16: PHP development with Docker

Thank you@yoshzz

github.com/yoshzhub.docker.com/u/yoshz