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.

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:
knife cookbook download docker
    1. Chef Recipe: To create docker image and launch mongo DB containers:
# 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
        tag 'v0.1.0'
        source '/home/ubuntu/chef-repo/cookbooks/docker-mongodb/files/default/Dockerfile'
        action :build_if_missing

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

# Launch docker container using above mentioned docker image
docker_container 'launch' do
        repo 'neerjaj2/mongodb-docker'
        tag 'v0.1.0'
        hostname 'mongo1'
        domain_name ''
        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.
    1. Contents of Dockerfile are:
FROM ubuntu:latest
RUN apt-key adv --keyserver hkp:// --recv 7F0CEB10
RUN echo 'deb 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"]
    1. Also add dependency in chef-repo/cookbooks/docker-mongodb/metadata.rb, as shown below:
depends 'docker', '~> 1.0'
  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.
chef-client -z -r “recipe[docker-mongodb]”

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.


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”


Leave a comment -