111
Developing and Deploying PHP with Docker Midwest PHP 2015

Developing and Deploying PHP with Docker

Embed Size (px)

Citation preview

Page 1: Developing and Deploying PHP with Docker

Developing and Deploying PHP with Docker

Midwest PHP 2015

Page 2: Developing and Deploying PHP with Docker

Patrick Mizer

SpareFoot - Austin, TXZCE: PHP4, PHP5, ZF

We use Docker at SpareFoot:● Containers for PHP services in dev● CI server builds and deploys containers ● Containers in production for Solr Search and Node.js

apps

Page 3: Developing and Deploying PHP with Docker

First, what I am NOT going to cover

Page 4: Developing and Deploying PHP with Docker

First, what I am NOT going to cover

Docker internals○ LXC Containers

■ Kernel namespacing (pid, mnt, net, user, ipc)■ Control groups (blkio, cpu, devices)

Page 5: Developing and Deploying PHP with Docker

First, what I am NOT going to cover

The Docker - CoreOS Debate○ Rocket vs Docker○ Whether everything running through one daemon is

a security risk○ Whether Docker is a platform or a composable

building block

Page 6: Developing and Deploying PHP with Docker

What I am going to cover

Page 7: Developing and Deploying PHP with Docker

What I am going to cover

● What is Docker?● Why you should care about Docker● How Docker works● Building a Docker container for a PHP

application● Docker tools

○ Compose○ Docker-Machine○ Swarm

● Live demos (time permitting)

Page 8: Developing and Deploying PHP with Docker

What is Docker?

Page 9: Developing and Deploying PHP with Docker

What is Docker?

Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications.

Docker enables apps to be quickly assembled from components and eliminates the friction between development, QA, and production environments.

Page 10: Developing and Deploying PHP with Docker

Oh, so it’s a Virtual Machine?

Page 11: Developing and Deploying PHP with Docker

Oh, so it’s a Virtual Machine?

Page 12: Developing and Deploying PHP with Docker

Physical Server

Host OS

Libs

App

Traditional server model

Page 13: Developing and Deploying PHP with Docker

Physical Server

Host OS

Libs

App

Physical Server

Host OS

Hypervisor

Guest OS

Libs

App

Guest OS

Libs

App

Traditional server model Virtual machine model

Page 14: Developing and Deploying PHP with Docker

Physical Server

Host OS

Hypervisor

Guest OS

Libs

App

Guest OS

Libs

App

Virtual machine model

Page 15: Developing and Deploying PHP with Docker

Physical Server

Host OS

Hypervisor

Guest OS

Libs

App

Guest OS

Libs

App

Physical Server

Host OS

Docker Engine

Libs

App

Libs

App

Virtual machine model Docker model

Page 16: Developing and Deploying PHP with Docker

Physical Server

Host OS

Docker Engine

Libs

App

Libs

App Docker Containers● Smaller than VMs (no O/S)● Isolation of VMs without overhead

The Container

Page 17: Developing and Deploying PHP with Docker

The Container Metaphor

Page 18: Developing and Deploying PHP with Docker

The Container Metaphor

Things to ship

Platformsfor shipped

goods

Page 19: Developing and Deploying PHP with Docker

The Container Metaphor

Things to ship

Platformsfor shipped

goods

Page 20: Developing and Deploying PHP with Docker

The Container Metaphor

Page 21: Developing and Deploying PHP with Docker

The Container Metaphor

Page 22: Developing and Deploying PHP with Docker

The Container Metaphor

Technologies Platforms

Page 23: Developing and Deploying PHP with Docker

The Container Metaphor

Platforms

Page 24: Developing and Deploying PHP with Docker

The Container Metaphor

Page 25: Developing and Deploying PHP with Docker

The Container Metaphor

Page 26: Developing and Deploying PHP with Docker

Got it?

Page 27: Developing and Deploying PHP with Docker

Got it?

Page 28: Developing and Deploying PHP with Docker

You really should!

Page 29: Developing and Deploying PHP with Docker

Why should you care about Docker?

Page 30: Developing and Deploying PHP with Docker

Why you should care

● Portable runtime environment for your code○ Stable dev environment○ Build once, run anywhere*

Page 31: Developing and Deploying PHP with Docker

Why you should care

● Portable runtime environment for your code○ Stable dev environment○ Build once, run anywhere*

● Immutable build artifacts○ Avoid dependency hell○ Avoid “drift”

Page 32: Developing and Deploying PHP with Docker

Why you should care

● Portable runtime environment for your code○ Stable dev environment○ Build once, run anywhere*

■ New technologies!● Immutable build artifacts

○ Avoid dependency hell○ Avoid “drift”

● Clear separation of concerns○ Dev - Code, Dependencies, Libs○ DevOps - Provisioning, Logging, Network

Page 33: Developing and Deploying PHP with Docker

Stable Dev Environment

Virtual Machine

% vagrant up

Page 34: Developing and Deploying PHP with Docker

Stable Dev Environment

Virtual Machine

% vagrant up

Page 35: Developing and Deploying PHP with Docker

But how do you model this?

Presentation

Service

Data

[ JSON / REST API ]

Page 36: Developing and Deploying PHP with Docker

Stable Dev Environment

Virtual Machine

% vagrant up

Page 37: Developing and Deploying PHP with Docker

Stable Dev Environment

% vagrant up

Page 38: Developing and Deploying PHP with Docker

Stable Dev Environment

PHP App 1

Redis Cache

Solr Search

MySQL DB

Virtual Machine

Containers% vagrant up

PHP App 2

Page 39: Developing and Deploying PHP with Docker

Separation of concerns

Page 40: Developing and Deploying PHP with Docker

Developers

Page 41: Developing and Deploying PHP with Docker

Developers

Inside the container:● Code● Libraries● Package Manager● Data

Page 42: Developing and Deploying PHP with Docker

DevOps

Page 43: Developing and Deploying PHP with Docker

DevOps

Outside the container:● Logging● Monitoring● Network Configuration

Page 44: Developing and Deploying PHP with Docker

Immutable Build Artifacts

2.4.10

5.6.5

5.6.23

5.5.21

5.6.23

2.4.12

5.5.21

5.6.3

2.4.09

14.10

14.04

Production

Development

Local

Page 45: Developing and Deploying PHP with Docker

Immutable Build Artifacts

2.4.10

5.6.5

5.6.23

5.5.21

5.6.23

2.4.12

5.5.21

5.6.3

2.4.09

14.10

14.04

Page 46: Developing and Deploying PHP with Docker

Immutable Build Artifacts

2.4.10

5.6.5

5.6.23

5.5.21

5.6.23

2.4.12

5.5.21

5.6.3

2.4.09

14.10

14.04

Page 47: Developing and Deploying PHP with Docker

Immutable Build Artifacts

2.4.10

5.6.5

5.6.23

5.5.21

5.6.23

2.4.12

5.5.21

5.6.3

2.4.09

14.10

14.04

Page 48: Developing and Deploying PHP with Docker

Immutable Build Artifacts

2.4.10

5.6.5

5.6.23

5.5.21

5.6.23

2.4.12

5.5.21

5.6.3

2.4.09

14.10

14.04

Page 49: Developing and Deploying PHP with Docker

Immutable Build Artifacts

2.4.10

5.6.5

5.6.23

5.5.21

5.6.23

2.4.12

5.5.21

5.6.3

2.4.09

14.10

14.04

Page 50: Developing and Deploying PHP with Docker

Immutable Build Artifacts

2.4.10

5.6.5

5.6.23

5.5.21

5.6.23

2.4.12

5.5.21

5.6.3

2.4.09

14.10

14.04

Page 51: Developing and Deploying PHP with Docker

Immutable Build Artifacts

14.10

14.04

Production

Development

Local 2.4.12

5.6.7

2.4.10

5.5.0

2.4.10

5.5.0

Page 52: Developing and Deploying PHP with Docker

Immutable Build Artifacts

14.10

14.04

2.4.12

5.6.7

2.4.10

5.5.0

2.4.10

5.5.0

Page 53: Developing and Deploying PHP with Docker

Immutable Build Artifacts

14.10

14.04

2.4.12

5.6.7

2.4.10

5.5.0

2.4.10

5.5.0

Page 54: Developing and Deploying PHP with Docker

Immutable Build Artifacts

14.10

14.04

2.4.12

5.6.7

2.4.10

5.5.0

2.4.10

5.5.0

2.4.12

5.6.7

Page 55: Developing and Deploying PHP with Docker

Immutable Build Artifacts

14.10

14.04

2.4.12

5.6.7

2.4.10

5.5.0

2.4.10

5.5.0

2.4.12

5.6.7

Page 56: Developing and Deploying PHP with Docker

Immutable Build Artifacts

14.10

14.04

2.4.12

5.6.7

2.4.10

5.5.0

2.4.10

5.5.0

2.4.12

5.6.7

2.4.12

5.6.7

Page 57: Developing and Deploying PHP with Docker

Immutable Build Artifacts

14.10

14.04

2.4.12

5.6.7

2.4.10

5.5.0

2.4.10

5.5.0

2.4.12

5.6.7

2.4.12

5.6.7

Page 58: Developing and Deploying PHP with Docker

Immutable Build Artifacts

14.10

14.04

2.4.12

5.6.7

2.4.10

5.5.0

2.4.10

5.5.0

2.4.12

5.6.7

2.4.12

5.6.7

OpsDev

Page 59: Developing and Deploying PHP with Docker

Let’s build something...

Page 60: Developing and Deploying PHP with Docker

Docker Concepts

Docker Daemon

Container 1

Container 2

Container 3

Container N

Docker CLI

docker psdocker pulldocker rundocker kill...

Docker Host Docker Registry

Image 1

Image 2

Image 3

Image N

Page 61: Developing and Deploying PHP with Docker

Building our first container

% mkdir first

% cd first

% touch index.php

% touch Dockerfile

Page 62: Developing and Deploying PHP with Docker

index.php

<?php

echo “Hello world”;

?>

Page 63: Developing and Deploying PHP with Docker

The Dockerfile

FROM ubuntu:trusty

RUN apt-get update && \

apt-get -y install php5-cli

ADD . /app

WORKDIR /app

EXPOSE 8080

CMD ["php", "-S", "0.0.0.0:8080"]

Page 64: Developing and Deploying PHP with Docker

The Dockerfile

FROM ubuntu:trustyRUN apt-get update && \

apt-get -y install php5-cli

ADD . /app

WORKDIR /app

EXPOSE 8080

CMD ["php", "-S", "0.0.0.0:8080"]

Page 65: Developing and Deploying PHP with Docker

The Dockerfile

FROM ubuntu:trusty

RUN apt-get update && \

apt-get -y install php5-cliADD . /app

WORKDIR /app

EXPOSE 8080

CMD ["php", "-S", "0.0.0.0:8080"]

Page 66: Developing and Deploying PHP with Docker

The Dockerfile

FROM ubuntu:trusty

RUN apt-get update && \

apt-get -y install php5-cli

ADD . /app

WORKDIR /appEXPOSE 8080

CMD ["php", "-S", "0.0.0.0:8080"]

Page 67: Developing and Deploying PHP with Docker

The Dockerfile

FROM ubuntu:trusty

RUN apt-get update && \

apt-get -y install php5-cli

ADD . /app

WORKDIR /app

EXPOSE 8080CMD ["php", "-S", "0.0.0.0:8080"]

Page 68: Developing and Deploying PHP with Docker

The Dockerfile

FROM ubuntu:trusty

RUN apt-get update && \

apt-get -y install php5-cli

ADD . /app

WORKDIR /app

EXPOSE 8080

CMD ["php", "-S", "0.0.0.0:8080"]

The Docker way: 1 process per container

Page 69: Developing and Deploying PHP with Docker

The Dockerfile

FROM ubuntu:trusty

RUN apt-get update && \

apt-get -y install php5-cli

ADD . /app

WORKDIR /app

EXPOSE 8080

CMD ["php", "-S", "0.0.0.0:8080"]

I’m Lazy: /usr/sbin/apache2ctl -D FOREGROUND

Page 70: Developing and Deploying PHP with Docker

Build our image

% docker build -t mizer/helloworld .

Page 71: Developing and Deploying PHP with Docker

Build our image

Docker Daemon

Docker CLI

docker build -t mizer/helloworld .

Docker Host Docker Registry

mizer/helloworld

ubuntu

Page 72: Developing and Deploying PHP with Docker

The Dockerfile

FROM ubuntu:trusty

RUN apt-get update && \

apt-get -y install php5-cli

ADD . /app

WORKDIR /app

EXPOSE 8080

CMD ["php", "-S", "0.0.0.0:8080"]

step 0

step 1

step 2

step 4

step 5

step 3

Each step is cached :)

Page 73: Developing and Deploying PHP with Docker

Run our first container

% sudo docker run -p 8080:8080

--name hello_1 -d mizer/helloworld

Page 74: Developing and Deploying PHP with Docker

Run our first container

Docker Daemon

Docker CLI

docker run -p 8080:8080 --name hello_1 -d mizer/helloworld

Docker Host Docker Registry

mizer/helloworld

hello_1

Page 75: Developing and Deploying PHP with Docker

Check our running containers

% docker ps

Page 76: Developing and Deploying PHP with Docker

Check our running containers

% docker ps

CONTAINER ID IMAGE COMMAND PORTS NAMES

5f1b82bb90b8 mizer/helloworld:latest "php -S 0.0.0.0:8080 8080->8080/tcp hello_1

Page 77: Developing and Deploying PHP with Docker

Create another

% docker run -p 8181:8080

--name hello_2 -d mizer/helloworld

Page 78: Developing and Deploying PHP with Docker

Docker Concepts

Docker Daemon

Docker CLI

docker run -p 8181:8080 --name hello_2 -d mizer/helloworld

Docker Host Docker Registry

mizer/helloworld

hello_1

hello_2

Page 79: Developing and Deploying PHP with Docker

Check our running containers

% docker ps

CONTAINER ID IMAGE COMMAND PORTS NAMES

5f1b82bb90b8 mizer/helloworld:latest "php -S 0.0.0.0:8080 8080->8080/tcp hello_1

3d1b8ccb80c7 mizer/helloworld:latest "php -S 0.0.0.0:8080 8181->8080/tcp hello_2

Page 80: Developing and Deploying PHP with Docker

Let’s remove both containers

% sudo docker rm -f hello_1 hello_2

Page 81: Developing and Deploying PHP with Docker

Let’s remove both containers

Docker Daemon

Docker CLI

docker rm -f hello_1 hello_2

Docker Host Docker Registry

mizer/helloworld

Page 82: Developing and Deploying PHP with Docker

… and the image

% sudo docker rmi -f mizer/helloworld

Page 83: Developing and Deploying PHP with Docker

… and the image

Docker Daemon

Docker CLI

docker run -p 8080:8080 --name hello_2 -d mizer/hello

Docker Host Docker Registry

Page 84: Developing and Deploying PHP with Docker

Creating a base image

FROM ubuntu:trusty

RUN apt-get update && \

apt-get -y install php5-cli

% docker build -t mizer/php

Page 85: Developing and Deploying PHP with Docker

Creating a base image

Docker Daemon

Docker CLI

docker build -t mizer/php .

Docker Host Docker Registry

mizer/php

Page 86: Developing and Deploying PHP with Docker

Using our base image

FROM mizer/php:latest

ADD . /app

WORKDIR /app

EXPOSE 8080

CMD ["php", "-S", "0.0.0.0:8080"]

% sudo docker build -t mizer/helloworld .

Page 87: Developing and Deploying PHP with Docker

Creating a base image

Docker Daemon

Docker CLI

docker build -t mizer/php .

Docker Host Docker Registry

mizer/php

mizer/helloworld

Page 88: Developing and Deploying PHP with Docker

Create the container

% sudo docker run -p 8080:8080

--name hello_1 -d mizer/helloworld

Page 89: Developing and Deploying PHP with Docker

Create the container

Docker Daemon

Docker CLI

docker run -p 8080:8080 --name hello_1 -d mizer/hello

Docker Host Docker Registry

mizer/php

mizer/hellohello_1

Page 90: Developing and Deploying PHP with Docker

Community images

% docker search php

Page 91: Developing and Deploying PHP with Docker

Community images

% docker search phpNAME DESCRIPTION STARS OFFICIAL AUTOMATED

php While designed for web development, the PH... 148 [OK]

tutum/apache-php Apache+PHP base image - listens in port 80... 59 [OK]

maxexcloo/nginx-php Docker framework container with Nginx and ... 30 [OK]

eboraas/apache-php PHP5 on Apache (with SSL support), built o... 17 [OK]

richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 10 [OK]

php-zendserver Zend Server - the integrated PHP applicati... 10 [OK]

clue/phpvirtualbox phpVirtualBox is a modern web interface de... 9 [OK]

jdeathe/centos-ssh-apache-php CentOS-6 6.5 x86_64 / Apache / PHP / PHP m... 8 [OK]

jprjr/php-fpm Arch Linux-based image with PHP-FPM instal... 8 [OK]

darh/php-essentials Provides a container full of php-cli tools... 7 [OK]

million12/nginx-php Nginx + PHP-FPM, CentOS-7 based. 5 [OK]

reinblau/php-apache2 Apache2 Server for PHP Projects 4 [OK]

eriksencosta/php-dev Docker image with latest supported PHP ver... 4 [OK]

alreece45/phusion-nginx-php nginx and php-fpm with configurable docume... 3 [OK]

million12/php-app-ssh million12/php-app, extended with SSHD and ... 2 [OK]

simpledrupalcloud/php 1 [OK]

yoshz/apache-php-dev A docker image with Apache, PHP 5.5, drush... 1 [OK]

azukiapp/php-apache Base docker image to run PHP applications ... 1 [OK]

Page 92: Developing and Deploying PHP with Docker

A modern architecture

Page 93: Developing and Deploying PHP with Docker

A modern architecture

Presentation

Service

Data

[ JSON / REST API ]

Page 94: Developing and Deploying PHP with Docker

A modern architecture

Docker Daemon

Docker CLIDocker Host Docker Registry

mizer/php

mizer/webweb_1

service_1

data_1

mizer/service

mizer/data

mizer/mysql

mizer/data

Page 95: Developing and Deploying PHP with Docker

Got it?



Page 96: Developing and Deploying PHP with Docker

Docker Orchestration

● Compose● Machine● Swarm

Page 97: Developing and Deploying PHP with Docker

South Africa

Page 98: Developing and Deploying PHP with Docker

Fig Docker Compose

Compose is a tool for defining and running complex applications with Docker. With Compose, you define a multi-container application in a single file, then spin your application up in a single command which does everything that needs to be done to get it running.

Page 99: Developing and Deploying PHP with Docker

docker-compose.ymlwebapp1:

build: webapp/.

command: php -S 0.0.0.0:8000 -t /code

ports:

- "8000:8000"

environment:

- DSN=mysql:host=mysql;dbname=test

- USERNAME=test

- PASSWORD=foo

links:

- mysql

Page 100: Developing and Deploying PHP with Docker

Docker Compose

% docker-compose up

Page 101: Developing and Deploying PHP with Docker

Docker-compose

Docker Daemon

Docker CLI

Docker Host Docker Registry

mizer/php

mizer/webweb_1

service_1

data_1

mizer/service

mizer/data

mizer/mysql

mizer/data

docker-compose.yml

% docker-compose up

Page 102: Developing and Deploying PHP with Docker

Docker Machine

Docker Machine takes you from zero-to-Docker in seconds with a single command.

Before Docker Machine, a developer would need to log in to the host and follow installation and configuration instructions specifically for that host and its OS. With Docker Machine, whether provisioning the Docker daemon on a new laptop, on virtual machines in the data center, or on a public cloud instance, the same, single command.

Page 103: Developing and Deploying PHP with Docker

Swarm

Docker CLI

Docker Daemon

Container 1

Container 2

Container 3

Docker Host

Docker Daemon

Container 1

Container 2

Container 3

Docker Host

Docker Daemon

Container 1

Container 2

Container 3

Docker Host

Docker-machine

Page 104: Developing and Deploying PHP with Docker

Docker Swarm

Docker Swarm is native clustering for Docker. It turns a pool of Docker hosts into a single, virtual host.

Swarm serves the standard Docker API, so any tool which already communicates with a Docker daemon can use Swarm to transparently scale to multiple hosts: Dokku, Compose, Krane, Flynn, Deis, DockerUI, Shipyard, Drone, Jenkins... and, of course, the Docker client itself.

Page 105: Developing and Deploying PHP with Docker

“Cows not pets”

Page 106: Developing and Deploying PHP with Docker

“Cows not pets”

Pets● Pets have names● They are unique, loving, hand

raised and cared for● When they are sick, they are

taken to the vet.

Page 107: Developing and Deploying PHP with Docker

“Cows not pets”

Pets● Pets have names● They are unique, loving, hand

raised and cared for● When they are sick, they are

taken to the vet.

Cows● Cows have numbers● They are not unique, they are a

commodity.● When they are sick....?

Page 108: Developing and Deploying PHP with Docker

“Cows not pets”

Pets● Pets have names● They are unique, loving, hand

raised and cared for● When they are sick, they are

taken to the vet.

Cows● Cows have numbers● They are not unique, they are a

commodity.● When they are sick....?

Page 109: Developing and Deploying PHP with Docker

Swarm

Docker CLI

Docker Daemon

Container 1

Container 2

Container 3

Docker Host

Docker Daemon

Container 1

Container 2

Container 3

Docker Host

Docker Daemon

Container 1

Container 2

Container 3

Docker Host

Swarm

Page 110: Developing and Deploying PHP with Docker

Live demo...

Page 111: Developing and Deploying PHP with Docker

Thank you!

[email protected]

Github: https://github.com/maximizer● I put all of the examples in

docker-midwestphp15

https://joind.in/13076