Upload
yosh-de-vos
View
403
Download
0
Embed Size (px)
Citation preview
PHP development with Docker
Introduction to PHP development using Docker containers
March 23rd, 2016 - Yosh de Vos
Setting up the development stack
Why Docker?• Play with new technology• Performance issues with
Vagrant on Windows• Runs native on Linux• Why not?
The setupSoftware
• Docker Engine• Docker Machine• Docker Compose
Stacks
• Apache• Nginx
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
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
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
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
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.
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
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; }}
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;}
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
docker-compose.yml
nginx
mysql_data
http -> 80, https -> 443
ssh -> 2022
mysql -> 3306Docker
Machine
php-fpm
<application>.dev mysql
memcached
The setup
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.
Thank you@yoshzz
github.com/yoshzhub.docker.com/u/yoshz