Developing and Deploying PHP with Docker

  • Published on
    07-Jan-2017

  • View
    68

  • Download
    6

Transcript

Developing and Deploying PHP with DockerMidwest PHP 2015Patrick MizerSpareFoot - Austin, TXZCE: PHP4, PHP5, ZFWe use Docker at SpareFoot: Containers for PHP services in dev CI server builds and deploys containers Containers in production for Solr Search and Node.js appsFirst, what I am NOT going to coverFirst, what I am NOT going to coverDocker internals LXC Containers Kernel namespacing (pid, mnt, net, user, ipc) Control groups (blkio, cpu, devices)First, what I am NOT going to coverThe Docker - CoreOS Debate Rocket vs Docker Whether everything running through one daemon is a security risk Whether Docker is a platform or a composable building blockWhat I am going to coverWhat I am going to cover What is Docker? Why you should care about Docker How Docker works Building a Docker container for a PHP application Docker tools Compose Docker-Machine Swarm Live demos (time permitting)What is Docker?What is Docker?Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications. Docker enables apps to be quickly assembled from components and eliminates the friction between development, QA, and production environments.Oh, so its a Virtual Machine?Oh, so its a Virtual Machine?Physical ServerHost OSLibsAppTraditional server modelPhysical ServerHost OSLibsAppPhysical ServerHost OSHypervisorGuest OSLibsAppGuest OSLibsAppTraditional server model Virtual machine modelPhysical ServerHost OSHypervisorGuest OSLibsAppGuest OSLibsAppVirtual machine modelPhysical ServerHost OSHypervisorGuest OSLibsAppGuest OSLibsAppPhysical ServerHost OSDocker EngineLibsAppLibsAppVirtual machine model Docker modelPhysical ServerHost OSDocker EngineLibsAppLibsApp Docker Containers Smaller than VMs (no O/S) Isolation of VMs without overheadThe ContainerThe Container MetaphorThe Container MetaphorThings to shipPlatformsfor shipped goodsThe Container MetaphorThings to shipPlatformsfor shipped goodsThe Container MetaphorThe Container MetaphorThe Container MetaphorTechnologies PlatformsThe Container MetaphorPlatformsThe Container MetaphorThe Container MetaphorGot it?Got it?You really should!Why should you care about Docker?Why you should care Portable runtime environment for your code Stable dev environment Build once, run anywhere*Why you should care Portable runtime environment for your code Stable dev environment Build once, run anywhere* Immutable build artifacts Avoid dependency hell Avoid driftWhy you should care Portable runtime environment for your code Stable dev environment Build once, run anywhere* New technologies! Immutable build artifacts Avoid dependency hell Avoid drift Clear separation of concerns Dev - Code, Dependencies, Libs DevOps - Provisioning, Logging, NetworkStable Dev EnvironmentVirtual Machine% vagrant upStable Dev EnvironmentVirtual Machine% vagrant upBut how do you model this?PresentationServiceData[ JSON / REST API ]Stable Dev EnvironmentVirtual Machine% vagrant upStable Dev Environment% vagrant upStable Dev EnvironmentPHP App 1Redis CacheSolr SearchMySQL DBVirtual MachineContainers% vagrant upPHP App 2Separation of concernsDevelopersDevelopersInside the container: Code Libraries Package Manager DataDevOpsDevOpsOutside the container: Logging Monitoring Network ConfigurationImmutable Build Artifacts2.4.105.6.55.6.235.5.215.6.232.4.125.5.215.6.32.4.0914.1014.04ProductionDevelopmentLocalImmutable Build Artifacts2.4.105.6.55.6.235.5.215.6.232.4.125.5.215.6.32.4.0914.1014.04Immutable Build Artifacts2.4.105.6.55.6.235.5.215.6.232.4.125.5.215.6.32.4.0914.1014.04Immutable Build Artifacts2.4.105.6.55.6.235.5.215.6.232.4.125.5.215.6.32.4.0914.1014.04Immutable Build Artifacts2.4.105.6.55.6.235.5.215.6.232.4.125.5.215.6.32.4.0914.1014.04Immutable Build Artifacts2.4.105.6.55.6.235.5.215.6.232.4.125.5.215.6.32.4.0914.1014.04Immutable Build Artifacts2.4.105.6.55.6.235.5.215.6.232.4.125.5.215.6.32.4.0914.1014.04Immutable Build Artifacts14.1014.04ProductionDevelopmentLocal 2.4.125.6.72.4.105.5.02.4.105.5.0Immutable Build Artifacts14.1014.042.4.125.6.72.4.105.5.02.4.105.5.0Immutable Build Artifacts14.1014.042.4.125.6.72.4.105.5.02.4.105.5.0Immutable Build Artifacts14.1014.042.4.125.6.72.4.105.5.02.4.105.5.02.4.125.6.7Immutable Build Artifacts14.1014.042.4.125.6.72.4.105.5.02.4.105.5.02.4.125.6.7Immutable Build Artifacts14.1014.042.4.125.6.72.4.105.5.02.4.105.5.02.4.125.6.72.4.125.6.7Immutable Build Artifacts14.1014.042.4.125.6.72.4.105.5.02.4.105.5.02.4.125.6.72.4.125.6.7Immutable Build Artifacts14.1014.042.4.125.6.72.4.105.5.02.4.105.5.02.4.125.6.72.4.125.6.7OpsDevLets build something...Docker ConceptsDocker DaemonContainer 1Container 2Container 3Container NDocker CLIdocker psdocker pulldocker rundocker kill...Docker Host Docker RegistryImage 1Image 2Image 3Image NBuilding our first container% mkdir first% cd first% touch index.php% touch Dockerfileindex.phpThe DockerfileFROM ubuntu:trustyRUN apt-get update && \ apt-get -y install php5-cliADD . /appWORKDIR /appEXPOSE 8080CMD ["php", "-S", "0.0.0.0:8080"]The DockerfileFROM ubuntu:trustyRUN apt-get update && \ apt-get -y install php5-cliADD . /appWORKDIR /appEXPOSE 8080CMD ["php", "-S", "0.0.0.0:8080"]The DockerfileFROM ubuntu:trustyRUN apt-get update && \ apt-get -y install php5-cliADD . /appWORKDIR /appEXPOSE 8080CMD ["php", "-S", "0.0.0.0:8080"]The DockerfileFROM ubuntu:trustyRUN apt-get update && \ apt-get -y install php5-cliADD . /appWORKDIR /appEXPOSE 8080CMD ["php", "-S", "0.0.0.0:8080"]The DockerfileFROM ubuntu:trustyRUN apt-get update && \ apt-get -y install php5-cliADD . /appWORKDIR /appEXPOSE 8080CMD ["php", "-S", "0.0.0.0:8080"]The DockerfileFROM ubuntu:trustyRUN apt-get update && \ apt-get -y install php5-cliADD . /appWORKDIR /appEXPOSE 8080CMD ["php", "-S", "0.0.0.0:8080"]The Docker way: 1 process per containerThe DockerfileFROM ubuntu:trustyRUN apt-get update && \ apt-get -y install php5-cliADD . /appWORKDIR /appEXPOSE 8080CMD ["php", "-S", "0.0.0.0:8080"]Im Lazy: /usr/sbin/apache2ctl -D FOREGROUNDBuild our image% docker build -t mizer/helloworld .Build our imageDocker DaemonDocker CLIdocker build -t mizer/helloworld .Docker Host Docker Registrymizer/helloworldubuntuThe DockerfileFROM ubuntu:trustyRUN apt-get update && \ apt-get -y install php5-cliADD . /appWORKDIR /appEXPOSE 8080CMD ["php", "-S", "0.0.0.0:8080"]step 0step 1step 2step 4step 5step 3Each step is cached :)Run our first container% sudo docker run -p 8080:8080 --name hello_1 -d mizer/helloworldRun our first containerDocker DaemonDocker CLIdocker run -p 8080:8080 --name hello_1 -d mizer/helloworldDocker Host Docker Registrymizer/helloworldhello_1Check our running containers% docker psCheck our running containers% docker psCONTAINER ID IMAGE COMMAND PORTS NAMES5f1b82bb90b8 mizer/helloworld:latest "php -S 0.0.0.0:8080 8080->8080/tcp hello_1Create another% docker run -p 8181:8080 --name hello_2 -d mizer/helloworldDocker ConceptsDocker DaemonDocker CLIdocker run -p 8181:8080 --name hello_2 -d mizer/helloworldDocker Host Docker Registrymizer/helloworldhello_1hello_2Check our running containers% docker psCONTAINER ID IMAGE COMMAND PORTS NAMES5f1b82bb90b8 mizer/helloworld:latest "php -S 0.0.0.0:8080 8080->8080/tcp hello_13d1b8ccb80c7 mizer/helloworld:latest "php -S 0.0.0.0:8080 8181->8080/tcp hello_2Lets remove both containers% sudo docker rm -f hello_1 hello_2 Lets remove both containersDocker DaemonDocker CLIdocker rm -f hello_1 hello_2Docker Host Docker Registrymizer/helloworld and the image% sudo docker rmi -f mizer/helloworld and the imageDocker DaemonDocker CLIdocker run -p 8080:8080 --name hello_2 -d mizer/helloDocker Host Docker RegistryCreating a base imageFROM ubuntu:trustyRUN apt-get update && \ apt-get -y install php5-cli% docker build -t mizer/phpCreating a base imageDocker DaemonDocker CLIdocker build -t mizer/php .Docker Host Docker Registrymizer/phpUsing our base imageFROM mizer/php:latestADD . /appWORKDIR /appEXPOSE 8080CMD ["php", "-S", "0.0.0.0:8080"]% sudo docker build -t mizer/helloworld .Creating a base imageDocker DaemonDocker CLIdocker build -t mizer/php .Docker Host Docker Registrymizer/phpmizer/helloworldCreate the container% sudo docker run -p 8080:8080 --name hello_1 -d mizer/helloworldCreate the containerDocker DaemonDocker CLIdocker run -p 8080:8080 --name hello_1 -d mizer/helloDocker Host Docker Registrymizer/phpmizer/hellohello_1Community images% docker search phpCommunity images% docker search phpNAME DESCRIPTION STARS OFFICIAL AUTOMATEDphp While designed for web development, the PH... 148 [OK] tutum/apache-php Apache+PHP base image - listens in port 80... 59 [OK]maxexcloo/nginx-php Docker framework container with Nginx and ... 30 [OK]eboraas/apache-php PHP5 on Apache (with SSL support), built o... 17 [OK]richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 10 [OK]php-zendserver Zend Server - the integrated PHP applicati... 10 [OK] clue/phpvirtualbox phpVirtualBox is a modern web interface de... 9 [OK]jdeathe/centos-ssh-apache-php CentOS-6 6.5 x86_64 / Apache / PHP / PHP m... 8 [OK]jprjr/php-fpm Arch Linux-based image with PHP-FPM instal... 8 [OK]darh/php-essentials Provides a container full of php-cli tools... 7 [OK]million12/nginx-php Nginx + PHP-FPM, CentOS-7 based. 5 [OK]reinblau/php-apache2 Apache2 Server for PHP Projects 4 [OK]eriksencosta/php-dev Docker image with latest supported PHP ver... 4 [OK]alreece45/phusion-nginx-php nginx and php-fpm with configurable docume... 3 [OK]million12/php-app-ssh million12/php-app, extended with SSHD and ... 2 [OK]simpledrupalcloud/php 1 [OK]yoshz/apache-php-dev A docker image with Apache, PHP 5.5, drush... 1 [OK]azukiapp/php-apache Base docker image to run PHP applications ... 1 [OK]A modern architectureA modern architecturePresentationServiceData[ JSON / REST API ]A modern architectureDocker DaemonDocker CLIDocker Host Docker Registrymizer/phpmizer/webweb_1service_1data_1mizer/servicemizer/datamizer/mysqlmizer/dataGot it?Docker Orchestration Compose Machine SwarmSouth AfricaFig Docker ComposeCompose is a tool for defining and running complex applications with Docker. With Compose, you define a multi-container application in a single file, then spin your application up in a single command which does everything that needs to be done to get it running.docker-compose.ymlwebapp1: build: webapp/. command: php -S 0.0.0.0:8000 -t /code ports: - "8000:8000" environment: - DSN=mysql:host=mysql;dbname=test - USERNAME=test - PASSWORD=foo links: - mysqlDocker Compose% docker-compose upDocker-composeDocker DaemonDocker CLIDocker Host Docker Registrymizer/phpmizer/webweb_1service_1data_1mizer/servicemizer/datamizer/mysqlmizer/datadocker-compose.yml% docker-compose upDocker MachineDocker Machine takes you from zero-to-Docker in seconds with a single command.Before Docker Machine, a developer would need to log in to the host and follow installation and configuration instructions specifically for that host and its OS. With Docker Machine, whether provisioning the Docker daemon on a new laptop, on virtual machines in the data center, or on a public cloud instance, the same, single command.SwarmDocker CLIDocker DaemonContainer 1Container 2Container 3Docker HostDocker DaemonContainer 1Container 2Container 3Docker HostDocker DaemonContainer 1Container 2Container 3Docker HostDocker-machineDocker SwarmDocker Swarm is native clustering for Docker. It turns a pool of Docker hosts into a single, virtual host.Swarm serves the standard Docker API, so any tool which already communicates with a Docker daemon can use Swarm to transparently scale to multiple hosts: Dokku, Compose, Krane, Flynn, Deis, DockerUI, Shipyard, Drone, Jenkins... and, of course, the Docker client itself.Cows not petsCows not petsPets Pets have names They are unique, loving, hand raised and cared for When they are sick, they are taken to the vet.Cows not petsPets Pets have names They are unique, loving, hand raised and cared for When they are sick, they are taken to the vet.Cows Cows have numbers They are not unique, they are a commodity. When they are sick....?Cows not petsPets Pets have names They are unique, loving, hand raised and cared for When they are sick, they are taken to the vet.Cows Cows have numbers They are not unique, they are a commodity. When they are sick....?SwarmDocker CLIDocker DaemonContainer 1Container 2Container 3Docker HostDocker DaemonContainer 1Container 2Container 3Docker HostDocker DaemonContainer 1Container 2Container 3Docker HostSwarmLive demo...Thank you!patrick@sparefoot.comGithub: https://github.com/maximizer I put all of the examples in docker-midwestphp15https://joind.in/13076mailto:patrick@sparefoot.commailto:patrick@sparefoot.comhttps://github.com/maximizerhttps://joind.in/13076https://joind.in/13076