How to deploy MongoDB Docker Containers using Chef

10 / Oct / 2015 by Neeraj Gupta 1 comments

In my last blog, we discussed how to setup MongoDB replica set on Docker, and in this blog we’ll be discussing setting up MongoDB docker containers using Chef. After going through this blog, you will be able to setup multiple MongoDB customized docker containers with ease.
mongodb_docker

Use Case: As discussed in the previous blog, we earlier use to setup MongoDB replica set of three docker containers using custom shell scripts. So, I thought it would great if we can use configuration manager tool like Chef for docker deployments and management. In this blog, I will be demonstrating how you can deploy MongoDB docker containers using chef.

Pre-requisite: Before going ahead with this blog prior knowledge of working with MongoDB, Docker and Chef basics are required.

    1. Download docker cookbook using command as mentioned below:

[js]knife cookbook download docker[/js]

    1. Chef Recipe: To create docker image and launch mongo DB containers:

[js]# Using docker_service for installation and configuration
docker_service ‘default’ do
action [:create, :start]
end

# Create docker image, if image not found locally
docker_image ‘neerjaj2/mongodb-docker’ do
tag ‘v0.1.0’
source ‘/home/ubuntu/chef-repo/cookbooks/docker-mongodb/files/default/Dockerfile’
action :build_if_missing
end

# Below code can be used to pull docker image from docker hub
"docker_image ‘neerjaj2/mongodb’ do
action :pull
end"

# Launch docker container using above mentioned docker image
docker_container ‘launch’ do
repo ‘neerjaj2/mongodb-docker’
tag ‘v0.1.0’
hostname ‘mongo1’
domain_name ‘ttnd.com’
publish_all_ports
command ‘–replSet ttnd –noprealloc –smallfiles’
end [/js]

Note: You can either pull docker image from docker hub or create a custom image on every. In the above example, we are creating a new image using chef-repo/cookbooks/docker-Mongodb/files/default/Dockerfile to customize every run.
    1. Contents of Dockerfile are:

[js]FROM ubuntu:latest
RUN apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 7F0CEB10
RUN echo ‘deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen’ | tee /etc/apt/sources.list.d/10gen.list
RUN apt-get update
RUN apt-get install -y mongodb-10gen=2.2.3
RUN mkdir -p /data/db
EXPOSE 27017
ENTRYPOINT ["usr/bin/mongod"]
[/js]

    1. Also add dependency in chef-repo/cookbooks/docker-mongodb/metadata.rb, as shown below:

[js]depends ‘docker’, ‘~> 1.0′[/js]

  1. So, let’s execute chef recipe using command mentioned below:
    • To run chef recipe on localhost, use below mentioned command. In case you want to bootstrap remote host you can do that using knife bootstrap command.

[js]chef-client -z -r “recipe[docker-mongodb]”[/js]

mongodb docker deployment using chef

From the above screenshot, you can see our chef recipe created new docker image neerjaj2/mongodb-docker and deployed a MongoDB container using the same.

    • Let us check Mongodb docker container hostname and MongoDB daemon status:

mongodb docker container deploy through chef
You can now play around this chef recipe and use it as per your requirement.

FOUND THIS USEFUL? SHARE IT

comments (1 “How to deploy MongoDB Docker Containers using Chef”)

  1. Adarsh.

    This is so greatly presented.
    Is there a way to automate
    “deploying a dockarized microservice with the help of chef-solo”

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *