Chef is a great configuration management tool. It allows managing environments, supports multiple platforms, handles updates, but takes a lot of time to deploy software from scratch. Chef is not like Docker, which allows spinning up containers in seconds. If you are already using Chef, then you might not have time to rewrite all the Cookbooks into Dockerfiles. In this post, I’m showing how to build Docker images quickly using existing Chef Cookbooks.
The obvious way to build a Docker image using Chef is to create a container with SSH running and use knife to run chef-solo inside. I was looking for existing solution that already does that. I found that there is a Docker driver for Kitchen. The default driver for Kitchen is Vagrant which sets up a VM and deploys a cookbook. Using Docker driver a container is used instead, which you can convert to an image and push to the registry afterwards.
I use nginx cookbook as an example (I know that there already is a better docker image for nginx). Following snippet clones the nginx cookbook, installs dependencies and deploys the cookbook into a docker container using Kitchen.
After that you should have a Docker container up and running with nginx cookbook deployed! Now you can commit the container and build an image.
Use following commands to run the image and test it.
If you have already .kitchen.yml then you just need to change the driver to docker. For more configuration options see kitchen-docker. This is the example configuration used by nginx cookbook.
I think this is a seamless way to migrate from Chef to Docker (at least for developers). You can use most of the Docker advantages without getting rid of existing Chef cookbooks.