From Docker to Production - ZendCon 2016

Chris Tankersley
Chris TankersleyPHP Programmer at Home
From Docker to
Production
Chris Tankersley
@dragonmantank
ZendCon 2016, October 2016
ZendCon, October 2016 1
ZendCon, October 2016 2
Subliminal Advertisement
ZendCon, October 2016 3
ZendCon, October 2016 4
ZendCon, October 2016 5
ZendCon, October 2016 6
ZendCon, October 2016 7
ZendCon, October 2016 8
ZendCon, October 2016 9
Considerations
ZendCon, October 2016 10
How does your app handle storage?
ZendCon, October 2016 11
What does your build process look like?
ZendCon, October 2016 12
How well does your app handle scaling?
ZendCon, October 2016 13
Does it make sense?
ZendCon, October 2016 14
The Build Process
ZendCon, October 2016 15
Start Small
• Build your application
• Run composer
• Run npm/bower
• Build JS/CSS
• Use the compiled output to build an image with docker build
• Push full image to private registry
ZendCon, October 2016 16
docker build
• Additional options to look at
• -f, --file – Specify a different filename for the Dockerfile
• --no-cache – Don’t use a cached layer
• --pull – Always pull a new version of the image
ZendCon, October 2016 17
Sample usage
docker build 
--no-cache 
–f docker/php/phpserver.dockerfile 
–t prod_php /opt/builds/20161010
ZendCon, October 2016 18
phpserver.dockerfile
FROM php:fpm
RUN docker-php-ext-install pdo pdo_mysql
COPY ./ /var/www
ZendCon, October 2016 19
Docker Compose
ZendCon, October 2016 20
Very Good for Small Deployments
• Can be used to augment your dev environment
• Works well with Docker Machine
ZendCon, October 2016 21
Create a machine
docker-machine create --driver digitalocean 
--digital-ocean-access-token [token] 
zendcon2016
ZendCon, October 2016 22
ZendCon, October 2016 23
Switch to the remote node
• Run docker-machine env zendcon2016
& "C:Program
FilesDockerDockerResourcesbindocker-
machine.exe" env zendcon2016 | Invoke-Expression
ZendCon, October 2016 24
Set up docker-compose
• Docker Compose allows multiple config files with -f
• Have a base docker-compose.yml for Production
• Add a secondary one for Development
ZendCon, October 2016 25
version: '2'
volumes:
mysqldata:
driver: local
services:
nginx:
build:
context: ./
dockerfile: ./nginx.dockerfile
ports:
- 80:80
- 443:443
phpserver:
build:
context: ./
dockerfile: ./phpserver.dockerfile
working_dir: /var/www/public
mysqlserver:
image: mysql
environment:
[redacted]
volumes:
- mysqldata:/var/lib/mysql
ZendCon, October 2016 26
docker-compose.yml
version: '2'
volumes:
mysqldata:
driver: local
services:
nginx:
image: nginx
volumes:
- ./output_dev:/var/www/public:ro
- ./app/nginx/default.conf:/etc/nginx/conf.d/default.conf
- ./ssl:/etc/nginx/ssl/
phpserver:
build:
context: ./
dockerfile: ./phpserver.dockerfile
working_dir: /var/www/public
volumes:
- ./app:/var/www/
- ./vendor:/var/www/vendor
mysqlserver:
image: mysql
environment:
[redacted]
volumes:
- mysqldata:/var/lib/mysql
ZendCon, October 2016 27
docker-compose.dev.yml
When doing development
docker-compose 
–f docker-compose.yml 
–f docker-compose.dev.yml 
up -d
ZendCon, October 2016 28
When doing a deployment
docker-compose up -d
ZendCon, October 2016 29
Other Alternative – Variable Substitution
• Docker Compose allows variable substitution inside the file
• Wrap variables in ${}
• image: ${DEPLOY_VERSION}_php
ZendCon, October 2016 30
When doing a deployment
docker build –f […] –t 20161010_php /opt/builds/20161010
DEPLOY_VERSION=20161010 docker-compose up -d
ZendCon, October 2016 31
Rancher
ZendCon, October 2016 32
Rancher and RancherOS
Daycamp 4 Developers - Ops for Devs 33
Manages your Containers
Daycamp 4 Developers - Ops for Devs 34
Manages your Hosts
Daycamp 4 Developers - Ops for Devs 35
Allows you to monitor containers
Daycamp 4 Developers - Ops for Devs 36
Allows you to manage your applications
Daycamp 4 Developers - Ops for Devs 37
Allows you to deploy your applications
Daycamp 4 Developers - Ops for Devs 38
Allows you to deploy your applications
Daycamp 4 Developers - Ops for Devs 39
Supports Docker Compose
Daycamp 4 Developers - Ops for Devs 40
Blue-Green Deployments
Daycamp 4 Developers - Ops for Devs 41
Router
App v1 App v2
Blue-Green Deployments
Daycamp 4 Developers - Ops for Devs 42
Router
App v3 App v2
Blue-Green Deployments
Daycamp 4 Developers - Ops for Devs 43
Router
App v3 App v2
Add rancher/server to the master
Daycamp 4 Developers - Ops for Devs 44
docker run -d 
--restart=always 
-p 8080:8080 
–name=rancher 
rancher/server
Add rancher/agent to nodes
Daycamp 4 Developers - Ops for Devs 45
docker run -d 
--privileged 
-v /var/run/docker.sock:/var/run/docker.sock 
rancher/agent:v0.7.9 
http://192.168.99.100:8080/v1/scripts/[hash]
All Done!
Daycamp 4 Developers - Ops for Devs 46
Deploying with Rancher CLI
Daycamp 4 Developers - Ops for Devs 47
What is it?
• Rancher has an API!
• Small executable that interacts with Rancher API
• Kind of like a custom docker-compose
Daycamp 4 Developers - Ops for Devs 48
Get an API Key
Daycamp 4 Developers - Ops for Devs 49
Get an API Key
Daycamp 4 Developers - Ops for Devs 50
Download Rancher CLI
Daycamp 4 Developers - Ops for Devs 51
Export Stack Config
Daycamp 4 Developers - Ops for Devs 52
Two Config Files
Daycamp 4 Developers - Ops for Devs 53
docker-compose.yml
Daycamp 4 Developers - Ops for Devs 54
Deploy Script
Daycamp 4 Developers - Ops for Devs 55
ACCESS_KEY="C4F407CE1D8C59EB53BE"
SECRET="daNENHR241Jzm5Z9iw6VsujD9hWfjHWrDzkKmKiA"
RANCHER_URL="http://192.168.99.100:8080"
./rancher-compose --secret-key=${SECRET} --access-key=${ACCESS_KEY} --url=${RANCHER_URL} --
file=docker-compose.yml --rancher-file=rancher-compose.yml -p phptest up --upgrade -d
Edit and Deploy
Daycamp 4 Developers - Ops for Devs 56
Edit and Deploy
Daycamp 4 Developers - Ops for Devs 57
Finish Upgrade
Daycamp 4 Developers - Ops for Devs 58
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
• chris@ctankersley.com
• @dragonmantank
ZendCon, October 2016 59
1 of 59

More Related Content

Similar to From Docker to Production - ZendCon 2016(20)

More from Chris Tankersley(20)

Docker is Dead: Long Live ContainersDocker is Dead: Long Live Containers
Docker is Dead: Long Live Containers
Chris Tankersley53 views
Bend time to your will with gitBend time to your will with git
Bend time to your will with git
Chris Tankersley194 views
Dead Simple APIs with OpenAPIDead Simple APIs with OpenAPI
Dead Simple APIs with OpenAPI
Chris Tankersley311 views
You Got Async in my PHP!You Got Async in my PHP!
You Got Async in my PHP!
Chris Tankersley164 views
Docker for Developers - PHP Detroit 2018Docker for Developers - PHP Detroit 2018
Docker for Developers - PHP Detroit 2018
Chris Tankersley865 views
Docker for DevelopersDocker for Developers
Docker for Developers
Chris Tankersley934 views
They are Watching YouThey are Watching You
They are Watching You
Chris Tankersley286 views
BASHing at the CLI - Midwest PHP 2018BASHing at the CLI - Midwest PHP 2018
BASHing at the CLI - Midwest PHP 2018
Chris Tankersley363 views
You Were Lied To About OptimizationYou Were Lied To About Optimization
You Were Lied To About Optimization
Chris Tankersley373 views
A Brief History of Open SourceA Brief History of Open Source
A Brief History of Open Source
Chris Tankersley294 views
Failing at Scale - PNWPHP 2016Failing at Scale - PNWPHP 2016
Failing at Scale - PNWPHP 2016
Chris Tankersley638 views
Deploying Containers with RancherDeploying Containers with Rancher
Deploying Containers with Rancher
Chris Tankersley2.4K views
WTF Is Rancher?WTF Is Rancher?
WTF Is Rancher?
Chris Tankersley1.1K views

Recently uploaded(20)

ChatGPT and AI for Web DevelopersChatGPT and AI for Web Developers
ChatGPT and AI for Web Developers
Maximiliano Firtman143 views
Green Leaf Consulting: Capabilities DeckGreen Leaf Consulting: Capabilities Deck
Green Leaf Consulting: Capabilities Deck
GreenLeafConsulting147 views
Java Platform Approach 1.0 - Picnic MeetupJava Platform Approach 1.0 - Picnic Meetup
Java Platform Approach 1.0 - Picnic Meetup
Rick Ossendrijver20 views
METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...
METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...
Prity Khastgir IPR Strategic India Patent Attorney Amplify Innovation22 views
ISWC2023-McGuinnessTWC16x9FinalShort.pdfISWC2023-McGuinnessTWC16x9FinalShort.pdf
ISWC2023-McGuinnessTWC16x9FinalShort.pdf
Deborah McGuinness80 views

From Docker to Production - ZendCon 2016