mirror of https://github.com/docker/docs.git
Merge pull request #5599 from ostezer/docs-rewrite-examples-mongodb
Rewrite and update the MongoDB service article
This commit is contained in:
commit
12201e2ea3
|
@ -57,7 +57,7 @@ pages:
|
||||||
- ['examples/hello_world.md', 'Examples', 'Hello World']
|
- ['examples/hello_world.md', 'Examples', 'Hello World']
|
||||||
- ['examples/nodejs_web_app.md', 'Examples', 'Node.js web application']
|
- ['examples/nodejs_web_app.md', 'Examples', 'Node.js web application']
|
||||||
- ['examples/python_web_app.md', 'Examples', 'Python 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/running_redis_service.md', 'Examples', 'Redis service']
|
||||||
- ['examples/postgresql_service.md', 'Examples', 'PostgreSQL service']
|
- ['examples/postgresql_service.md', 'Examples', 'PostgreSQL service']
|
||||||
- ['examples/running_riak_service.md', 'Examples', 'Running a Riak service']
|
- ['examples/running_riak_service.md', 'Examples', 'Running a Riak service']
|
||||||
|
|
|
@ -1,89 +1,164 @@
|
||||||
page_title: Building a Docker Image with MongoDB
|
page_title: Dockerizing MongoDB
|
||||||
page_description: How to build a Docker image with MongoDB pre-installed
|
page_description: Creating a Docker image with MongoDB pre-installed using a Dockerfile and sharing the image on Docker.io
|
||||||
page_keywords: docker, example, package installation, networking, mongodb
|
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
|
> This example assumes you have Docker running in daemon mode. To verify,
|
||||||
> more information please see [*Check your Docker
|
> try running `sudo docker info`.
|
||||||
> install*](../hello_world/#running-examples).
|
> For more information, please see: [*Check your Docker installation*](
|
||||||
> - **If you don't like sudo** then see [*Giving non-root
|
> /examples/hello_world/#running-examples).
|
||||||
> access*](/installation/binaries/#dockergroup)
|
|
||||||
|
|
||||||
The goal of this example is to show how you can build your own Docker
|
> **Note:**
|
||||||
images with MongoDB pre-installed. We will do that by constructing a
|
>
|
||||||
Dockerfile that downloads a base image, adds an
|
> If you do **_not_** like `sudo`, you might want to check out:
|
||||||
apt source and installs the database software on Ubuntu.
|
> [*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
|
Although optional, it is handy to have comments at the beginning of a
|
||||||
top of. Here, we'll use [Ubuntu](https://index.docker.io/_/ubuntu/)
|
`Dockerfile` explaining its purpose:
|
||||||
(tag: `latest`) available on the [docker
|
|
||||||
index](http://index.docker.io):
|
|
||||||
|
|
||||||
|
# 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/
|
||||||
|
|
||||||
|
> **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.
|
||||||
|
|
||||||
|
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
|
FROM ubuntu:latest
|
||||||
|
|
||||||
Since we want to be running the latest version of MongoDB we'll need to
|
Continuing, we will declare the `MAINTAINER` of the `Dockerfile`:
|
||||||
add the 10gen repo to our apt sources list.
|
|
||||||
|
|
||||||
# Add 10gen official apt source to the sources list
|
# 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 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
|
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
|
After this initial preparation we can update our packages and install MongoDB.
|
||||||
we'll divert `/sbin/initctl` to
|
|
||||||
`/bin/true` so it thinks everything is working.
|
|
||||||
|
|
||||||
# Hack for initctl not being available in Ubuntu
|
# Update apt-get sources AND install MongoDB
|
||||||
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
|
|
||||||
RUN apt-get update
|
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
|
> **Tip:** You can install a specific version of MongoDB by using a list
|
||||||
we want it to run without needing to provide a special configuration
|
> of required packages with versions, e.g.:
|
||||||
file)
|
>
|
||||||
|
> 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
|
# Create the MongoDB data directory
|
||||||
RUN mkdir -p /data/db
|
RUN mkdir -p /data/db
|
||||||
|
|
||||||
Finally, we'll expose the standard port that MongoDB runs on, 27107, as
|
Lastly we set the `ENTRYPOINT` which will tell Docker to run `mongod` inside
|
||||||
well as define an `ENTRYPOINT` instruction for the
|
the containers launched from our MongoDB image. And for ports, we will use
|
||||||
container.
|
the `EXPOSE` instruction.
|
||||||
|
|
||||||
|
# Expose port 27017 from the container to the host
|
||||||
EXPOSE 27017
|
EXPOSE 27017
|
||||||
ENTRYPOINT ["usr/bin/mongod"]
|
|
||||||
|
|
||||||
Now, lets build the image which will go through the
|
# Set usr/bin/mongod as the dockerized entry-point application
|
||||||
Dockerfile we made and run all of the commands.
|
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
|
> **Note:**
|
||||||
and be able to connect on the local port!
|
>
|
||||||
|
> The full version of this `Dockerfile` can be found [here](/
|
||||||
|
> /examples/mongodb/Dockerfile).
|
||||||
|
|
||||||
# Regular style
|
## Building the MongoDB Docker image
|
||||||
$ MONGO_ID=$(sudo docker run -d <yourname>/mongodb)
|
|
||||||
|
|
||||||
# Lean and mean
|
With our `Dockerfile`, we can now build the MongoDB image using Docker. Unless
|
||||||
$ MONGO_ID=$(sudo docker run -d <yourname>/mongodb --noprealloc --smallfiles)
|
experimenting, it is always a good practice to tag Docker images by passing the
|
||||||
|
`--tag` option to `docker build` command.
|
||||||
|
|
||||||
# Check the logs out
|
# Format: sudo docker build --tag/-t <user-name>/<repository> .
|
||||||
$ sudo docker logs $MONGO_ID
|
# Example:
|
||||||
|
$ sudo docker build --tag my/repo .
|
||||||
|
|
||||||
# Connect and play around
|
Once this command is issued, Docker will go through the `Dockerfile` and build
|
||||||
$ mongo --port <port you get from `docker ps`>
|
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)
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue