I recently mouthed off about the benefit of using Docker and Packer together, so I thought I would actually show a quick proof of concept on how this would all work. This is a first step and not an end to end continuous delivery setup with Packer and Docker, but it is a good first step on the journey.
TL;DR section, just look at the cookbook on github
So these will be the project pieces
- Ubuntu 14.04
- Docker
- Packer
- Packer Template files
PROBLEM: If this is a simple POC, why aren't you running it on your own
mac?
SOLUTION: It's a not easy on a mac, and a POC is supposed to run easily.
People more intimately familiar with Docker know it's designed to run natively on linux, but not MacOSX (what most devs/ops use). Therefore, Docker built a proxyVM called boot2docker for Macs that allows you to boot up containers locally. This works for running base containers with Dockerfiles and your terminal, but doesn't play nice with Packer currently. So we simply set up a local linux VM with Vagrant, and configure it with Chef.
the Organization for the cookbooks is pretty straightforward.
- Role Cookbook for holding application cookbooks and custom data
- docker cookbook (community)
- df_packer cookbook (code by DevOps Jeff)
- POC box specific configuration (in role cookbook)
This is what the run_list looks like for the base installation
include_recipe "df_packer::default"
include_recipe "docker"
(Don't forget to include your dependencies in your Berksfile and metadata.rb)
IMPORTANT: There is a bug with the latest version of Docker working with Packer, so you will need to use a slightly older version of Docker. This can be done by setting an override attribute in the role cookbook
override['docker']['version'] = "1.3.3"
Once you get get packer and docker built into your run_list (example), lets set up a directory for
the packer templates and create a template.json.
directory "/home/vagrant/packer_test" do recursive true action :create end template "/home/vagrant/packer_test/template.json" do source "template.json.erb" user "vagrant" mode "0777" action :create end
The core piece to getting packer to work is using template files . Written in JSON, I think that this is an incredibly easy to read format for defining your builds. The most important being the builder block. As you would have guessed, It is the block where you describe that you want a docker image made. There are other pieces to consider, but for now this is all we need to know.
{ "builders": [ { "type": "docker", "image": "tutum/tomcat", "export_path": "image.tar" }] }
This template specifies
- Docker is our builder
- we are using the tutum/tomcat image as a base, which is found on the community Dockerhub registry
- Once it is built we are going to wrap the image in a tarball file for exporting.
In order for it to run you just need to run <packer build template.json> in the directory where the template is installed, and Packer will take care of the rest.
So there you have it. You have used Packer to build a Docker image. This is as basic as it gets. For those of use who have to do more advanced configuration, we will work on that in part two.