Slide 1

Slide 1 text

Containerizing PHP Applications Josh Butts PHP World 2016

Slide 2

Slide 2 text

About Me • VP of Engineering
 at Offers.com • Austin PHP Organizer • github.com/jimbojsb • @jimbojsb 2

Slide 3

Slide 3 text

3

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

SURVEY Quick 6

Slide 7

Slide 7 text

CAVEATS A Few 7

Slide 8

Slide 8 text

DOCKER? What is 8

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

High Level Overview 11

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

DOCKER What does it mean to run 23

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

HELLO WORLD EXAMPLE Lets try a 27

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

DOCKERFILE Lets actually write a 31

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

PHP APPLICATIONS Building Dockerfiles for 34

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

WORDPRESS Lets build a Dockerfile for 37

Slide 38

Slide 38 text

LARAVEL Lets build a Dockerfile for 38

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

2016 2016