84
Docker for PHP Developers Chris Tankersley @dragonmantank ZendCon 2016, October 2016 ZendCon, October 2016 1

Docker for PHP Developers - ZendCon 2016

Embed Size (px)

Citation preview

Page 1: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 1

Docker for PHP DevelopersChris Tankersley@dragonmantankZendCon 2016, October 2016

Page 2: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 2

What Is Docker?“Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications. Consisting of Docker Engine, a portable, lightweight runtime and packaging tool, and Docker Hub, a cloud service for sharing applications and automating workflows, Docker enables apps to be quickly assembled from components and eliminates the friction between development, QA, and production environments.”

https://www.docker.com/whatisdocker/

Page 3: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 3

Containers

Page 4: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 4

Normal Bare-Metal Server

CPU RAM HD Network

Operating System

nginx PHP DB

Page 5: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 5

Virtual Machines

CPU RAM HD Network

Operating System

nginx PHP DB

Operating System

nginx PHP DB

Operating System

Hypervisor

Page 6: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 6

Containers

CPU RAM HD Network

Operating System

nginxnginx PHP DB PHP DB

Page 7: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 7

Containers Are Not New• LXC (Linux Containers)• OpenVZ• Systemd-nspawn• Qemu/kvm• BSD Jails• Solaris Zones• chroot

Page 8: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 8

Docker is an Ecosystem

Docker Engine

Page 9: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 9

Docker is an Ecosystem

Docker ComposeDocker Machine Docker Swarm

Page 10: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 10

How does it work?

Uses a variety of existingContainer technologies

Server ContainersHyper-V Containers xhyve Virtualization

Page 11: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 11

Sorry OSX < 10.10 and Windows < 10 Users

Docker Toolbox

Page 12: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 12

Let’s use Docker

Page 13: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 13

Running a container• `docker run` will run a container• This will not restart an existing container, just create a new one• docker run [options] IMAGE [command] [arguments]• [options ]modify the docker process for this container• IMAGE is the image to use• [command] is the command to run inside the container• [arguments] are arguments for the command

Page 14: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 14

Running a simple shell

Page 15: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 15

Running a simple shell

Page 16: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 16

Running a simple shell

Page 17: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 17

What’s Going On?

Ubuntu Kernel

/+ bin/+ etc/+ dev/+ home/+ usr/+ var/+ lib/+ …

nginx

bash

/+ bin/+ etc/+ dev/+ home/+ usr/+ var/+ lib/+ …

php

Page 18: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 18

Running Two Webservers

Page 19: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 19

Running Two Webservers

Page 20: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 20

Running Two Webservers

Page 21: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 21

Running Two Webservers

Page 22: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 22

Running Two Webservers

Page 23: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 23

Running Two Webservers

Page 24: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 24

Running Two Webservers

Page 25: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 25

Running Two Webservers

Page 26: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 26

Some Notes• All three containers are 100% self contained• Docker containers share common ancestors, but keep their own files• `docker run` parameters:• --rm – Destroy a container once it exits• -d – Run in the background (daemon mode)• -i – Run in interactive mode• --name – Give the container a name• -p [local port]:[container port] – Forward the local port to the container port

Page 27: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 27

Volumes

Page 28: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 28

Modifying a running container• `docker exec` can run a command inside of an existing container• Use Volumes to share data

Page 29: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 29

Persistent Data with Volumes• You can designate a volume with –v• Create a named volume with `volume create`• Volumes can be shared amongst containers• Volumes can mount data from the host system

Page 30: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 30

Mounting from the host machine

Page 31: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 31

Mounting from the host machine

Page 32: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 32

Mounting from the host machine

Page 33: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 33

Mounting from the host machine

Page 34: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 34

Mounting from the host machine

Page 35: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 35

Mounting from the host isn’t perfect• The container now has a window into your host machine• Permissions can get screwy if you are modifying in the container• Most things it creates will be root by default, and you probably aren’t root on

the host machine

• Host-mounted volumes are not portable at all• OSX and Hyper-V VMs have limited pathings to mount• OSX has poor I/O performance

Page 36: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 36

Named Data Volumes• Creates a space that becomes persistent• Can be mounted anywhere inside your images• Have our app containers use the data volume to store data• Use ‘editor containers’ to go in and modify data when needed

Page 37: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 37

Mounting Data Volumes

Page 38: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 38

Mounting Data Volumes

Page 39: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 39

Mounting Data Volumes

Page 40: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 40

Mounting Data Volumes

Page 41: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 41

Mounting Data Volumes

Page 42: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 42

Mounting Data Volumes

Page 43: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 43

Why go through the hassle?• Data volumes are portable, depending on the driver• Data volumes are safer• Separates the app containers from data• Production can use a data volume, dev can use a host volume

• Our app containers stay small• Works directly with other tools

Page 44: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 44

Network Linking

Page 45: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 45

Docker Links• Allows containers to ‘see’ each other over the network• Each container thinks the other one is just another machine• Containers all have an internal network address, so we don’t need to

expose everything through the host• Legacy Links work with `--link`• Can set up virtual networks

Page 46: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 46

More Traditional Setup

INTARWEBS Nginx PHP-FPM

Data Volume

Port 9000

Editor

Page 47: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 47

Mounting Data Volumes

Page 48: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 48

Mounting Data Volumes

Page 49: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 49

Mounting Data Volumes

Page 50: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 50

Mounting Data Volumes

Page 51: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 51

Mounting Data Volumes

Page 52: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 52

Mounting Data Volumes

Page 53: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 53

Let’s Build It

Page 54: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 54

Let’s Build It

Page 55: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 55

Let’s Build It

Page 56: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 56

Let’s Build It

Page 57: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 57

Let’s Build It

Page 58: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 58

More Notes!• We can now rebuild sections of the app as needed• We can restart nginx without impacting PHP• We can extend much easier

• Docker 1.12 has added a whole bunch of new stuff

Page 59: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 59

BREAK TIME! WOO!

Page 60: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 60

Other Helpful Commands

Page 61: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 61

Inspect a containerdocker inspect [options] CONTAINER_NAME

• Returns a JSON string with data about the container• Can also query• docker inspect -f “{{ .NetworkSettings.IPAddress }}” web_server

• Really handy for scripting out things like reverse proxies

Page 62: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 62

Work with images• docker pull IMAGE – Pulls down an image before using• docker images – Lists all the images that are downloaded• docker rmi IMAGE – Deletes an image if it’s not being used

Page 63: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 63

Our Goals• Not change our workflow (much)• Run PHP 7, Unit Tests, and webserver• Deploy “easily”

Page 64: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 64

Containerizing Commands

Page 65: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 65

Running Composerdocker run --rm \ -v c:/Users/drago/.composer:/root/.composer \ -v c:/Users/drago/Projects/workshop:/app \ -v c:/Users/drago/.ssh:/root/.ssh \ composer/composer \ require phpunit/phpunit

Page 66: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 66

Functions!function docker-composer() { appname=$(basename `pwd -P`) appname="${appname/-/}" imagename='composer/composer' output=$(docker images | grep "${appname}_composer") if [ "$?" = "0" ]; then imagename="${appname}_composer" fi docker run --rm -v ~/.composer:/root/.composer -v $(pwd):/app -v ~/.ssh:/root/.ssh $imagename $*}

Page 67: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 67

Add our code to the autloader{ "require": { "phpunit/phpunit": "^5.5" }, "autoload": { "psr-4": { "DemoApp\\": "src/" } }}

Page 68: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 68

Run our appdocker run -d --name phptest \ -v c:/Users/drago/Projects/workshop/:/app \ -w /app/html \ -p 8080:80 \ php:cli \ php -S 0.0.0.0:80

Page 69: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 69

Unit Test our Codedocker run --rm -ti \ -v c:/Users/drago/Projects/workshop/:/app -w /app php:cli vendor/bin/phpunit -c phpunit.dist.xml

Page 70: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 70

Docker Compose

Page 71: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 71

What is Docker Compose?• Multi-container orchestration• A single config file holds all of your container info• Works with Docker Swarm and a few other tools, like Rancher

Page 72: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 72

Sample docker-compose.ymlversion: ‘2’

volumes: mysqldata: driver: local

phpserver: build: ./docker/php volumes: - ./:/var/www/

mysqlserver: image: mysql environment: MYSQL_DATABASE: dockerfordevs MYSQL_ROOT_PASSWORD: docker volumes: - mysqldata:/var/lib/mysql

nginx: build: ./docker/nginx ports: - "80:80" - "443:443"

Page 73: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 73

Creating your own Images

Page 74: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 74

Dockerfile• Dockerfile is the configuration steps for an image• Can be created from scratch, or based on another image• Allows you to add files, create default volumes, ports, etc• Can be used privately or pushed to Docker Hub

Page 75: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 75

FROM php:7RUN apt-get update \ && apt-get install –y \ libmcrypt-dev \ libpng12-dev \ libfreetype6-dev \ libjpeg62-turbo-dev \ && docker-php-ext-install iconv mcrypt pdo pdo_mysql

COPY build/app /var/www# …EXPOSE 80 443VOLUME /var/wwwVOLUME /var/log

RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

Page 76: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 76

Build itdocker build -t tag_name ./

• This runs through the Dockerfile and generates the image• We can now use the tag name to run the image

Page 77: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 77

Add in some Compose

Page 78: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 78

Start the app with Compose

Page 79: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 79

Docker Machine

Page 80: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 80

What is Docker Machine?• A provisioning tool that is used to set up a box with Docker• Used in Docker Toolbox to create the VM• Supports:• EC2• Azure• Digital Ocean• Hyper-V• OpenStack• Virtualbox• VMWare

Page 81: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 81

Why use it?• Makes it very easy to spin up new boxes• Docker Machine handles all of the dirty stuff for you• Docker Toolbox users are already using it• Integrates with Docker Swarm

• It is not necessarily portable

Page 82: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 82

Let’s make a machine!

Page 83: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 83

Let’s Connect!

Page 84: Docker for PHP Developers - ZendCon 2016

ZendCon, October 2016 84

Thank You!• Software Engineer for InQuest• Author of “Docker for Developers”• https://leanpub.com/dockerfordevs

• Co-Host of “Jerks Talk Games”• http://jerkstalkgames

• http://ctankersley.com• [email protected]• @dragonmantank