java 发表于 2017-12-27 15:59:35

Docker 安装 MongoDB

本帖最后由 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目录,创建DockerfileFROM debian:jessie-slim

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN groupadd -r mongodb && useradd -r -g mongodb mongodb

RUN apt-get update \
        && apt-get install -y --no-install-recommends \
                ca-certificates \
                jq \
                numactl \
        && rm -rf /var/lib/apt/lists/*

# grab gosu for easy step-down from root (https://github.com/tianon/gosu/releases)
ENV GOSU_VERSION 1.10
# grab "js-yaml" for parsing mongod's YAML config files (https://github.com/nodeca/js-yaml/releases)
ENV JSYAML_VERSION 3.10.0

RUN set -ex; \
        \
        apt-get update; \
        apt-get install -y --no-install-recommends \
                wget \
        ; \
        rm -rf /var/lib/apt/lists/*; \
        \
        dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \
        wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch"; \
        wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.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; \
        \
        wget -O /js-yaml.js "https://github.com/nodeca/js-yaml/raw/${JSYAML_VERSION}/dist/js-yaml.js"; \
# TODO some sort of download verification here
        \
        apt-get purge -y --auto-remove wget

RUN mkdir /docker-entrypoint-initdb.d

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

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

ENV MONGO_MAJOR 3.6
ENV MONGO_VERSION 3.6.1

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"

RUN set -x \
        && apt-get update \
        && apt-get install -y \
                ${MONGO_PACKAGE}=$MONGO_VERSION \
                ${MONGO_PACKAGE}-server=$MONGO_VERSION \
                ${MONGO_PACKAGE}-shell=$MONGO_VERSION \
                ${MONGO_PACKAGE}-mongos=$MONGO_VERSION \
                ${MONGO_PACKAGE}-tools=$MONGO_VERSION \
        && rm -rf /var/lib/apt/lists/* \
        && rm -rf /var/lib/mongodb \
        && mv /etc/mongod.conf /etc/mongod.conf.orig

RUN mkdir -p /data/db /data/configdb \
        && chown -R mongodb:mongodb /data/db /data/configdb
VOLUME /data/db /data/configdb

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

EXPOSE 27017
CMD ["mongod"]创建docker-entrypoint.sh#!/bin/bash
set -Eeuo pipefail

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

originalArgOne="$1"

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

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

      exec gosu mongodb "$BASH_SOURCE" "$@"
fi

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

# usage: file_env VAR
#    ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
#"$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
      local var="$1"
      local fileVar="${var}_FILE"
      local def="${2:-}"
      if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
                echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
                exit 1
      fi
      local val="$def"
      if [ "${!var:-}" ]; then
                val="${!var}"
      elif [ "${!fileVar:-}" ]; then
                val="$(< "${!fileVar}")"
      fi
      export "$var"="$val"
      unset "$fileVar"
}

# see https://github.com/docker-library/mongo/issues/147 (mongod is picky about duplicated arguments)
_mongod_hack_have_arg() {
      local checkArg="$1"; shift
      local arg
      for arg; do
                case "$arg" in
                        "$checkArg"|"$checkArg"=*)
                              return 0
                              ;;
                esac
      done
      return 1
}
# _mongod_hack_get_arg_val '--some-arg' "$@"
_mongod_hack_get_arg_val() {
      local checkArg="$1"; shift
      while [ "$#" -gt 0 ]; do
                local arg="$1"; shift
                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后加上下面一句话: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



页: [1]
查看完整版本: Docker 安装 MongoDB