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