2014-05-05 17:17:58 +00:00
|
|
|
page_title: Dockerizing MongoDB
|
|
|
|
page_description: Creating a Docker image with MongoDB pre-installed using a Dockerfile and sharing the image on Docker.io
|
|
|
|
page_keywords: docker, dockerize, dockerizing, article, example, docker.io, platform, package, installation, networking, mongodb, containers, images, image, sharing, dockerfile, build, auto-building, virtualization, framework
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-21 21:05:19 +00:00
|
|
|
# Dockerizing MongoDB
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
## Introduction
|
|
|
|
|
|
|
|
In this example, we are going to learn how to build a Docker image
|
|
|
|
with MongoDB pre-installed.
|
|
|
|
We'll also see how to `push` that image to the [Docker.io registry](
|
|
|
|
https://index.docker.io) and share it with others!
|
|
|
|
|
|
|
|
Using Docker and containers for deploying [MongoDB](https://www.mongodb.org/)
|
|
|
|
instances will bring several benefits, such as:
|
|
|
|
|
|
|
|
- Easy to maintain, highly configurable MongoDB instances;
|
|
|
|
- Ready to run and start working within milliseconds;
|
|
|
|
- Based on globally accessible and shareable images.
|
|
|
|
|
|
|
|
> **Note:**
|
|
|
|
>
|
|
|
|
> If you do **_not_** like `sudo`, you might want to check out:
|
2014-05-21 21:05:19 +00:00
|
|
|
> [*Giving non-root access*](/installation/binaries/#giving-non-root-access).
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
## Creating a Dockerfile for MongoDB
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
Let's create our `Dockerfile` and start building it:
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
$ nano Dockerfile
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
Although optional, it is handy to have comments at the beginning of a
|
|
|
|
`Dockerfile` explaining its purpose:
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
# Dockerizing MongoDB: Dockerfile for building MongoDB images
|
|
|
|
# Based on ubuntu:latest, installs MongoDB following the instructions from:
|
|
|
|
# http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
> **Tip:** `Dockerfile`s are flexible. However, they need to follow a certain
|
|
|
|
> format. The first item to be defined is the name of an image, which becomes
|
|
|
|
> the *parent* of your *Dockerized MongoDB* image.
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
We will build our image using the latest version of Ubuntu from the
|
|
|
|
[Docker.io Ubuntu](https://index.docker.io/_/ubuntu/) repository.
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
# Format: FROM repository[:version]
|
|
|
|
FROM ubuntu:latest
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
Continuing, we will declare the `MAINTAINER` of the `Dockerfile`:
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
# Format: MAINTAINER Name <email@addr.ess>
|
|
|
|
MAINTAINER M.Y. Name <myname@addr.ess>
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
> **Note:** Although Ubuntu systems have MongoDB packages, they are likely to
|
|
|
|
> be outdated. Therefore in this example, we will use the official MongoDB
|
|
|
|
> packages.
|
|
|
|
|
|
|
|
We will begin with importing the MongoDB public GPG key. We will also create
|
|
|
|
a MongoDB repository file for the package manager.
|
|
|
|
|
|
|
|
# Installation:
|
|
|
|
# Import MongoDB public GPG key AND create a MongoDB list file
|
|
|
|
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
|
|
|
|
RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/10gen.list
|
|
|
|
|
|
|
|
After this initial preparation we can update our packages and install MongoDB.
|
|
|
|
|
|
|
|
# Update apt-get sources AND install MongoDB
|
2014-04-16 00:53:12 +00:00
|
|
|
RUN apt-get update
|
2014-05-05 17:17:58 +00:00
|
|
|
RUN apt-get install -y -q mongodb-org
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
> **Tip:** You can install a specific version of MongoDB by using a list
|
|
|
|
> of required packages with versions, e.g.:
|
|
|
|
>
|
|
|
|
> RUN apt-get install -y -q 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
|
|
|
|
|
|
|
|
MongoDB requires a data directory. Let's create it as the final step of our
|
|
|
|
installation instructions.
|
2014-04-16 00:53:12 +00:00
|
|
|
|
|
|
|
# Create the MongoDB data directory
|
|
|
|
RUN mkdir -p /data/db
|
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
Lastly we set the `ENTRYPOINT` which will tell Docker to run `mongod` inside
|
|
|
|
the containers launched from our MongoDB image. And for ports, we will use
|
|
|
|
the `EXPOSE` instruction.
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
# Expose port 27017 from the container to the host
|
2014-04-16 00:53:12 +00:00
|
|
|
EXPOSE 27017
|
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
# Set usr/bin/mongod as the dockerized entry-point application
|
|
|
|
ENTRYPOINT usr/bin/mongod
|
|
|
|
|
|
|
|
Now save the file and let's build our image.
|
|
|
|
|
|
|
|
> **Note:**
|
|
|
|
>
|
2014-05-21 21:05:19 +00:00
|
|
|
> The full version of this `Dockerfile` can be found [here](/examples/mongodb/Dockerfile).
|
2014-05-05 17:17:58 +00:00
|
|
|
|
|
|
|
## Building the MongoDB Docker image
|
|
|
|
|
|
|
|
With our `Dockerfile`, we can now build the MongoDB image using Docker. Unless
|
|
|
|
experimenting, it is always a good practice to tag Docker images by passing the
|
|
|
|
`--tag` option to `docker build` command.
|
|
|
|
|
|
|
|
# Format: sudo docker build --tag/-t <user-name>/<repository> .
|
|
|
|
# Example:
|
|
|
|
$ sudo docker build --tag my/repo .
|
|
|
|
|
|
|
|
Once this command is issued, Docker will go through the `Dockerfile` and build
|
|
|
|
the image. The final image will be tagged `my/repo`.
|
|
|
|
|
|
|
|
## Pushing the MongoDB image to Docker.io
|
|
|
|
|
|
|
|
All Docker image repositories can be hosted and shared on
|
|
|
|
[Docker.io](https://index.docker.io) with the `docker push` command. For this,
|
|
|
|
you need to be logged-in.
|
|
|
|
|
|
|
|
# Log-in
|
|
|
|
$ sudo docker login
|
|
|
|
Username:
|
|
|
|
..
|
|
|
|
|
|
|
|
# Push the image
|
|
|
|
# Format: sudo docker push <user-name>/<repository>
|
|
|
|
$ sudo docker push my/repo
|
|
|
|
The push refers to a repository [my/repo] (len: 1)
|
|
|
|
Sending image list
|
|
|
|
Pushing repository my/repo (1 tags)
|
|
|
|
..
|
|
|
|
|
|
|
|
## Using the MongoDB image
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
Using the MongoDB image we created, we can run one or more MongoDB instances
|
|
|
|
as daemon process(es).
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
# Basic way
|
|
|
|
# Usage: sudo docker run --name <name for container> -d <user-name>/<repository>
|
|
|
|
$ sudo docker run --name mongo_instance_001 -d my/repo
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
# Dockerized MongoDB, lean and mean!
|
|
|
|
# Usage: sudo docker run --name <name for container> -d <user-name>/<repository> --noprealloc --smallfiles
|
|
|
|
$ sudo docker run --name mongo_instance_001 -d my/repo --noprealloc --smallfiles
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
# Checking out the logs of a MongoDB container
|
|
|
|
# Usage: sudo docker logs <name for container>
|
|
|
|
$ sudo docker logs mongo_instance_001
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-05 17:17:58 +00:00
|
|
|
# Playing with MongoDB
|
|
|
|
# Usage: mongo --port <port you get from `docker ps`>
|
|
|
|
$ mongo --port 12345
|
2014-04-16 00:53:12 +00:00
|
|
|
|
2014-05-21 21:05:19 +00:00
|
|
|
- [Linking containers](/userguide/dockerlinks)
|
|
|
|
- [Cross-host linking containers](/articles/ambassador_pattern_linking/)
|
2014-05-05 17:17:58 +00:00
|
|
|
- [Creating a Trusted Build](/docker-io/builds/#trusted-builds)
|