Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Containerizing PHP Applications (PHP World 2016)

Josh Butts
November 15, 2016

Containerizing PHP Applications (PHP World 2016)

Docker is hot right now and if you’re ready to get started, you’ve come to the right place. In this tutorial, we’ll go over basic Docker concepts like Dockerfiles, containers and images. We’ll look at some sample applications including WordPress, Drupal as well as some framework-based examples. We will talk about strategies for building Docker images for these scenarios, and we’ll also cover how to handle external dependencies such as storage, databases, caches in both development and production.

Josh Butts

November 15, 2016
Tweet

More Decks by Josh Butts

Other Decks in Programming

Transcript

  1. Containerizing PHP Applications
    Josh Butts
    PHP World 2016

    View Slide

  2. About Me
    • VP of Engineering

    at Offers.com
    • Austin PHP Organizer
    • github.com/jimbojsb
    • @jimbojsb
    2

    View Slide

  3. 3

    View Slide

  4. About Offers.com
    • We help people save money
    • Launched in 2009
    • 200k lines of PHP across
    several apps
    • Billions of requests / month
    • Runs entirely in Docker
    4

    View Slide

  5. Agenda In No Particular Order
    • What is Docker
    • Docker Terminology
    • Running Docker in Dev vs Prod
    • Docker CLI tools
    • Anatomy of a Dockerfile
    • Building a Dockerfile for PHP apps
    • Wordpress Example
    • Laravel Example
    • Docker orchestration
    • Docker Compose
    • Registries & Sharing Containers

    View Slide

  6. SURVEY
    Quick
    6

    View Slide

  7. CAVEATS
    A Few
    7

    View Slide

  8. DOCKER?
    What is
    8

    View Slide

  9. BUILDING, RUNNING AND
    DISTRIBUTING APPLICATION
    CONTAINERS
    Docker is a tool for
    9

    View Slide

  10. What is Docker?
    • Docker allows you to package
    applications with infrastructure
    • Docker uses Linux kernel container
    technology
    • Docker is well suited for distributed
    and highly available applications
    10

    View Slide

  11. High Level Overview
    11

    View Slide

  12. Who Cares?
    • Docker allows engineers to
    choose exactly the stack for any
    app
    • Containers run anywhere that
    runs Docker
    • Ops focuses on infrastructure
    instead of stacks
    12

    View Slide

  13. Docker Dictionary - Container
    • Container
    • A running instance of your app
    and it’s complete software
    stack
    • You might have many identical
    containers running
    • Often runs just one process
    13

    View Slide

  14. Docker Dictionary - Image
    • An image is the compiled
    distributable version of your app
    and it’s stack
    • Images are built from
    Dockerfiles
    • You can start many containers
    from the same image
    14

    View Slide

  15. Docker Dictionary - Layer
    • Docker uses a layering system
    to optimize storage
    • Each line in your Dockerfile is a
    layer
    • Many layers make up an image
    • Images share common layers
    where possible
    15

    View Slide

  16. Docker Dictionary - Dockerfile
    • The Dockerfile is a text file that
    lives in your project root
    • Describes how to build your image
    • Installs software packages,
    configures runtimes, etc
    • Tells Docker what command to
    run and what ports to open
    16

    View Slide

  17. Docker Dictionary - Registry
    • A server that stores images
    • Provides organization and
    authentication as needed
    • Facilitates sharing of pre-built
    images
    • Example: DockerHub, Amazon
    ECR
    17

    View Slide

  18. The Docker Binary
    • Both the CLI client and the
    server
    • Written in Go
    • Speaks REST-ish over Unix
    socket or TCP/SSL
    • CLI and server can be on
    different hosts
    18

    View Slide

  19. Building Images
    19
    Dockerfile Image
    Container
    Registry
    Image
    Image
    Build
    Push/Pull
    Run

    View Slide

  20. How Containers Run
    • Docker daemon runs as root and
    starts containers
    • Containers are basically stateless
    • Can expose local storage if you
    need to
    • Can’t see each other’s files
    • State is gone on exit
    20

    View Slide

  21. Outsource your Persistence
    • Cloud (AWS) example:
    • RDS for database storage
    • Sessions in Memcached or Redis
    • File storage in S3
    • This may require changes to your
    app!
    21

    View Slide

  22. Docker vs Virtualization
    • Docker is lightweight
    • No overhead of a guest OS
    • Docker can be controlled /
    contained by systemd and
    cgroups
    22

    View Slide

  23. DOCKER
    What does it mean to run
    23

    View Slide

  24. Running the Docker Daemon
    • Unless you’re running Linux,
    you need a VM (i.e. dev
    environment)
    • For MacOS / Windows, Docker
    provides a VM tool
    • docker -d
    24

    View Slide

  25. A Word on the Control OS
    • Any modern Linux should be
    able to run Docker
    • I personally don’t recommend
    Ubuntu
    • CoreOS is a small Linux distro
    that is designed to run Docker in
    production
    25

    View Slide

  26. Typical Non-Linux Setup
    26
    MacOS
    Projects /
    Source Code VM
    Linux
    Docker
    Daemon
    Containers
    Containers
    Containers
    Containers
    Docker
    CLI

    View Slide

  27. HELLO WORLD
    EXAMPLE
    Lets try a
    27

    View Slide

  28. Naming Images
    • hello-world
    • hello-world:latest
    • _/hello-world
    • dockerhub.com/_/hello-world
    28

    View Slide

  29. Naming Containers
    • Containers get a name
    • It’s different from the name of
    the image
    • It’s dumb by default
    • use --name
    29

    View Slide

  30. Dockerfiles
    • FROM - specify your base image
    • RUN - run commands in the
    context
    • ADD - put your files in
    • EXPOSE - listen on ports
    • ENV - set environment vars
    • VOLUME - persistent storage
    30

    View Slide

  31. DOCKERFILE
    Lets actually write a
    31

    View Slide

  32. Building Images from Dockerfiles
    • docker build .
    • use -t to give it a repo / tag
    • docker images to see what’s
    available on your system
    • docker rmi to delete images
    32

    View Slide

  33. Running Containers
    • docker run --name [image]
    • At runtime you can
    • add volumes
    • specify port mappings
    • change the CMD
    33

    View Slide

  34. PHP APPLICATIONS
    Building Dockerfiles for
    34

    View Slide

  35. My Container Philosophy
    • ONE Dockerfile for your app
    • Use that docker file for dev and
    prod
    • Default to prod, configure for
    dev
    35

    View Slide

  36. Docker Compose
    • Docker Compose lets you
    orchestrate containers
    • Doesn’t do anything that Docker
    itself can’t do
    • YAML config file
    • Speaks the Docker Remote API
    36

    View Slide

  37. WORDPRESS
    Lets build a Dockerfile for
    37

    View Slide

  38. LARAVEL
    Lets build a Dockerfile for
    38

    View Slide

  39. Github Links
    • http://github.com/jimbojsb/wp-docker-
    demo
    • http://github.com/jimbojsb/phpcli-
    docker-demo
    • http://github.com/jimbojsb/phpweb-
    docker-demo
    • http://github.com/jimbojsb/laravel-
    docker-demo
    39

    View Slide

  40. JOIND.IN/TALK/9D3FC
    I’d love your feedback:
    40

    View Slide

  41. 2016
    2016

    View Slide