How to deploy MongoDB Docker Containers using Chef
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.
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.
- Download docker cookbook using command as mentioned below:
[js]knife cookbook download docker[/js]
- 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]
# Create docker image, if image not found locally
docker_image ‘neerjaj2/mongodb-docker’ do
# Below code can be used to pull docker image from docker hub
"docker_image ‘neerjaj2/mongodb’ do
# Launch docker container using above mentioned docker image
docker_container ‘launch’ do
command ‘–replSet ttnd –noprealloc –smallfiles’
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.
- Contents of Dockerfile are:
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
- Also add dependency in chef-repo/cookbooks/docker-mongodb/metadata.rb, as shown below:
[js]depends ‘docker’, ‘~> 1.0′[/js]
- 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]
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:
You can now play around this chef recipe and use it as per your requirement.
This is so greatly presented.
Is there a way to automate
“deploying a dockarized microservice with the help of chef-solo”