At the time of writing there is not a lot of information about how to set-up Chef with Docker, so I decided to write a short tutorial. The code below shows how to use chef with Docker. This tutorial assumes you have chef and docker installed. The user executing should also be included the Docker group.

First, install chef container:

$ chef gem install knife-container

Use knife to initialize a docker container:

$ knife container docker init example1 -r 'recipe[my_cookbook]' -z
Downloading base image: chef/ubuntu-12.04:latest. This process may take awhile...
Tagging base image chef/ubuntu-12.04 as example1

Context Created: /home/ward/cookbooks/dockerfiles/example1

Note: Replace my_cookbook with your own cookbook

Build the new Docker container:

$ knife container docker build example1
Successfully built 39cca130d1f8

Run the new Docker container:

$ docker run --name example1_instance1 --publish= -i -t example1
Chef Client finished, 1/4 resources updated in 11.100677039 seconds

This command also exposes port 80 of the Docker container to the host system.

That's it

That was it, you just installed knife-container, initialized a Dockerfile and ran the container using knife. Any feedback using the comments below is appreciated!

About the Author

Ward has been a system administrator for more than a decade and has been working as a Consultant and Trainer for the last few years. Besides DevOps he is also into the latest Big Data technologies. Originally Belgian, currently enjoying life in London.