[Docker]: Provisioning docker container with chef-solo

Provisioning docker image with chef using chef-solo

Building basic Dockerfile

FROM ubuntu
RUN echo "Hello World"

Adding chef-solo

Dockerfile

FROM ubuntu
RUN apt-get -y update
RUN apt-get -y install curl build-essential libxml2-dev libxslt-dev git
RUN curl -L https://www.opscode.com/chef/install.sh | bash
RUN echo "gem: --no-ri --no-rdoc" > ~/.gemrc
RUN /opt/chef/embedded/bin/gem install berkshelf

Chef-solo

Chef-solo requires solo.rb and solo.json.
These files will be created in the same directory:

|_ Dockerfile (created above)
|_ solo.rb
|_ solo.json
|_ Berksfile

solo.rb

# Solo.rb
root = File.absolute_path(File.dirname(__FILE__))
file_cache_path root
cookbook_path root + '/cookbooks'

Berksfile

#Berksfile
site :opscode
cookbook 'build-essential'
cookbook 'git'
cookbook 'nginx' 

solo.json

# solo.json
{
    "run_list": [
    "recipe[nginx::default]"
    ]
}

Extenidng Dockerfile to run the recipes

So far the Dockerfile installed chef client.
The added script will run the cookbooks defined in the solo.json file.

FROM ubuntu:14.04
RUN apt-get -y update
RUN apt-get -y install curl build-essential libxml2-dev libxslt-dev git
RUN curl -L https://www.opscode.com/chef/install.sh | bash
RUN echo "gem: --no-ri --no-rdoc" > ~/.gemrc
RUN /opt/chef/embedded/bin/gem install berkshelf

# -------- added part ---------- 

RUN apt-get -y update
RUN apt-get -y install python-software-properties
RUN apt-get -y update

ADD ./Berksfile /Berksfile
ADD ./solo.rb /var/chef/solo.rb
ADD ./solo.json /var/chef/solo.json

RUN cd / && /opt/chef/embedded/bin/berks install --path /var/chef/cookbooks
RUN chef-solo -c /var/chef/solo.rb -j /var/chef/solo.json
RUN echo "daemon off;" >> /etc/nginx/nginx.conf

CMD ["nginx"]

Now, you can just build it up, using

docker build -t majdarbash/test_docker

After building the image, you can run it exposing the port 8080:
docker run -d -p 8080:80 majdarbash/test_docker

Majd Arbash

Leave a Reply