github.com/eikeon/docker@v1.5.0-rc4/docs/sources/examples/mongodb.md (about) 1 page_title: Dockerizing MongoDB 2 page_description: Creating a Docker image with MongoDB pre-installed using a Dockerfile and sharing the image on Docker Hub 3 page_keywords: docker, dockerize, dockerizing, article, example, docker.io, platform, package, installation, networking, mongodb, containers, images, image, sharing, dockerfile, build, auto-building, virtualization, framework 4 5 # Dockerizing MongoDB 6 7 ## Introduction 8 9 In this example, we are going to learn how to build a Docker image with 10 MongoDB pre-installed. We'll also see how to `push` that image to the 11 [Docker Hub registry](https://hub.docker.com) and share it with others! 12 13 Using Docker and containers for deploying [MongoDB](https://www.mongodb.org/) 14 instances will bring several benefits, such as: 15 16 - Easy to maintain, highly configurable MongoDB instances; 17 - Ready to run and start working within milliseconds; 18 - Based on globally accessible and shareable images. 19 20 > **Note:** 21 > 22 > If you do **_not_** like `sudo`, you might want to check out: 23 > [*Giving non-root access*](/installation/binaries/#giving-non-root-access). 24 25 ## Creating a Dockerfile for MongoDB 26 27 Let's create our `Dockerfile` and start building it: 28 29 $ nano Dockerfile 30 31 Although optional, it is handy to have comments at the beginning of a 32 `Dockerfile` explaining its purpose: 33 34 # Dockerizing MongoDB: Dockerfile for building MongoDB images 35 # Based on ubuntu:latest, installs MongoDB following the instructions from: 36 # http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/ 37 38 > **Tip:** `Dockerfile`s are flexible. However, they need to follow a certain 39 > format. The first item to be defined is the name of an image, which becomes 40 > the *parent* of your *Dockerized MongoDB* image. 41 42 We will build our image using the latest version of Ubuntu from the 43 [Docker Hub Ubuntu](https://registry.hub.docker.com/_/ubuntu/) repository. 44 45 # Format: FROM repository[:version] 46 FROM ubuntu:latest 47 48 Continuing, we will declare the `MAINTAINER` of the `Dockerfile`: 49 50 # Format: MAINTAINER Name <email@addr.ess> 51 MAINTAINER M.Y. Name <myname@addr.ess> 52 53 > **Note:** Although Ubuntu systems have MongoDB packages, they are likely to 54 > be outdated. Therefore in this example, we will use the official MongoDB 55 > packages. 56 57 We will begin with importing the MongoDB public GPG key. We will also create 58 a MongoDB repository file for the package manager. 59 60 # Installation: 61 # Import MongoDB public GPG key AND create a MongoDB list file 62 RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 63 RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/10gen.list 64 65 After this initial preparation we can update our packages and install MongoDB. 66 67 # Update apt-get sources AND install MongoDB 68 RUN apt-get update && apt-get install -y mongodb-org 69 70 > **Tip:** You can install a specific version of MongoDB by using a list 71 > of required packages with versions, e.g.: 72 > 73 > RUN apt-get update && apt-get install -y mongodb-org=2.6.1 mongodb-org-server=2.6.1 mongodb-org-shell=2.6.1 mongodb-org-mongos=2.6.1 mongodb-org-tools=2.6.1 74 75 MongoDB requires a data directory. Let's create it as the final step of our 76 installation instructions. 77 78 # Create the MongoDB data directory 79 RUN mkdir -p /data/db 80 81 Lastly we set the `ENTRYPOINT` which will tell Docker to run `mongod` inside 82 the containers launched from our MongoDB image. And for ports, we will use 83 the `EXPOSE` instruction. 84 85 # Expose port 27017 from the container to the host 86 EXPOSE 27017 87 88 # Set usr/bin/mongod as the dockerized entry-point application 89 ENTRYPOINT usr/bin/mongod 90 91 Now save the file and let's build our image. 92 93 > **Note:** 94 > 95 > The full version of this `Dockerfile` can be found [here](/examples/mongodb/Dockerfile). 96 97 ## Building the MongoDB Docker image 98 99 With our `Dockerfile`, we can now build the MongoDB image using Docker. Unless 100 experimenting, it is always a good practice to tag Docker images by passing the 101 `--tag` option to `docker build` command. 102 103 # Format: sudo docker build --tag/-t <user-name>/<repository> . 104 # Example: 105 $ sudo docker build --tag my/repo . 106 107 Once this command is issued, Docker will go through the `Dockerfile` and build 108 the image. The final image will be tagged `my/repo`. 109 110 ## Pushing the MongoDB image to Docker Hub 111 112 All Docker image repositories can be hosted and shared on 113 [Docker Hub](https://hub.docker.com) with the `docker push` command. For this, 114 you need to be logged-in. 115 116 # Log-in 117 $ sudo docker login 118 Username: 119 .. 120 121 # Push the image 122 # Format: sudo docker push <user-name>/<repository> 123 $ sudo docker push my/repo 124 The push refers to a repository [my/repo] (len: 1) 125 Sending image list 126 Pushing repository my/repo (1 tags) 127 .. 128 129 ## Using the MongoDB image 130 131 Using the MongoDB image we created, we can run one or more MongoDB instances 132 as daemon process(es). 133 134 # Basic way 135 # Usage: sudo docker run --name <name for container> -d <user-name>/<repository> 136 $ sudo docker run --name mongo_instance_001 -d my/repo 137 138 # Dockerized MongoDB, lean and mean! 139 # Usage: sudo docker run --name <name for container> -d <user-name>/<repository> --noprealloc --smallfiles 140 $ sudo docker run --name mongo_instance_001 -d my/repo --noprealloc --smallfiles 141 142 # Checking out the logs of a MongoDB container 143 # Usage: sudo docker logs <name for container> 144 $ sudo docker logs mongo_instance_001 145 146 # Playing with MongoDB 147 # Usage: mongo --port <port you get from `docker ps`> 148 $ mongo --port 12345 149 150 - [Linking containers](/userguide/dockerlinks) 151 - [Cross-host linking containers](/articles/ambassador_pattern_linking/) 152 - [Creating an Automated Build](/docker-io/builds/#automated-builds)