Slide 1

Slide 1 text

Building Development Environments with Docker Josh Butts Zendcon 2015

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

About Offers.com • We help people save money • Launched in 2009 • 100k lines of PHP across several apps • Millions of Uniques / Month • Runs almost entirely in Docker 3

Slide 4

Slide 4 text

Agenda In No Particular Order • What is Docker • Docker Terminology • Flow of Data in Docker • Running Docker • Docker CLI tools • Anatomy of a Dockerfile • Building a Dockerfile for PHP apps • Images & Containers • Docker orchestration • Docker Compose • Registries & Sharing Containers • Putting it all together

Slide 5

Slide 5 text

DOCKER? What is 5

Slide 6

Slide 6 text

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

Slide 7

Slide 7 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 7

Slide 8

Slide 8 text

High Level Overview 8

Slide 9

Slide 9 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 9

Slide 10

Slide 10 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 10

Slide 11

Slide 11 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 11

Slide 12

Slide 12 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 12

Slide 13

Slide 13 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 13

Slide 14

Slide 14 text

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

Slide 15

Slide 15 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 15

Slide 16

Slide 16 text

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

Slide 17

Slide 17 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 17

Slide 18

Slide 18 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! 18

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

DOCKER What does it mean to run 20

Slide 21

Slide 21 text

Running the Docker Daemon • Unless you’re running Linux, you need a VM (i.e. dev environment) • For MacOS / Windows, you want Vagrant for this • docker -d 21

Slide 22

Slide 22 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 22

Slide 23

Slide 23 text

HELLO WORLD EXAMPLE Lets try a 23

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 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 26

Slide 27

Slide 27 text

DOCKERFILE Lets actually write a 27

Slide 28

Slide 28 text

Typical Non-Linux Setup 28 MacOS Projects / Source Code Vagrant VM CoreOS Docker Daemon Containers Containers Containers Containers Docker CLI

Slide 29

Slide 29 text

More on CoreOS • “Docker Native” • Auto Self-upgrading • Pay attention^^ • No package manager • Minimal wasted non-Docker resources 29

Slide 30

Slide 30 text

Notes on Sample Docker VM • Based on CoreOS • Designed for development • Designed to be your only dev VM • Designed to run lots of containers simultaneously 30

Slide 31

Slide 31 text

DOCKER VM Running a development 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

WORDPRESS Lets build a Dockerfile for 36

Slide 37

Slide 37 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 37

Slide 38

Slide 38 text

Sharing Images • You probably want to run your own registry • SSL is required, you need a real TLD • This is a giant pain in the ass • Check out Quay.io 38

Slide 39

Slide 39 text

CI Strategies • Auto build images from all pull request branches • Tag with :pr1234 • Use docker-compose to bring up test integration environment • Mysql with a ramdisk 39

Slide 40

Slide 40 text

QUESTIONS? Anyone have 40

Slide 41

Slide 41 text

JOIND.IN/15498 I’d love your feedback: 41