2015-06-07 23:07:20 -04:00
|
|
|
<!--[metadata]>
|
|
|
|
+++
|
|
|
|
title = "Dockerizing MongoDB"
|
|
|
|
description = "Creating a Docker image with MongoDB pre-installed using a Dockerfile and sharing the image on Docker Hub"
|
2015-12-07 15:29:09 -05:00
|
|
|
keywords = ["docker, dockerize, dockerizing, article, example, docker.io, platform, package, installation, networking, mongodb, containers, images, image, sharing, dockerfile, build, auto-building, framework"]
|
2015-06-07 23:07:20 -04:00
|
|
|
[menu.main]
|
|
|
|
parent = "smn_applied"
|
|
|
|
+++
|
|
|
|
<![end-metadata]-->
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-21 17:05:19 -04:00
|
|
|
# Dockerizing MongoDB
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
## Introduction
|
|
|
|
|
2014-06-01 16:48:04 -04:00
|
|
|
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 Hub registry](https://hub.docker.com) and share it with others!
|
2014-05-05 13:17:58 -04:00
|
|
|
|
2015-08-30 10:54:21 -04:00
|
|
|
> **Note:** This guide will show the mechanics of building a MongoDB container, but
|
2015-05-25 13:39:56 -04:00
|
|
|
> you will probably want to use the official image on [Docker Hub]( https://registry.hub.docker.com/_/mongo/)
|
2015-05-18 16:08:31 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
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:**
|
2015-08-31 22:27:29 -04:00
|
|
|
>
|
|
|
|
> If you do **_not_** like `sudo`, you might want to check out:
|
2015-10-09 19:50:41 -04:00
|
|
|
> [*Giving non-root access*](../installation/binaries.md#giving-non-root-access).
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
## Creating a Dockerfile for MongoDB
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
Let's create our `Dockerfile` and start building it:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
$ nano Dockerfile
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
Although optional, it is handy to have comments at the beginning of a
|
|
|
|
`Dockerfile` explaining its purpose:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04: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-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04: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-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
We will build our image using the latest version of Ubuntu from the
|
2014-06-01 16:48:04 -04:00
|
|
|
[Docker Hub Ubuntu](https://registry.hub.docker.com/_/ubuntu/) repository.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
# Format: FROM repository[:version]
|
|
|
|
FROM ubuntu:latest
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
Continuing, we will declare the `MAINTAINER` of the `Dockerfile`:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
# Format: MAINTAINER Name <email@addr.ess>
|
|
|
|
MAINTAINER M.Y. Name <myname@addr.ess>
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04: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
|
2015-05-18 16:08:31 -04:00
|
|
|
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
|
|
|
|
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
|
2014-05-05 13:17:58 -04:00
|
|
|
|
|
|
|
After this initial preparation we can update our packages and install MongoDB.
|
|
|
|
|
|
|
|
# Update apt-get sources AND install MongoDB
|
2014-07-07 14:06:34 -04:00
|
|
|
RUN apt-get update && apt-get install -y mongodb-org
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
> **Tip:** You can install a specific version of MongoDB by using a list
|
|
|
|
> of required packages with versions, e.g.:
|
2015-08-31 22:27:29 -04:00
|
|
|
>
|
2015-05-18 16:08:31 -04:00
|
|
|
> 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
|
2014-05-05 13:17:58 -04:00
|
|
|
|
|
|
|
MongoDB requires a data directory. Let's create it as the final step of our
|
|
|
|
installation instructions.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
# Create the MongoDB data directory
|
|
|
|
RUN mkdir -p /data/db
|
|
|
|
|
2014-05-05 13:17:58 -04: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-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
# Expose port 27017 from the container to the host
|
2014-04-15 20:53:12 -04:00
|
|
|
EXPOSE 27017
|
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
# Set usr/bin/mongod as the dockerized entry-point application
|
2015-05-18 16:08:31 -04:00
|
|
|
ENTRYPOINT ["/usr/bin/mongod"]
|
2014-05-05 13:17:58 -04:00
|
|
|
|
|
|
|
Now save the file and let's build our image.
|
|
|
|
|
|
|
|
> **Note:**
|
2015-08-31 22:27:29 -04:00
|
|
|
>
|
|
|
|
> The full version of this `Dockerfile` can be found [here](https://github.com/docker/docker/blob/master/docs/examples/mongodb/Dockerfile).
|
2014-05-05 13:17:58 -04: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.
|
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
# Format: docker build --tag/-t <user-name>/<repository> .
|
2014-05-05 13:17:58 -04:00
|
|
|
# Example:
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker build --tag my/repo .
|
2014-05-05 13:17:58 -04:00
|
|
|
|
|
|
|
Once this command is issued, Docker will go through the `Dockerfile` and build
|
|
|
|
the image. The final image will be tagged `my/repo`.
|
|
|
|
|
2014-06-01 16:48:04 -04:00
|
|
|
## Pushing the MongoDB image to Docker Hub
|
2014-05-05 13:17:58 -04:00
|
|
|
|
|
|
|
All Docker image repositories can be hosted and shared on
|
2014-06-01 16:48:04 -04:00
|
|
|
[Docker Hub](https://hub.docker.com) with the `docker push` command. For this,
|
2014-05-05 13:17:58 -04:00
|
|
|
you need to be logged-in.
|
|
|
|
|
|
|
|
# Log-in
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker login
|
2014-05-05 13:17:58 -04:00
|
|
|
Username:
|
|
|
|
..
|
|
|
|
|
|
|
|
# Push the image
|
2015-03-26 14:12:37 -04:00
|
|
|
# Format: docker push <user-name>/<repository>
|
|
|
|
$ docker push my/repo
|
2014-05-05 13:17:58 -04:00
|
|
|
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-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
Using the MongoDB image we created, we can run one or more MongoDB instances
|
|
|
|
as daemon process(es).
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
# Basic way
|
2015-03-26 14:12:37 -04:00
|
|
|
# Usage: docker run --name <name for container> -d <user-name>/<repository>
|
2015-05-18 16:08:31 -04:00
|
|
|
$ docker run -p 27017:27017 --name mongo_instance_001 -d my/repo
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
# Dockerized MongoDB, lean and mean!
|
2015-03-26 14:12:37 -04:00
|
|
|
# Usage: docker run --name <name for container> -d <user-name>/<repository> --noprealloc --smallfiles
|
2015-08-30 10:54:21 -04:00
|
|
|
$ docker run -p 27017:27017 --name mongo_instance_001 -d my/repo --smallfiles
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
# Checking out the logs of a MongoDB container
|
2015-03-26 14:12:37 -04:00
|
|
|
# Usage: docker logs <name for container>
|
|
|
|
$ docker logs mongo_instance_001
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-05 13:17:58 -04:00
|
|
|
# Playing with MongoDB
|
2015-08-31 22:27:29 -04:00
|
|
|
# Usage: mongo --port <port you get from `docker ps`>
|
2015-05-18 16:08:31 -04:00
|
|
|
$ mongo --port 27017
|
|
|
|
|
2015-07-17 16:05:45 -04:00
|
|
|
# If using docker-machine
|
|
|
|
# Usage: mongo --port <port you get from `docker ps`> --host <ip address from `docker-machine ip VM_NAME`>
|
2015-05-18 16:08:31 -04:00
|
|
|
$ mongo --port 27017 --host 192.168.59.103
|
|
|
|
|
|
|
|
> **Tip:**
|
|
|
|
If you want to run two containers on the same engine, then you will need to map
|
|
|
|
the exposed port to two different ports on the host
|
|
|
|
|
|
|
|
# Start two containers and map the ports
|
|
|
|
$ docker run -p 28001:27017 --name mongo_instance_001 -d my/repo
|
|
|
|
$ docker run -p 28002:27017 --name mongo_instance_002 -d my/repo
|
|
|
|
|
|
|
|
# Now you can connect to each MongoDB instance on the two ports
|
|
|
|
$ mongo --port 28001
|
|
|
|
$ mongo --port 28002
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-09-30 16:11:36 -04:00
|
|
|
- [Linking containers](../userguide/networking/default_network/dockerlinks.md)
|
2015-10-09 19:50:41 -04:00
|
|
|
- [Cross-host linking containers](../articles/ambassador_pattern_linking.md)
|
|
|
|
- [Creating an Automated Build](https://docs.docker.com/docker-hub/builds/)
|