Developing and Deploying PHP with Docker

  • 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