homeblogbuilding puppet based applications inside docker

Building Puppet-Based Applications Inside Docker

Creating a Docker Dockerfile to build an application is pretty easy. But what if you already have a large collection of Puppet modules (or Chef cookbooks) that you want to use to build your applications? We’re going to see how easy it is to make use of those modules inside a Dockerfile.

We’re first going to build an image that has Docker apps with Puppet installed. We’ll also add Tim Sharpe’s very cool Librarian-Puppet to the image. Librarian-Puppet is a bundler for your Puppet modules. You can use it to select and install modules from GitHub or the Puppet Labs Forge.

Let’s create a Dockerfile to build our Puppet image.

This Dockerfile will use an Ubuntu-based image and then install Puppet and Librarian-Puppet via RubyGems.

To build this image we run:

Here we’ve built a new image called jamtur01/puppetbase. We’re going to use this image as the basis for our new application image.

Next we need to create a Puppetfile file which Librarian-Puppet uses to install the required Puppet modules. As our example we’re going to install a basic Nginx server.

The Puppetfile tells Librarian-Puppet to install the puppet-nginx module from GitHub.

Now we need to create another Dockerfile for our application image.

This Dockerfile uses the jamtur01/puppetbase image we’ve just created. It adds our local Puppetfile file to the root of the image and then runs librarian-puppet install to install our required modules (by default into /modules).

We then install Nginx via the puppet-nginx module using the puppet apply command. This runs Puppet locally on the host (i.e. without a client-server Puppet installation).

In this image we’re just installing Nginx itself. We could also install virtual hosts or a web application or anything else that the Nginx module supports.

We can now build our application image like so:

Finally let’s launch a container from it.

We’ve launched a new container with the ID of fd461a1418c6, run it daemonized and told it to open any exposed ports, in our case port 80 that we EXPOSE‘ed in the Dockerfile. Let’s check the container and see what port it has mapped to Nginx.

Now let’s browse to port 49158 to see if Nginx is running.

message showing nginx server is installed

Woot! We’ve got Nginx installed via Puppet. You could easily repeat this process for any Puppet-based (or other CM tool) application or infrastructure.

*This article originally appeared on James Turnbull's blog.

About the author: A former IT executive in the banking industry and author of five technology books, James has been involved in IT Operations for 20 years and is an advocate of open source technology. He joined Puppet Labs in March 2010 as the VP of Operations, was VP of Engineering at Venmo and is currently VP of Services at Docker Inc. We highly recommend that you read his blog and follow him on Twitter.

Learn More