课程表

Docker 安装和使用

Docker 实例

Docker 参考手册

工具箱
速查手册

Docker 安装 MongoDB

当前位置:免费教程 » 程序设计 » Docker

方法一、通过 Dockerfile 构建

创建Dockerfile

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

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

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

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

  1. FROM debian:wheezy
  2.  
  3. # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
  4. RUN groupadd -r mongodb && useradd -r -g mongodb mongodb
  5.  
  6. RUN apt-get update && apt-get install -y --no-install-recommends numactl && rm -rf /var/lib/apt/lists/*
  7.  
  8. # grab gosu for easy step-down from root
  9. ENV GOSU_VERSION 1.7
  10. RUN set -x && apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/* && wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)" && wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc" && export GNUPGHOME="$(mktemp -d)" && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 && gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu && rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc && chmod +x /usr/local/bin/gosu && gosu nobody true && apt-get purge -y --auto-remove ca-certificates wget
  11.  
  12. # gpg: key 7F0CEB10: public key "Richard Kreuter <richard@10gen.com>" imported
  13. RUN apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys 492EAFE8CD016A07919F1D2B9ECBEC467F0CEB10
  14.  
  15. ENV MONGO_MAJOR 3.0
  16. ENV MONGO_VERSION 3.0.12
  17.  
  18. RUN echo "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/$MONGO_MAJOR main" > /etc/apt/sources.list.d/mongodb-org.list
  19.  
  20. RUN set -x && apt-get update && apt-get install -y mongodb-org=$MONGO_VERSION mongodb-org-server=$MONGO_VERSION mongodb-org-shell=$MONGO_VERSION mongodb-org-mongos=$MONGO_VERSION mongodb-org-tools=$MONGO_VERSION && rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/mongodb && mv /etc/mongod.conf /etc/mongod.conf.orig
  21.  
  22. RUN mkdir -p /data/db /data/configdb && chown -R mongodb:mongodb /data/db /data/configdb
  23. VOLUME /data/db /data/configdb
  24.  
  25. COPY docker-entrypoint.sh /entrypoint.sh
  26. ENTRYPOINT ["/entrypoint.sh"]
  27.  
  28. EXPOSE 27017
  29. CMD ["mongod"]

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

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

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

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

方法二、docker pull mongo:3.2

查找Docker Hub上的mongo镜像

  1. w3xue@w3xue:~/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. w3xue@w3xue:~/mongo$ docker pull mongo:3.2

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


使用mongo镜像

运行容器

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

命令说明:

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

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

查看容器启动情况

  1. w3xue@w3xue:~/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. w3xue@w3xue:~/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. >
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号