1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #5599 from ostezer/docs-rewrite-examples-mongodb

Rewrite and update the MongoDB service article
This commit is contained in:
James Turnbull 2014-05-09 10:25:37 +02:00
commit 12201e2ea3
3 changed files with 158 additions and 59 deletions

View file

@ -57,7 +57,7 @@ pages:
- ['examples/hello_world.md', 'Examples', 'Hello World']
- ['examples/nodejs_web_app.md', 'Examples', 'Node.js web application']
- ['examples/python_web_app.md', 'Examples', 'Python web application']
- ['examples/mongodb.md', 'Examples', 'MongoDB service']
- ['examples/mongodb.md', 'Examples', 'Dockerizing MongoDB']
- ['examples/running_redis_service.md', 'Examples', 'Redis service']
- ['examples/postgresql_service.md', 'Examples', 'PostgreSQL service']
- ['examples/running_riak_service.md', 'Examples', 'Running a Riak service']

View file

@ -1,89 +1,164 @@
page_title: Building a Docker Image with MongoDB
page_description: How to build a Docker image with MongoDB pre-installed
page_keywords: docker, example, package installation, networking, mongodb
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
# Building an Image with MongoDB
# Dockerizing MongoDB
> **Note**:
## 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:**
>
> - This example assumes you have Docker running in daemon mode. For
> more information please see [*Check your Docker
> install*](../hello_world/#running-examples).
> - **If you don't like sudo** then see [*Giving non-root
> access*](/installation/binaries/#dockergroup)
> This example assumes you have Docker running in daemon mode. To verify,
> try running `sudo docker info`.
> For more information, please see: [*Check your Docker installation*](
> /examples/hello_world/#running-examples).
The goal of this example is to show how you can build your own Docker
images with MongoDB pre-installed. We will do that by constructing a
Dockerfile that downloads a base image, adds an
apt source and installs the database software on Ubuntu.
> **Note:**
>
> If you do **_not_** like `sudo`, you might want to check out:
> [*Giving non-root access*](installation/binaries/#giving-non-root-access).
## Creating a Dockerfile
## Creating a Dockerfile for MongoDB
Create an empty file called Dockerfile:
Let's create our `Dockerfile` and start building it:
$ touch Dockerfile
$ nano Dockerfile
Next, define the parent image you want to use to build your own image on
top of. Here, we'll use [Ubuntu](https://index.docker.io/_/ubuntu/)
(tag: `latest`) available on the [docker
index](http://index.docker.io):
Although optional, it is handy to have comments at the beginning of a
`Dockerfile` explaining its purpose:
FROM ubuntu:latest
# 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/
Since we want to be running the latest version of MongoDB we'll need to
add the 10gen repo to our apt sources list.
> **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.
# Add 10gen official apt source to the sources list
We will build our image using the latest version of Ubuntu from the
[Docker.io Ubuntu](https://index.docker.io/_/ubuntu/) repository.
# Format: FROM repository[:version]
FROM ubuntu:latest
Continuing, we will declare the `MAINTAINER` of the `Dockerfile`:
# Format: MAINTAINER Name <email@addr.ess>
MAINTAINER M.Y. Name <myname@addr.ess>
> **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
Then, we don't want Ubuntu to complain about init not being available so
we'll divert `/sbin/initctl` to
`/bin/true` so it thinks everything is working.
After this initial preparation we can update our packages and install MongoDB.
# Hack for initctl not being available in Ubuntu
RUN dpkg-divert --local --rename --add /sbin/initctl
RUN ln -s /bin/true /sbin/initctl
Afterwards we'll be able to update our apt repositories and install
MongoDB
# Install MongoDB
# Update apt-get sources AND install MongoDB
RUN apt-get update
RUN apt-get install mongodb-10gen
RUN apt-get install -y -q mongodb-org
To run MongoDB we'll have to create the default data directory (because
we want it to run without needing to provide a special configuration
file)
> **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.
# Create the MongoDB data directory
RUN mkdir -p /data/db
Finally, we'll expose the standard port that MongoDB runs on, 27107, as
well as define an `ENTRYPOINT` instruction for the
container.
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.
# Expose port 27017 from the container to the host
EXPOSE 27017
ENTRYPOINT ["usr/bin/mongod"]
Now, lets build the image which will go through the
Dockerfile we made and run all of the commands.
# Set usr/bin/mongod as the dockerized entry-point application
ENTRYPOINT usr/bin/mongod
$ sudo docker build -t <yourname>/mongodb .
Now save the file and let's build our image.
Now you should be able to run `mongod` as a daemon
and be able to connect on the local port!
> **Note:**
>
> The full version of this `Dockerfile` can be found [here](/
> /examples/mongodb/Dockerfile).
# Regular style
$ MONGO_ID=$(sudo docker run -d <yourname>/mongodb)
## Building the MongoDB Docker image
# Lean and mean
$ MONGO_ID=$(sudo docker run -d <yourname>/mongodb --noprealloc --smallfiles)
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.
# Check the logs out
$ sudo docker logs $MONGO_ID
# Format: sudo docker build --tag/-t <user-name>/<repository> .
# Example:
$ sudo docker build --tag my/repo .
# Connect and play around
$ mongo --port <port you get from `docker ps`>
Once this command is issued, Docker will go through the `Dockerfile` and build
the image. The final image will be tagged `my/repo`.
Sweet!
## 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
Using the MongoDB image we created, we can run one or more MongoDB instances
as daemon process(es).
# Basic way
# Usage: sudo docker run --name <name for container> -d <user-name>/<repository>
$ sudo docker run --name mongo_instance_001 -d my/repo
# 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
# Checking out the logs of a MongoDB container
# Usage: sudo docker logs <name for container>
$ sudo docker logs mongo_instance_001
# Playing with MongoDB
# Usage: mongo --port <port you get from `docker ps`>
$ mongo --port 12345
## Learn more
- [Linking containers](/use/working_with_links_names/)
- [Cross-host linking containers](/use/ambassador_pattern_linking/)
- [Creating a Trusted Build](/docker-io/builds/#trusted-builds)

View file

@ -0,0 +1,24 @@
# 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/
FROM ubuntu:latest
MAINTAINER Docker
# 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
# Update apt-get sources AND install MongoDB
RUN apt-get update
RUN apt-get install -y -q mongodb-org
# Create the MongoDB data directory
RUN mkdir -p /data/db
# Expose port #27017 from the container to the host
EXPOSE 27017
# Set usr/bin/mongod as the dockerized entry-point application
ENTRYPOINT usr/bin/mongod