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