Mikyjpeg
Mikyjpeg

Reputation: 1219

How to include docker cookbook in chef?

I'm trying to define a chef recipe to install docker using the cookbook provided at this link: https://supermarket.chef.io/cookbooks/docker#readme

I'm using Berkshelf so I put this line on Berkfile

cookbook 'docker', '~> 2.9.6'

and this on the metadata.db

depends "docker"

My recipe is the following

include_recipe 'docker::docker_service'
include_recipe 'docker::docker_image'
include_recipe 'docker::docker_container'

docker_service 'default' do
        action [:create, :start]
end

but when I try to run the kitchen I obtain the following error:

ERROR: could not find recipe docker_service for cookbook docker

but if I look in my berkshelf repository the recipe is there:

$ ls ~/.berkshelf/cookbooks/docker-2.9.6/libraries/ | grep docker_service.rb
docker_service.rb

What am I doing wrong?

Thanks, Michele.

Upvotes: 1

Views: 2645

Answers (3)

Mark O'Connor
Mark O'Connor

Reputation: 77951

As stated by @coderanger and @NilsLandt you need to write your own cookbook to install docker and just call the resources provided by the docker cookbook.

Example

Sample cookbook generated by the "chef generate cookbook" command

├── Berksfile
├── chefignore
├── metadata.rb
├── README.md
├── recipes
│   └── default.rb
└── test
    └── integration
        ├── default
        │   └── serverspec
        │       └── default_spec.rb
        └── helpers
            └── serverspec
                └── spec_helper.rb

metadata.rb

Add the "apt" and "docker" cookbooks as dependencies

name 'docker_demo'
maintainer 'Mark O''Connor'
maintainer_email '[email protected]'
license 'all_rights'
description 'Installs/Configures docker_demo'
long_description 'Installs/Configures docker_demo'
version '0.1.0'

depends 'apt'
depends 'docker'

recipes/default.rb

Just use the "docker_service" resource to create a docker service. The "create" action will install Docker.

#
# Cookbook Name:: docker_demo
# Recipe:: default
#
# Copyright (c) 2016 The Authors, All Rights Reserved.

include_recipe 'apt'

docker_service 'default' do
  action [:create, :start]
end

test/integration/default/serverspec/default_spec.rb

A set of tests to verify the installation of docker

require 'spec_helper'

describe file('/usr/bin/docker') do
  it { should be_file }
  it { should be_executable }
end

describe command('/usr/bin/docker version') do
  its(:stdout) { should match /Version:      1.12.1/ }
end

Upvotes: 4

coderanger
coderanger

Reputation: 54181

docker_service is a resource, not a recipe. See the readme for some example usage, but the overall idea is you need to write your own wrapper cookbook which uses the custom resources provided.

Upvotes: 1

Nils Landt
Nils Landt

Reputation: 3134

The docker cookbook does not provide any recipes, which is why your include_recipe calls fail.

Instead, you just use the resources, like you already have with the docker_service resource.

Your recipe should run when you delete your three include_recipe lines.

Upvotes: 3

Related Questions