docker for developers - sunshine php

97
Docker for PHP Developers Chris Tankersley @dragonmantank Sunshine PHP 2017 Sunshine PHP 2017 1

Upload: chris-tankersley

Post on 09-Feb-2017

115 views

Category:

Technology


6 download

TRANSCRIPT

Page 1: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 1

Docker for PHP DevelopersChris Tankersley@dragonmantankSunshine PHP 2017

Page 2: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 2

What Is Docker?“Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications. Consisting of Docker Engine, a portable, lightweight runtime and packaging tool, and Docker Hub, a cloud service for sharing applications and automating workflows, Docker enables apps to be quickly assembled from components and eliminates the friction between development, QA, and production environments.”

https://www.docker.com/whatisdocker/

Page 3: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 3

Containers

Page 4: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 4

Normal Bare-Metal Server

CPU RAM HD Network

Operating System

nginx PHP DB

Page 5: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 5

Virtual Machines

CPU RAM HD Network

Operating System

nginx PHP DB

Operating System

nginx PHP DB

Operating System

Hypervisor

Page 6: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 6

Containers

CPU RAM HD Network

Operating System

nginxnginx PHP DB PHP DB

Page 7: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 7

Containers Are Not New• LXC (Linux Containers)• OpenVZ• Systemd-nspawn• Qemu/kvm• BSD Jails• Solaris Zones• chroot

Page 8: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 8

Docker is an Ecosystem

Docker Engine

Page 9: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 9

Docker is an Ecosystem

Docker ComposeDocker Machine Docker Swarm

Page 10: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 10

How does it work?

Uses a variety of existingContainer technologies

Server ContainersHyper-V Containers xhyve Virtualization

Page 11: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 11

Sorry OSX < 10.10 and Windows < 10 Users

Docker Toolbox

Page 12: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 12

Let’s use Docker

Page 13: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 13

Running a container• `docker run` will run a container• This will not restart an existing container, just create a new one• docker run [options] IMAGE [command] [arguments]• [options ]modify the docker process for this container• IMAGE is the image to use• [command] is the command to run inside the container• [arguments] are arguments for the command

Page 14: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 14

Running a simple shell

Page 15: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 15

Running a simple shell

Page 16: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 16

Running a simple shell

Page 17: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 17

What’s Going On?

Ubuntu Kernel

/+ bin/+ etc/+ dev/+ home/+ usr/+ var/+ lib/+ …

nginx

bash

/+ bin/+ etc/+ dev/+ home/+ usr/+ var/+ lib/+ …

php

Page 18: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 18

Running Two Webservers

Page 19: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 19

Running Two Webservers

Page 20: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 20

Running Two Webservers

Page 21: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 21

Running Two Webservers

Page 22: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 22

Running Two Webservers

Page 23: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 23

Running Two Webservers

Page 24: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 24

Running Two Webservers

Page 25: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 25

Running Two Webservers

Page 26: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 26

Some Notes• All three containers are 100% self contained• Docker containers share common ancestors, but keep their own files• `docker run` parameters:• --rm – Destroy a container once it exits• -d – Run in the background (daemon mode)• -i – Run in interactive mode• --name – Give the container a name• -p [local port]:[container port] – Forward the local port to the container port

Page 27: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 27

Volumes

Page 28: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 28

Modifying a running container• `docker exec` can run a command inside of an existing container• Use Volumes to share data

Page 29: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 29

Persistent Data with Volumes• You can designate a volume with –v• Create a named volume with `volume create`• Volumes can be shared amongst containers• Volumes can mount data from the host system

Page 30: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 30

Mounting from the host machine

Page 31: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 31

Mounting from the host machine

Page 32: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 32

Mounting from the host machine

Page 33: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 33

Mounting from the host machine

Page 34: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 34

Mounting from the host machine

Page 35: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 35

Mounting from the host isn’t perfect• The container now has a window into your host machine• Permissions can get screwy if you are modifying in the container• Most things it creates will be root by default, and you probably aren’t root on

the host machine

• Host-mounted volumes are not portable at all• OSX and Hyper-V VMs have limited pathings to mount• OSX has poor I/O performance

Page 36: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 36

Named Data Volumes• Creates a space that becomes persistent• Can be mounted anywhere inside your images• Have our app containers use the data volume to store data• Use ‘editor containers’ to go in and modify data when needed

Page 37: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 37

vim Tutorial• vim is a Modal text editor• ESC will drop you back to default mode• :new /opt/webconfig/default to create a new file• In default mode, i will get us into interactive (edit) mode• :w to save a file• :q will quit

Page 38: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 38

Mounting Data Volumes

Page 39: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 39

Mounting Data Volumes

Page 40: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 40

Mounting Data Volumes

Page 41: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 41

Mounting Data Volumes

Page 42: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 42

Mounting Data Volumes

Page 43: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 43

Mounting Data Volumes

Page 44: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 44

Why go through the hassle?• Data volumes are portable, depending on the driver• Data volumes are safer• Separates the app containers from data• Production can use a data volume, dev can use a host volume

• Our app containers stay small• Works directly with other tools

Page 45: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 45

Networking

Page 46: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 46

Networking• Docker can create multiple network “pools”• Each container gets an IP address• Containers can be attached to multiple networks• Docker network allow service discovery inside networks

Page 47: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 47

Legacy - Docker Links• Legacy Links work with `--link`• Only works on the legacy “bridge” network• Doesn’t support service discovery

• Not worth it to use anymore

Page 48: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 48

Docker Networks• Discreet IP pool for containers• Containers can be added and removed to the network at whim• Service discovery though ‘--network-alias’• Can be set up to work across hosts

Page 49: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 49

Create a network

Page 50: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 50

Attach to a network

Page 51: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 51

Ping the web container

Page 52: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 52

Add another web and kill web1

Page 53: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 53

BREAK TIME! WOO!

Page 54: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 54

Other Helpful Commands

Page 55: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 55

Inspect a containerdocker inspect [options] CONTAINER_NAME

• Returns a JSON string with data about the container• Can also query• docker inspect -f “{{ .NetworkSettings.IPAddress }}” web_server

• Really handy for scripting out things like reverse proxies

Page 56: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 56

Work with images• docker pull IMAGE – Pulls down an image before using• docker images – Lists all the images that are downloaded• docker rmi IMAGE – Deletes an image if it’s not being used

Page 57: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 57

Containerizing An Application

Page 58: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 58

Our Goals• Not change our workflow (much)• Run PHP 7, Unit Tests, and webserver• Deploy “easily”

Page 59: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 59

Just try and run itdocker run -d --name d4dapp \ -v C:\drago\Projects\dockerfordevs-app:/var/www/ \ -p 8080:80 php:apache

Page 60: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 60

Page 61: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 61

Checking Logs• Containers log to stdout/stderr• Docker aggregates the logs• Can be viewed with docker logs

Page 62: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 62

Oops

Page 63: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 63

Custom Images• PHP images are pretty bare• Lots of times need to install extensions

Page 64: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 64

Dockerfile• Dockerfile is the configuration steps for an image• Can be created from scratch, or based on another image• Allows you to add files, create default volumes, ports, etc• Can be used privately or pushed to Docker Hub

Page 65: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 65

docker/DockerfileFROM php:apache

RUN a2enmod rewrite

Page 66: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 66

Build itdocker build -t tag_name ./

• This runs through the Dockerfile and generates the image• We can now use the tag name to run the image

Page 67: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 67

Build itdocker build -t d4dapp docker/

Page 68: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 68

Page 69: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 69

Use the new imagedocker run -d --name d4dapp \ -v C:\drago\Projects\dockerfordevs-app:/var/www/ \ -p 8080:80 d4dapp

Page 70: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 70

Use the new image

Page 71: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 71

Slightly better

Page 72: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 72

Install Dependencies

Page 73: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 73

Running Composerdocker run --rm \ -v c:/Users/drago/.composer:/root/.composer \ -v c:/Users/drago/Projects/workshop:/app \ -v c:/Users/drago/.ssh:/root/.ssh \ composer/composer \ install

Page 74: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 74

Better!

Page 75: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 75

Look at queues!

Page 76: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 76

docker/DockerfileFROM php:apache

RUN a2enmod rewrite\ && docker-php-ext-install pdo_mysql

Page 77: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 77

Rebuild itdocker build -t d4dapp docker/

Page 78: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 78

Rebuild the imagedocker build -t d4dapp docker/

Page 79: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 79

Rebuild the container$ docker rm -f d4dapp$ docker run -d --name d4dapp \ -v C:\drago\Projects\dockerfordevs-app:/var/www/ \ -p 8080:80 d4dapp

Page 80: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 80

Progress!

Page 81: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 81

Docker Compose

Page 82: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 82

What is Docker Compose?• Multi-container orchestration• A single config file holds all of your container info• Works with Docker Swarm and a few other tools, like Rancher

Page 83: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 83

Sample docker-compose.ymlversion: '2'

volumes: mysqldata: driver: local

services: d4dapp: build: ./docker/ volumes: - ./:/var/www/ ports: - 8080:80

mysqlserver: image: mysql environment: MYSQL_DATABASE: dockerfordevs MYSQL_ROOT_PASSWORD: 's3curep@assword' volumes: - mysqldata:/var/lib/mysql

Page 84: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 84

No longer use docker run$ docker rm –f d4dapp$ docker-compose up -d

Page 85: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 85

Now we have 2 containers

Page 86: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 86

Config for DB now points to the service name<?php

return [ 'debug' => true,

'config_cache_enabled' => false,

'db' => [ 'driver' => 'Pdo_Mysql', 'hostname' => 'mysqlserver', 'port' => '3306', 'database' => 'dockerfordevs', 'user' => 'root', 'password' => 's3curep@assword', ],];

Page 87: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 87

Yay!

Page 88: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 88

Install our DB Migration Softwaredocker run --rm \ -v c:/Users/drago/.composer:/root/.composer \ -v c:/Users/drago/Projects/workshop:/app \ -v c:/Users/drago/.ssh:/root/.ssh \ composer/composer \ require robmorgan/phinx

Page 89: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 89

Set up phinxdocker run --rm \ -v C:\Users\drago\Projects\dockerfordevs-app\:/app \ -w /app \ php:cli php vendor/bin/phinx init

Page 90: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 90

Run the migrationdocker run --rm \ -v C:\Users\drago\Projects\dockerfordevs-app\:/app \ -w /app \ --network dockerfordevsapp_default \ php:cli php vendor/bin/phinx migrate

Page 91: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 91

Oops

Page 92: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 92

Let’s use the existing containerdocker run --rm \ -v C:\Users\drago\Projects\dockerfordevs-app\:/app \ -w /app \ --network dockerfordevsapp_default \ dockerfordevsapp_d4dapp php vendor/bin/phinx migrate

Page 93: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 93

Good…

Page 94: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 94

It Lives!

Page 95: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 95

Other Tools

Page 96: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 96

Docker Ecosystem• Docker Machine• Docker Swarm

Page 97: Docker for Developers - Sunshine PHP

Sunshine PHP 2017 97

Thank You!• Software Engineer for InQuest• Author of “Docker for Developers”• https://leanpub.com/dockerfordevs

• Co-Host of “Jerks Talk Games”• http://jerkstalkgames

• http://ctankersley.com• [email protected]• @dragonmantank