@muktaa
Baking Docker using Chef
Mukta Aphale
Agile India Conf 2016, Bangalore
+
@muktaa
Who am I?
• C, Ruby, Java developer turned into DevOps Architect
• Contributed to Chef development
• Chef azure extension
• Knife plugins: knife-azure, knife-ec2, knife-openstack
• Knife WinRM, knife windows listener
• Worked extensively with Docker
• Docker authorized consultant
• Technology, innovation and the thirst to keep learning are what
define me
• Love to travel, read, write
• Above all, I am a mother to two boys!
@muktaa
Agenda
•The Chef Journey
•Container Era
•Chef + Docker
•Example
•Docker cookbook
•Example
@muktaa
The Chef Journey
@muktaa
Chef Journey
• Adam Jacob, Jessie Robbins, Barry Steinglass, Nathan Haneysmith,
Joshua Timberman
• Marionette
• Opscode
• First release: Jan 2009
• Ruby, Erlang
• Facebook, Nordstorm, Disney, GE
• Configuration Management
• Cloud Management
• Chef Delivery
@muktaa
Container Era
@muktaa
Evolution
• 2000: Jails, FreeBSD
• 2001: Linux-Vserver
• 2006: cgroups
• 2008: LXC Containers
• 2013: Docker
• June 2014: Docker 1.0
• Today: Docker 1.10.3
@muktaa
Docker
• Learning curve
• No need for huge investment at the early stage
• “I wont use Chef for that small deployment”
• “Now I have 100 servers. Makes sense to use Chef”
• “Now I have 100 containers. How do I manage them?”
@muktaa
Docker
A Quick Introduction
@muktaa
What is Docker?
Linux	
  Container
3	
  Components:
Docker Engine
Docker Hub
Docker Images
Benefits:
Speed
Portability
Density
Open	
  Source
“Can	
  create	
  lightweight,	
  self	
  
sufficient	
  containers	
  from	
  
any	
  application”
@muktaa
Docker is not a VM
Virtual Machine Docker
@muktaa
FROM	
  ubuntu:14.04
RUN	
  apt-­‐get	
  update
RUN	
  apt-­‐get	
  install	
  libfuse-­‐
dev
ADD	
  dev.conf/etc/myapp-­‐
config/
Dockerfiles
• Codify your configuration
• Set of bash commands
• Example:
• HelloScala
• Dockerfile
• dev.conf
• Docker build HelloScala
@muktaa
Use Cases of Docker
•Microservices
•Lightweight Testing
•Production
•CaaS
•PaaS
@muktaa
Chef and Docker
@muktaa
Config Management Vs Golden Images
•Control the environment Vs System Image /
Runtime image
•Tradeoff between flexibility and manageability
•CM is the vein of DevOps
•Shell scripts -> Chef
•Immutable Infrastructure
@muktaa
Chef and Docker
Replaces	
  Human	
  Tasks,
Idempotence,
Thick	
  client	
  -­‐ thin	
  servers,
Order	
  Matters,
Huge	
  Community	
  Support
An	
  improved	
  Robot,
Fast,
Easy,
Relatively	
  new	
  in	
  the	
  
market!
@muktaa
Simple CD Pipeline
Because simple things can bring the most happiness!
@muktaa
Simple CI/CD Pipeline
•git	
  push
•Triggers	
  
Build
Code
•Build	
   tools	
  
have	
  docker	
  
support
•Build	
   tools	
  
generate	
  a	
  
docker	
  image
Build	
  
Process
Save	
  image
Docker	
  
Image
Unique	
   tag
Docker	
  
Registry
•docker	
  pull	
  
•docker	
  stop
•docker	
  run
Deploy	
  
using	
  knife-­‐
ssh or	
  Push	
  
Jobs
CI	
  Server
@muktaa
The Simple Steps
• git push to https://github.com/muktaa/hello-nodejs
• Triggers a build on your CI server
• npm install, npm test
• docker push muktaa/hello-nodejs
• knifessh 'role:test''deploy.sh' -x ssh-user -i ssh-key-c knife.rb
• Some build tools offer docker integration
• Eg: Maven has docker-maven-plugin
• https://github.com/spotify/docker-maven-plugin
• mvn clean package docker:build -DpushImage
@muktaa
Example
• Git clone https://github.com/muktaa/hello-nodejs/
• <make changes>
• Git add, commit, push
• Jenkins Job runs
• Check image uploaded to docker hub
• Knife-ssh
• URL: http://54.218.32.234:49160/
@muktaa
When Reality Strikes…
If only applications were Hello World programs!
@muktaa
Docker Image
Application Configuration Docker Image
@muktaa
What is Configuration?
Packages Custom	
  SetupsCredentials
Softwares Database
Files
Environment	
  
Specific	
  
Configuration
Ports
@muktaa
ENVIRONMENTS
DEV
Docker
Container
Docker
Container
Docker
Container
PRE	
  
PROD
Docker
Container
Docker
Container
Docker
Container
PROD
Docker
Container
Docker
Container
Docker
Container
@muktaa
Secure Credential Management
•Credentials inside docker containers
•Hard codes
•Set environment variables
•Docker-compose.yml
env_file:
- .env
SOME_USERNAME=myUser
SOME_PWD_VAR=myPwd
@muktaa
Provisioning Machines
• Docker engine
• Ports
• Security groups
• User access
• Eg:
• Knife ec2 server create
@muktaa
Docker Chef Cookbook
To manage docker images and deployment
@muktaa
Docker Cookbook
• Available in Supermarket:
https://supermarket.chef.io/cookbooks/docker
• Install docker
• Build docker image
• Pull image and run container
• Push docker image to registry
• LWRPs
• Docker_container
• Docker_image
• Docker_registry
• https://github.com/bflad/chef-docker/blob/master/README.md
@muktaa
Credential Management
secret = Chef::EncryptedDataBagItem.load_secret
@docker_cred = Chef::EncryptedDataBagItem.load(
node['docker']['creds']['databag'],
node['docker']['user'],
secret
)
docker_registry ‘https://registry.hub.docker.com/u/muktaa/hello-scala/’ do
email docker_cred['email']
username docker_cred['username']
password docker_cred['password']
end
@muktaa
Docker_image
# Build a dockerimage using docker_image resource
docker_image node['docker']['image'] do
tag node['docker']['image']['tag']
source'/var/docker'
action :build
end
# Push the image to docker registery
docker_image node['docker']['image'] do
action :push
end
# Delete the image from the machine
docker_image node['docker']['image'] do
action :remove
end
@muktaa
Docker_container
# Run Container
docker_container ‘muktaa/hello-scala’
detach true
port ‘8081:8081’, ‘8085:8085’
env ‘ENVIRONMENT=pre-prod’
volume ‘/mnt/docker/docker-storage’
action :run
end
@muktaa
GENERATE DOCKERFILE
# Generate a docker file using template.
template "#{node['docker']['directory']}/Dockerfile" do
source 'dockerfile.erb'
variables image: node['docker']['base']['image']['name'],
maintainer:@docker_cred['maintainer'],
email: docker_cred['email'],
build_cmd:node['docker']['build']['commands'],
entry_point: node['docker']['build']['entry_point']
action :create
end
@muktaa
WORKFLOW
Build	
  
Application
• Save	
  the	
  Artifact
Build	
  Docker
Image
• Docker cookbook	
  would	
  build	
  and	
  save	
  the	
  
docker image	
  to	
  Docker hub	
  or	
  DTR
Deploy
• Docker cookbook	
  runs	
  the	
  
container	
  on	
  the	
  nodes
@muktaa
Docker Ecosystem
•Debugging apps in containers
•Docker networking
•Notifications
•Cluster management
•Orchestration
•Schedulers
•Service Discovery
@muktaa
Thank You!
aMukta@gmail.com

Baking Docker Using Chef