{"id":27986,"date":"2015-10-10T22:27:28","date_gmt":"2015-10-10T16:57:28","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=27986"},"modified":"2015-10-13T13:01:33","modified_gmt":"2015-10-13T07:31:33","slug":"how-to-deploy-mongodb-docker-containers-using-chef","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-deploy-mongodb-docker-containers-using-chef\/","title":{"rendered":"How to deploy MongoDB Docker Containers using Chef"},"content":{"rendered":"<p>In my last <a href=\"http:\/\/www.tothenew.com\/blog\/mongodb-replica-set-on-docker\/\">blog<\/a>, we discussed how to setup MongoDB replica set on Docker, and in this blog we&#8217;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.<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28022\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/mongodb_docker.png\" alt=\"mongodb_docker\" width=\"800\" height=\"266\" \/><\/p>\n<p><b>Use Case:<\/b>\u00a0As 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\u00a0if we can use configuration manager tool like <a href=\"http:\/\/www.tothenew.com\/devops-chef-puppet-docker\">Chef for docker<\/a> deployments and management. In this blog, I will be demonstrating how you can deploy MongoDB docker containers using chef.<\/p>\n<p><b>Pre-requisite:<\/b>\u00a0Before going ahead with this blog prior knowledge of working with MongoDB, Docker and Chef basics are required.<\/p>\n<ol>\n<ol>\n<li>Download docker cookbook using command as mentioned below:<\/li>\n<\/ol>\n<\/ol>\n<p>[js]knife cookbook download docker[\/js]<\/p>\n<ol>\n<ol>\n<li>Chef Recipe: To create docker image and launch mongo DB containers:<\/li>\n<\/ol>\n<\/ol>\n<p>[js]# Using docker_service for installation and configuration<br \/>\ndocker_service &#8216;default&#8217; do<br \/>\n        action [:create, :start]<br \/>\nend<\/p>\n<p># Create docker image, if image not found locally<br \/>\ndocker_image &#8216;neerjaj2\/mongodb-docker&#8217; do<br \/>\n        tag &#8216;v0.1.0&#8217;<br \/>\n        source &#8216;\/home\/ubuntu\/chef-repo\/cookbooks\/docker-mongodb\/files\/default\/Dockerfile&#8217;<br \/>\n        action :build_if_missing<br \/>\nend<\/p>\n<p># Below code can be used to pull docker image from docker hub<br \/>\n&quot;docker_image &#8216;neerjaj2\/mongodb&#8217; do<br \/>\n        action :pull<br \/>\nend&quot;<\/p>\n<p># Launch docker container using above mentioned docker image<br \/>\ndocker_container &#8216;launch&#8217; do<br \/>\n        repo &#8216;neerjaj2\/mongodb-docker&#8217;<br \/>\n        tag &#8216;v0.1.0&#8217;<br \/>\n        hostname &#8216;mongo1&#8217;<br \/>\n        domain_name &#8216;ttnd.com&#8217;<br \/>\n        publish_all_ports<br \/>\n        command &#8216;&#8211;replSet ttnd &#8211;noprealloc &#8211;smallfiles&#8217;<br \/>\nend [\/js]<\/p>\n<h6>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.<\/h6>\n<ol>\n<ol>\n<li>Contents of Dockerfile are:<\/li>\n<\/ol>\n<\/ol>\n<p>[js]FROM ubuntu:latest<br \/>\nRUN apt-key adv &#8211;keyserver hkp:\/\/keyserver.ubuntu.com:80 &#8211;recv 7F0CEB10<br \/>\nRUN echo &#8216;deb http:\/\/downloads-distro.mongodb.org\/repo\/ubuntu-upstart dist 10gen&#8217; | tee \/etc\/apt\/sources.list.d\/10gen.list<br \/>\nRUN apt-get update<br \/>\nRUN apt-get install -y mongodb-10gen=2.2.3<br \/>\nRUN mkdir -p \/data\/db<br \/>\nEXPOSE 27017<br \/>\nENTRYPOINT [&quot;usr\/bin\/mongod&quot;]<br \/>\n[\/js]<\/p>\n<ol>\n<ol>\n<li>Also add dependency in chef-repo\/cookbooks\/docker-mongodb\/metadata.rb, as shown below:<\/li>\n<\/ol>\n<\/ol>\n<p>[js]depends &#8216;docker&#8217;, &#8216;~&gt; 1.0&#8242;[\/js]<\/p>\n<ol>\n<li>So, let\u2019s execute chef recipe using command mentioned below:<\/li>\n<\/ol>\n<ul>\n<ul>\n<li>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.<\/li>\n<\/ul>\n<\/ul>\n<p>[js]chef-client -z -r \u201crecipe[docker-mongodb]\u201d[\/js]<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-27992\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/create_docker_image_launch_container_with_chef_1.png\" alt=\"mongodb docker deployment using chef\" width=\"800\" height=\"275\" \/><\/p>\n<p>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.<\/p>\n<ul>\n<ul>\n<li>Let us check Mongodb docker container hostname and MongoDB daemon status:<\/li>\n<\/ul>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-27998\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/connect_to_docker_check_hostname_mongo_container_2.png\" alt=\"mongodb docker container deploy through chef\" width=\"800\" height=\"110\" \/><br \/>\nYou can now play around this chef recipe and use it as per your requirement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last blog, we discussed how to setup MongoDB replica set on Docker, and in this blog we&#8217;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:\u00a0As discussed in the previous blog, we earlier [&hellip;]<\/p>\n","protected":false},"author":216,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[1],"tags":[1883,2558,4846],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/27986"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/216"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=27986"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/27986\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=27986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=27986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=27986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}