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

Containerizing PHP Applications (Lone Star PHP ...

Containerizing PHP Applications (Lone Star PHP 2017)

Josh Butts

April 21, 2017

Transcript

39 slides
  1. About Me • VP of Engineering
 at Offers.com • Austin

    PHP Organizer • github.com/jimbojsb • @jimbojsb 2
  2. 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 3
  3. 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
  4. 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 9
  5. 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 11
  6. 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 12
  7. 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 13
  8. 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 14
  9. 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 15
  10. Docker Dictionary - Registry • A server that stores images

    • Provides organization and authentication as needed • Facilitates sharing of pre-built images • Example: DockerHub, Amazon ECR 16
  11. 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 17
  12. 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 19
  13. 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! 20
  14. Docker vs Virtualization • Docker is lightweight • No overhead

    of a guest OS • Docker can be controlled / contained by systemd and cgroups 21
  15. 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 23
  16. 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 24
  17. Typical Non-Linux Setup 25 MacOS Projects / Source Code VM

    Linux Docker Daemon Containers Containers Containers Containers Docker CLI
  18. Naming Containers • Containers get a name • It’s different

    from the name of the image • It’s dumb by default • use --name 28
  19. 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 29
  20. 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 31
  21. Running Containers • docker run --name [image] • At runtime

    you can • add volumes • specify port mappings • change the CMD 32
  22. My Container Philosophy • ONE Dockerfile for your app •

    Use that docker file for dev and prod • Default to prod, configure for dev 34
  23. 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 35

More Decks by Josh Butts

Other Decks in Technology