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