Developing and Deploying PHP with Docker

  • Published on
    07-Jan-2017

  • View
    68

  • Download
    6

Embed Size (px)

Transcript

  • Developing and Deploying PHP with Docker

    Midwest PHP 2015

  • Patrick Mizer

    SpareFoot - Austin, TXZCE: PHP4, PHP5, ZF

    We 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

    apps

  • First, what I am NOT going to cover

  • First, what I am NOT going to cover

    Docker internals LXC Containers

    Kernel namespacing (pid, mnt, net, user, ipc) Control groups (blkio, cpu, devices)

  • First, what I am NOT going to cover

    The 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 block

  • What I am going to cover

  • What 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 Server

    Host OS

    Libs

    App

    Traditional server model

  • Physical Server

    Host OS

    Libs

    App

    Physical Server

    Host OS

    Hypervisor

    Guest OS

    Libs

    App

    Guest OS

    Libs

    App

    Traditional server model Virtual machine model

  • Physical Server

    Host OS

    Hypervisor

    Guest OS

    Libs

    App

    Guest OS

    Libs

    App

    Virtual machine model

  • Physical Server

    Host OS

    Hypervisor

    Guest OS

    Libs

    App

    Guest OS

    Libs

    App

    Physical Server

    Host OS

    Docker Engine

    Libs

    App

    Libs

    App

    Virtual machine model Docker model

  • Physical Server

    Host OS

    Docker Engine

    Libs

    App

    Libs

    App Docker Containers Smaller than VMs (no O/S) Isolation of VMs without overhead

    The Container

  • The Container Metaphor

  • The Container Metaphor

    Things to ship

    Platformsfor shipped

    goods

  • The Container Metaphor

    Things to ship

    Platformsfor shipped

    goods

  • The Container Metaphor

  • The Container Metaphor

  • The Container Metaphor

    Technologies Platforms

  • The Container Metaphor

    Platforms

  • The Container Metaphor

  • The Container Metaphor

  • Got 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 drift

  • Why 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, Network

  • Stable Dev Environment

    Virtual Machine

    % vagrant up

  • Stable Dev Environment

    Virtual Machine

    % vagrant up

  • But how do you model this?

    Presentation

    Service

    Data

    [ JSON / REST API ]

  • Stable Dev Environment

    Virtual Machine

    % vagrant up

  • Stable Dev Environment

    % vagrant up

  • Stable Dev Environment

    PHP App 1

    Redis Cache

    Solr Search

    MySQL DB

    Virtual Machine

    Containers% vagrant up

    PHP App 2

  • Separation of concerns

  • Developers

  • Developers

    Inside the container: Code Libraries Package Manager Data

  • DevOps

  • DevOps

    Outside the container: Logging Monitoring Network Configuration

  • Immutable Build Artifacts

    2.4.10

    5.6.5

    5.6.23

    5.5.21

    5.6.23

    2.4.12

    5.5.21

    5.6.3

    2.4.09

    14.10

    14.04

    Production

    Development

    Local

  • Immutable Build Artifacts

    2.4.10

    5.6.5

    5.6.23

    5.5.21

    5.6.23

    2.4.12

    5.5.21

    5.6.3

    2.4.09

    14.10

    14.04

  • Immutable Build Artifacts

    2.4.10

    5.6.5

    5.6.23

    5.5.21

    5.6.23

    2.4.12

    5.5.21

    5.6.3

    2.4.09

    14.10

    14.04

  • Immutable Build Artifacts

    2.4.10

    5.6.5

    5.6.23

    5.5.21

    5.6.23

    2.4.12

    5.5.21

    5.6.3

    2.4.09

    14.10

    14.04

  • Immutable Build Artifacts

    2.4.10

    5.6.5

    5.6.23

    5.5.21

    5.6.23

    2.4.12

    5.5.21

    5.6.3

    2.4.09

    14.10

    14.04

  • Immutable Build Artifacts

    2.4.10

    5.6.5

    5.6.23

    5.5.21

    5.6.23

    2.4.12

    5.5.21

    5.6.3

    2.4.09

    14.10

    14.04

  • Immutable Build Artifacts

    2.4.10

    5.6.5

    5.6.23

    5.5.21

    5.6.23

    2.4.12

    5.5.21

    5.6.3

    2.4.09

    14.10

    14.04

  • Immutable Build Artifacts

    14.10

    14.04

    Production

    Development

    Local 2.4.125.6.7

    2.4.10

    5.5.0

    2.4.10

    5.5.0

  • Immutable Build Artifacts

    14.10

    14.04

    2.4.12

    5.6.7

    2.4.10

    5.5.0

    2.4.10

    5.5.0

  • Immutable Build Artifacts

    14.10

    14.04

    2.4.12

    5.6.7

    2.4.10

    5.5.0

    2.4.10

    5.5.0

  • Immutable Build Artifacts

    14.10

    14.04

    2.4.12

    5.6.7

    2.4.10

    5.5.0

    2.4.10

    5.5.0

    2.4.12

    5.6.7

  • Immutable Build Artifacts

    14.10

    14.04

    2.4.12

    5.6.7

    2.4.10

    5.5.0

    2.4.10

    5.5.0

    2.4.12

    5.6.7

  • Immutable Build Artifacts

    14.10

    14.04

    2.4.12

    5.6.7

    2.4.10

    5.5.0

    2.4.10

    5.5.0

    2.4.12

    5.6.7

    2.4.12

    5.6.7

  • Immutable Build Artifacts

    14.10

    14.04

    2.4.12

    5.6.7

    2.4.10

    5.5.0

    2.4.10

    5.5.0

    2.4.12

    5.6.7

    2.4.12

    5.6.7

  • Immutable Build Artifacts

    14.10

    14.04

    2.4.12

    5.6.7

    2.4.10

    5.5.0

    2.4.10

    5.5.0

    2.4.12

    5.6.7

    2.4.12

    5.6.7

    OpsDev

  • Lets build something...

  • Docker Concepts

    Docker Daemon

    Container 1

    Container 2

    Container 3

    Container N

    Docker CLI

    docker psdocker pulldocker rundocker kill...

    Docker Host Docker Registry

    Image 1

    Image 2

    Image 3

    Image N

  • Building our first container

    % mkdir first

    % cd first

    % touch index.php

    % touch Dockerfile

  • index.php

  • The Dockerfile

    FROM ubuntu:trusty

    RUN apt-get update && \

    apt-get -y install php5-cli

    ADD . /app

    WORKDIR /app

    EXPOSE 8080

    CMD ["php", "-S", "0.0.0.0:8080"]

  • The Dockerfile

    FROM ubuntu:trustyRUN apt-get update && \

    apt-get -y install php5-cli

    ADD . /app

    WORKDIR /app

    EXPOSE 8080

    CMD ["php", "-S", "0.0.0.0:8080"]

  • The Dockerfile

    FROM ubuntu:trusty

    RUN apt-get update && \

    apt-get -y install php5-cliADD . /app

    WORKDIR /app

    EXPOSE 8080

    CMD ["php", "-S", "0.0.0.0:8080"]

  • The Dockerfile

    FROM ubuntu:trusty

    RUN apt-get update && \

    apt-get -y install php5-cli

    ADD . /app

    WORKDIR /appEXPOSE 8080

    CMD ["php", "-S", "0.0.0.0:8080"]

  • The Dockerfile

    FROM ubuntu:trusty

    RUN apt-get update && \

    apt-get -y install php5-cli

    ADD . /app

    WORKDIR /app

    EXPOSE 8080CMD ["php", "-S", "0.0.0.0:8080"]

  • The Dockerfile

    FROM ubuntu:trusty

    RUN apt-get update && \

    apt-get -y install php5-cli

    ADD . /app

    WORKDIR /app

    EXPOSE 8080

    CMD ["php", "-S", "0.0.0.0:8080"]

    The Docker way: 1 process per container

  • The Dockerfile

    FROM ubuntu:trusty

    RUN apt-get update && \

    apt-get -y install php5-cli

    ADD . /app

    WORKDIR /app

    EXPOSE 8080

    CMD ["php", "-S", "0.0.0.0:8080"]

    Im Lazy: /usr/sbin/apache2ctl -D FOREGROUND

  • Build our image

    % docker build -t mizer/helloworld .

  • Build our image

    Docker Daemon

    Docker CLI

    docker build -t mizer/helloworld .

    Docker Host Docker Registry

    mizer/helloworld

    ubuntu

  • The Dockerfile

    FROM ubuntu:trusty

    RUN apt-get update && \

    apt-get -y install php5-cli

    ADD . /app

    WORKDIR /app

    EXPOSE 8080

    CMD ["php", "-S", "0.0.0.0:8080"]

    step 0

    step 1

    step 2

    step 4

    step 5

    step 3

    Each step is cached :)

  • Run our first container

    % sudo docker run -p 8080:8080

    --name hello_1 -d mizer/helloworld

  • Run our first container

    Docker Daemon

    Docker CLI

    docker run -p 8080:8080 --name hello_1 -d mizer/helloworld

    Docker Host Docker Registry

    mizer/helloworld

    hello_1

  • Check our running containers

    % docker ps

  • Check our running containers

    % docker ps

    CONTAINER ID IMAGE COMMAND PORTS NAMES

    5f1b82bb90b8 mizer/helloworld:latest "php -S 0.0.0.0:8080 8080->8080/tcp hello_1

  • Create another

    % docker run -p 8181:8080

    --name hello_2 -d mizer/helloworld

  • Docker Concepts

    Docker Daemon

    Docker CLI

    docker run -p 8181:8080 --name hello_2 -d mizer/helloworld

    Docker Host Docker Registry

    mizer/helloworld

    hello_1

    hello_2

  • Check our running containers

    % docker ps

    CONTAINER ID IMAGE COMMAND PORTS NAMES

    5f1b82bb90b8 mizer/helloworld:latest "php -S 0.0.0.0:8080 8080->8080/tcp hello_1

    3d1b8ccb80c7 mizer/helloworld:latest "php -S 0.0.0.0:8080 8181->8080/tcp hello_2

  • Lets remove both containers

    % sudo docker rm -f hello_1 hello_2

  • Lets remove both containers

    Docker Daemon

    Docker CLI

    docker rm -f hello_1 hello_2

    Docker Host Docker Registry

    mizer/helloworld

  • and the image

    % sudo docker rmi -f mizer/helloworld

  • and the image

    Docker Daemon

    Docker CLI

    docker run -p 8080:8080 --name hello_2 -d mizer/hello

    Docker Host Docker Registry

  • Creating a base image

    FROM ubuntu:trusty

    RUN apt-get update && \

    apt-get -y install php5-cli

    % docker build -t mizer/php

  • Creating a base image

    Docker Daemon

    Docker CLI

    docker build -t mizer/php .

    Docker Host Docker Registry

    mizer/php

  • Using our base image

    FROM mizer/php:latest

    ADD . /app

    WORKDIR /app

    EXPOSE 8080

    CMD ["php", "-S", "0.0.0.0:8080"]

    % sudo docker build -t mizer/helloworld .

  • Creating a base image

    Docker Daemon

    Docker CLI

    docker build -t mizer/php .

    Docker Host Docker Registry

    mizer/php

    mizer/helloworld

  • Create the container

    % sudo docker run -p 8080:8080

    --name hello_1 -d mizer/helloworld

  • Create the container

    Docker Daemon

    Docker CLI

    docker run -p 8080:8080 --name hello_1 -d mizer/hello

    Docker Host Docker Registry

    mizer/php

    mizer/hellohello_1

  • Community images

    % docker search php

  • Community images

    % docker search phpNAME DESCRIPTION STARS OFFICIAL AUTOMATED

    php 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 architecture

  • A modern architecture

    Presentation

    Service

    Data

    [ JSON / REST API ]

  • A modern architecture

    Docker Daemon

    Docker CLIDocker Host Docker Registry

    mizer/php

    mizer/webweb_1

    service_1

    data_1

    mizer/service

    mizer/data

    mizer/mysql

    mizer/data

  • Got it?

  • Docker Orchestration

    Compose Machine Swarm

  • South Africa

  • Fig Docker Compose

    Compose 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:

    - mysql

  • Docker Compose

    % docker-compose up

  • Docker-compose

    Docker Daemon

    Docker CLI

    Docker Host Docker Registry

    mizer/php

    mizer/webweb_1

    service_1

    data_1

    mizer/service

    mizer/data

    mizer/mysql

    mizer/data

    docker-compose.yml

    % docker-compose up

  • Docker Machine

    Docker 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.

  • Swarm

    Docker CLI

    Docker Daemon

    Container 1

    Container 2

    Container 3

    Docker Host

    Docker Daemon

    Container 1

    Container 2

    Container 3

    Docker Host

    Docker Daemon

    Container 1

    Container 2

    Container 3

    Docker Host

    Docker-machine

  • Docker Swarm

    Docker 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 pets

  • Cows not pets

    Pets Pets have names They are unique, loving, hand

    raised and cared for When they are sick, they are

    taken to the vet.

  • Cows not pets

    Pets 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 pets

    Pets 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....?

  • Swarm

    Docker CLI

    Docker Daemon

    Container 1

    Container 2

    Container 3

    Docker Host

    Docker Daemon

    Container 1

    Container 2

    Container 3

    Docker Host

    Docker Daemon

    Container 1

    Container 2

    Container 3

    Docker Host

    Swarm

  • Live demo...

  • Thank you!

    patrick@sparefoot.com

    Github: https://github.com/maximizer I put all of the examples in

    docker-midwestphp15

    https://joind.in/13076

    mailto:patrick@sparefoot.commailto:patrick@sparefoot.comhttps://github.com/maximizerhttps://joind.in/13076https://joind.in/13076