mongodb.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. :title: Building a Docker Image with MongoDB
  2. :description: How to build a Docker image with MongoDB pre-installed
  3. :keywords: docker, example, package installation, networking, mongodb
  4. .. _mongodb_image:
  5. Building an Image with MongoDB
  6. ==============================
  7. .. include:: example_header.inc
  8. The goal of this example is to show how you can build your own
  9. Docker images with MongoDB pre-installed. We will do that by
  10. constructing a ``Dockerfile`` that downloads a base image, adds an
  11. apt source and installs the database software on Ubuntu.
  12. Creating a ``Dockerfile``
  13. +++++++++++++++++++++++++
  14. Create an empty file called ``Dockerfile``:
  15. .. code-block:: bash
  16. touch Dockerfile
  17. Next, define the parent image you want to use to build your own image on top of.
  18. Here, we’ll use `Ubuntu <https://index.docker.io/_/ubuntu/>`_ (tag: ``latest``)
  19. available on the `docker index <http://index.docker.io>`_:
  20. .. code-block:: bash
  21. FROM ubuntu:latest
  22. Since we want to be running the latest version of MongoDB we'll need to add the
  23. 10gen repo to our apt sources list.
  24. .. code-block:: bash
  25. # Add 10gen official apt source to the sources list
  26. RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
  27. RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/10gen.list
  28. Then, we don't want Ubuntu to complain about init not being available so we'll
  29. divert ``/sbin/initctl`` to ``/bin/true`` so it thinks everything is working.
  30. .. code-block:: bash
  31. # Hack for initctl not being available in Ubuntu
  32. RUN dpkg-divert --local --rename --add /sbin/initctl
  33. RUN ln -s /bin/true /sbin/initctl
  34. Afterwards we'll be able to update our apt repositories and install MongoDB
  35. .. code-block:: bash
  36. # Install MongoDB
  37. RUN apt-get update
  38. RUN apt-get install mongodb-10gen
  39. To run MongoDB we'll have to create the default data directory (because we want it to
  40. run without needing to provide a special configuration file)
  41. .. code-block:: bash
  42. # Create the MongoDB data directory
  43. RUN mkdir -p /data/db
  44. Finally, we'll expose the standard port that MongoDB runs on, 27107, as well as
  45. define an ``ENTRYPOINT`` instruction for the container.
  46. .. code-block:: bash
  47. EXPOSE 27017
  48. ENTRYPOINT ["usr/bin/mongod"]
  49. Now, lets build the image which will go through the ``Dockerfile`` we made and
  50. run all of the commands.
  51. .. code-block:: bash
  52. sudo docker build -t <yourname>/mongodb .
  53. Now you should be able to run ``mongod`` as a daemon and be able to connect on
  54. the local port!
  55. .. code-block:: bash
  56. # Regular style
  57. MONGO_ID=$(sudo docker run -d <yourname>/mongodb)
  58. # Lean and mean
  59. MONGO_ID=$(sudo docker run -d <yourname>/mongodb --noprealloc --smallfiles)
  60. # Check the logs out
  61. sudo docker logs $MONGO_ID
  62. # Connect and play around
  63. mongo --port <port you get from `docker ps`>
  64. Sweet!