Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 3144|回复: 0
打印 上一主题 下一主题

Docker 安装 MongoDB

[复制链接]

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
跳转到指定楼层
楼主
发表于 2017-12-27 15:59:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 java 于 2017-12-27 17:09 编辑

http://www.runoob.com/docker/docker-install-mongodb.html

创建Dockerfile
首先,创建目录mongo,用于存放后面的相关东西。
runoob@runoob:~$ 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.         rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc; \
  29.         chmod +x /usr/local/bin/gosu; \
  30.         gosu nobody true; \
  31.         \
  32.         wget -O /js-yaml.js "https://github.com/nodeca/js-yaml/raw/${JSYAML_VERSION}/dist/js-yaml.js"; \
  33. # TODO some sort of download verification here
  34.         \
  35.         apt-get purge -y --auto-remove wget

  36. RUN mkdir /docker-entrypoint-initdb.d

  37. ENV GPG_KEYS \
  38. # pub   4096R/91FA4AD5 2016-12-14 [expires: 2018-12-14]
  39. #       Key fingerprint = 2930 ADAE 8CAF 5059 EE73  BB4B 5871 2A22 91FA 4AD5
  40. # uid                  MongoDB 3.6 Release Signing Key <packaging@mongodb.com>
  41.         2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
  42. # https://docs.mongodb.com/manual/tutorial/verify-mongodb-packages/#download-then-import-the-key-file
  43. RUN set -ex; \
  44.         export GNUPGHOME="$(mktemp -d)"; \
  45.         for key in $GPG_KEYS; do \
  46.                 gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
  47.         done; \
  48.         gpg --export $GPG_KEYS > /etc/apt/trusted.gpg.d/mongodb.gpg; \
  49.         rm -r "$GNUPGHOME"; \
  50.         apt-key list

  51. # Allow build-time overrides (eg. to build image with MongoDB Enterprise version)
  52. # Options for MONGO_PACKAGE: mongodb-org OR mongodb-enterprise
  53. # Options for MONGO_REPO: repo.mongodb.org OR repo.mongodb.com
  54. # Example: docker build --build-arg MONGO_PACKAGE=mongodb-enterprise --build-arg MONGO_REPO=repo.mongodb.com .
  55. ARG MONGO_PACKAGE=mongodb-org
  56. ARG MONGO_REPO=repo.mongodb.org
  57. ENV MONGO_PACKAGE=${MONGO_PACKAGE} MONGO_REPO=${MONGO_REPO}

  58. ENV MONGO_MAJOR 3.6
  59. ENV MONGO_VERSION 3.6.1

  60. 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"

  61. RUN set -x \
  62.         && apt-get update \
  63.         && apt-get install -y \
  64.                 ${MONGO_PACKAGE}=$MONGO_VERSION \
  65.                 ${MONGO_PACKAGE}-server=$MONGO_VERSION \
  66.                 ${MONGO_PACKAGE}-shell=$MONGO_VERSION \
  67.                 ${MONGO_PACKAGE}-mongos=$MONGO_VERSION \
  68.                 ${MONGO_PACKAGE}-tools=$MONGO_VERSION \
  69.         && rm -rf /var/lib/apt/lists/* \
  70.         && rm -rf /var/lib/mongodb \
  71.         && mv /etc/mongod.conf /etc/mongod.conf.orig

  72. RUN mkdir -p /data/db /data/configdb \
  73.         && chown -R mongodb:mongodb /data/db /data/configdb
  74. VOLUME /data/db /data/configdb

  75. COPY docker-entrypoint.sh /usr/local/bin/
  76. ENTRYPOINT ["docker-entrypoint.sh"]

  77. EXPOSE 27017
  78. CMD ["mongod"]
复制代码
创建docker-entrypoint.sh
  1. #!/bin/bash
  2. set -Eeuo pipefail

  3. if [ "${1:0:1}" = '-' ]; then
  4.         set -- mongod "$@"
  5. fi

  6. originalArgOne="$1"

  7. # allow the container to be started with `--user`
  8. # all mongo* commands should be dropped to the correct user
  9. if [[ "$originalArgOne" == mongo* ]] && [ "$(id -u)" = '0' ]; then
  10.         if [ "$originalArgOne" = 'mongod' ]; then
  11.                 chown -R mongodb /data/configdb /data/db
  12.         fi

  13.         # make sure we can write to stdout and stderr as "mongodb"
  14.         # (for our "initdb" code later; see "--logpath" below)
  15.         chown --dereference mongodb "/proc/$/fd/1" "/proc/$/fd/2" || :
  16.         # ignore errors thanks to https://github.com/docker-library/mongo/issues/149

  17.         exec gosu mongodb "$BASH_SOURCE" "$@"
  18. fi

  19. # you should use numactl to start your mongod instances, including the config servers, mongos instances, and any clients.
  20. # https://docs.mongodb.com/manual/administration/production-notes/#configuring-numa-on-linux
  21. if [[ "$originalArgOne" == mongo* ]]; then
  22.         numa='numactl --interleave=all'
  23.         if $numa true &> /dev/null; then
  24.                 set -- $numa "$@"
  25.         fi
  26. fi

  27. # usage: file_env VAR [DEFAULT]
  28. #    ie: file_env 'XYZ_DB_PASSWORD' 'example'
  29. # (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
  30. #  "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
  31. file_env() {
  32.         local var="$1"
  33.         local fileVar="${var}_FILE"
  34.         local def="${2:-}"
  35.         if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
  36.                 echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
  37.                 exit 1
  38.         fi
  39.         local val="$def"
  40.         if [ "${!var:-}" ]; then
  41.                 val="${!var}"
  42.         elif [ "${!fileVar:-}" ]; then
  43.                 val="$(< "${!fileVar}")"
  44.         fi
  45.         export "$var"="$val"
  46.         unset "$fileVar"
  47. }

  48. # see https://github.com/docker-library/mongo/issues/147 (mongod is picky about duplicated arguments)
  49. _mongod_hack_have_arg() {
  50.         local checkArg="$1"; shift
  51.         local arg
  52.         for arg; do
  53.                 case "$arg" in
  54.                         "$checkArg"|"$checkArg"=*)
  55.                                 return 0
  56.                                 ;;
  57.                 esac
  58.         done
  59.         return 1
  60. }
  61. # _mongod_hack_get_arg_val '--some-arg' "$@"
  62. _mongod_hack_get_arg_val() {
  63.         local checkArg="$1"; shift
  64.         while [ "$#" -gt 0 ]; do
  65.                 local arg="$1"; shift
  66.                 case "$arg" in<blockquote>出现如下错误:
复制代码
https://github.com/docker-library/mongo/blob/master/3.6/

通过Dockerfile创建一个镜像,替换成你自己的名字
runoob@runoob:~/mongo$ docker build -t mongo:3.6 .

出现如下错误:
docker: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "exec: \"docker-entrypoint.sh\": executable file not found in $PATH".
文件Dockerfile的COPY后加上下面一句话:
  1. RUN chmod +x /usr/local/bin/docker-entrypoint.sh
复制代码
使用mongo镜像
运行容器

docker run --name mongo -p 27017:27017 -v $PWD/db:/data/db -d mongo:3.6

命令说明:
-p 27017:27017 :将容器的27017 端口映射到主机的27017 端口
-v $PWD/db:/data/db :将主机中当前目录下的db挂载到容器的/data/db,作为mongo数据存储目录


验证:
进入docker容器
docker exec -it mongo bash
登录本机mongo
创建myTest数据库及表user
use myTest;
db.createCollection('user');
插入一条记录
db.user.insert({_id:1,name:"test"});
查看表中记录
db.user.find();
退出
exit



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-11-26 01:48 , Processed in 0.057238 second(s), 19 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表