Docker 安装 MongoDB

方法一、docker pull mongo

查找Docker Hub上的mongo镜像

  1. test@docker:~/mongo$ docker search mongo
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. mongo MongoDB document databases ... 1989 [OK]
  4. mongo-express Web-based MongoDB admin int... 22 [OK]
  5. mvertes/alpine-mongo light MongoDB container 19 [OK]
  6. mongooseim/mongooseim-docker MongooseIM server the lates... 9 [OK]
  7. torusware/speedus-mongo Always updated official Mon... 9 [OK]
  8. jacksoncage/mongo Instant MongoDB sharded cluster 6 [OK]
  9. mongoclient/mongoclient Official docker image for M... 4 [OK]
  10. jadsonlourenco/mongo-rocks Percona Mongodb with Rocksd... 4 [OK]
  11. asteris/apache-php-mongo Apache2.4 + PHP + Mongo + m... 2 [OK]
  12. 19hz/mongo-container Mongodb replicaset for coreos 1 [OK]
  13. nitra/mongo Mongo3 centos7 1 [OK]
  14. ackee/mongo MongoDB with fixed Bluemix p... 1 [OK]
  15. kobotoolbox/mongo https://github.com/kobotoolb... 1 [OK]
  16. valtlfelipe/mongo Docker Image based on the la... 1 [OK]

这里我们拉取官方的镜像,标签为3.2

  1. test@docker:~/mongo$ docker pull mongo

等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为mongo,标签为3.2的镜像。

  1. test@docker:~/mongo$ docker images mongo
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. mongo latest 63c6b736e399 2 days ago 379MB

方法二、通过 Dockerfile 构建

创建Dockerfile

首先,创建目录mongo,用于存放后面的相关东西。

  1. test@docker:~$ mkdir -p ~/mongo ~/mongo/db

db目录将映射为mongo容器配置的/data/db目录,作为mongo数据的存储目录

进入创建的mongo目录,创建Dockerfile

  1. FROM debian:jessie-slim
  2. # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
  3. RUN groupadd -r mongodb && useradd -r -g mongodb mongodb
  4. RUN apt-get update \
  5. && apt-get install -y --no-install-recommends \
  6. ca-certificates \
  7. jq \
  8. numactl \
  9. && rm -rf /var/lib/apt/lists/*
  10. # grab gosu for easy step-down from root (https://github.com/tianon/gosu/releases)
  11. ENV GOSU_VERSION 1.10
  12. # grab "js-yaml" for parsing mongod's YAML config files (https://github.com/nodeca/js-yaml/releases)
  13. ENV JSYAML_VERSION 3.10.0
  14. RUN set -ex; \
  15. \
  16. apt-get update; \
  17. apt-get install -y --no-install-recommends \
  18. wget \
  19. ; \
  20. rm -rf /var/lib/apt/lists/*; \
  21. \
  22. dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \
  23. wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch"; \
  24. wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc"; \
  25. export GNUPGHOME="$(mktemp -d)"; \
  26. gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \
  27. gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \
  28. command -v gpgconf && gpgconf --kill all || :; \
  29. rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc; \
  30. chmod +x /usr/local/bin/gosu; \
  31. gosu nobody true; \
  32. \
  33. wget -O /js-yaml.js "https://github.com/nodeca/js-yaml/raw/${JSYAML_VERSION}/dist/js-yaml.js"; \
  34. # TODO some sort of download verification here
  35. \
  36. apt-get purge -y --auto-remove wget
  37. RUN mkdir /docker-entrypoint-initdb.d
  38. ENV GPG_KEYS \
  39. # pub 4096R/AAB2461C 2014-02-25 [expires: 2016-02-25]
  40. # Key fingerprint = DFFA 3DCF 326E 302C 4787 673A 01C4 E7FA AAB2 461C
  41. # uid MongoDB 2.6 Release Signing Key <packaging@mongodb.com>
  42. DFFA3DCF326E302C4787673A01C4E7FAAAB2461C \
  43. # pub 4096R/EA312927 2015-10-09 [expires: 2017-10-08]
  44. # Key fingerprint = 42F3 E95A 2C4F 0827 9C49 60AD D68F A50F EA31 2927
  45. # uid MongoDB 3.2 Release Signing Key <packaging@mongodb.com>
  46. 42F3E95A2C4F08279C4960ADD68FA50FEA312927
  47. # https://docs.mongodb.com/manual/tutorial/verify-mongodb-packages/#download-then-import-the-key-file
  48. RUN set -ex; \
  49. export GNUPGHOME="$(mktemp -d)"; \
  50. for key in $GPG_KEYS; do \
  51. gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
  52. done; \
  53. gpg --export $GPG_KEYS > /etc/apt/trusted.gpg.d/mongodb.gpg; \
  54. command -v gpgconf && gpgconf --kill all || :; \
  55. rm -r "$GNUPGHOME"; \
  56. apt-key list
  57. # Allow build-time overrides (eg. to build image with MongoDB Enterprise version)
  58. # Options for MONGO_PACKAGE: mongodb-org OR mongodb-enterprise
  59. # Options for MONGO_REPO: repo.mongodb.org OR repo.mongodb.com
  60. # Example: docker build --build-arg MONGO_PACKAGE=mongodb-enterprise --build-arg MONGO_REPO=repo.mongodb.com .
  61. ARG MONGO_PACKAGE=mongodb-org
  62. ARG MONGO_REPO=repo.mongodb.org
  63. ENV MONGO_PACKAGE=${MONGO_PACKAGE} MONGO_REPO=${MONGO_REPO}
  64. ENV MONGO_MAJOR 3.2
  65. ENV MONGO_VERSION 3.2.20
  66. RUN echo "deb http://$MONGO_REPO/apt/debian jessie/${MONGO_PACKAGE%-unstable}/$MONGO_MAJOR main" | tee "/etc/apt/sources.list.d/${MONGO_PACKAGE%-unstable}.list"
  67. RUN set -x \
  68. && apt-get update \
  69. && apt-get install -y \
  70. ${MONGO_PACKAGE}=$MONGO_VERSION \
  71. ${MONGO_PACKAGE}-server=$MONGO_VERSION \
  72. ${MONGO_PACKAGE}-shell=$MONGO_VERSION \
  73. ${MONGO_PACKAGE}-mongos=$MONGO_VERSION \
  74. ${MONGO_PACKAGE}-tools=$MONGO_VERSION \
  75. && rm -rf /var/lib/apt/lists/* \
  76. && rm -rf /var/lib/mongodb \
  77. && mv /etc/mongod.conf /etc/mongod.conf.orig
  78. RUN mkdir -p /data/db /data/configdb \
  79. && chown -R mongodb:mongodb /data/db /data/configdb
  80. VOLUME /data/db /data/configdb
  81. COPY docker-entrypoint.sh /usr/local/bin/
  82. RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat
  83. ENTRYPOINT ["docker-entrypoint.sh"]
  84. EXPOSE 27017
  85. CMD ["mongod"]

通过Dockerfile创建一个镜像,替换成你自己的名字

  1. test@docker:~/mongo$ docker build -t mongo:3.2 .

创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像

  1. test@docker:~/mongo$ docker images mongo:3.2
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. mongo 3.2 282fd552add6 9 days ago 336.1 MB

使用mongo镜像

运行容器

  1. test@docker:~/mongo$ docker run -p 27017:27017 -v $PWD/db:/data/db -d mongo:3.2
  2. cda8830cad5fe35e9c4aed037bbd5434b69b19bf2075c8626911e6ebb08cad51
  3. test@docker:~/mongo$

命令说明:

  • -p 27017:27017 :将容器的27017 端口映射到主机的27017 端口

  • -v $PWD/db:/data/db :将主机中当前目录下的db挂载到容器的/data/db,作为mongo数据存储目录

查看容器启动情况

  1. test@docker:~/mongo$ docker ps
  2. CONTAINER ID IMAGE COMMAND ... PORTS NAMES
  3. cda8830cad5f mongo:3.2 "/entrypoint.sh mongo" ... 0.0.0.0:27017->27017/tcp suspicious_goodall

使用mongo镜像执行mongo 命令连接到刚启动的容器,主机IP为172.17.0.1

  1. test@docker:~/mongo$ docker run -it mongo:3.2 mongo --host 172.17.0.1
  2. MongoDB shell version: 3.2.7
  3. connecting to: 172.17.0.1:27017/test
  4. Welcome to the MongoDB shell.
  5. For interactive help, type "help".
  6. For more comprehensive documentation, see
  7. http://docs.mongodb.org/
  8. Questions? Try the support group
  9. http://groups.google.com/group/mongodb-user
  10. >