Docker 安装 MongoDB
Docker 安装 MongoDB
方法一、docker pull mongo
查找 Docker Hub 上的 mongo 镜像:
docker search mongo
这里我们拉取官方的镜像 mongo:3.2:
docker pull mongo:3.2
等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORY 为 mongo,标签为 3.2 的镜像:
docker images | grep mongo
方法二、通过 Dockerfile 构建
创建Dockerfile
首先,创建目录 mongo,用于存放后面的相关东西:
mkdir -p ./mongo ./mongo/db
db目录将映射为 mongo 容器配置的 /data/db 目录,作为 mongo 数据的存储目录
进入创建的 mongo 目录,创建 Dockerfile
FROM 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; \ command -v gpgconf && gpgconf --kill all || :; \ 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/AAB2461C 2014-02-25 [expires: 2016-02-25] # Key fingerprint = DFFA 3DCF 326E 302C 4787 673A 01C4 E7FA AAB2 461C # uid MongoDB 2.6 Release Signing Key <packaging@mongodb.com> DFFA3DCF326E302C4787673A01C4E7FAAAB2461C \ # pub 4096R/EA312927 2015-10-09 [expires: 2017-10-08] # Key fingerprint = 42F3 E95A 2C4F 0827 9C49 60AD D68F A50F EA31 2927 # uid MongoDB 3.2 Release Signing Key <packaging@mongodb.com> 42F3E95A2C4F08279C4960ADD68FA50FEA312927 # 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; \ command -v gpgconf && gpgconf --kill all || :; \ 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.2 ENV MONGO_VERSION 3.2.20 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/ RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat ENTRYPOINT ["docker-entrypoint.sh"] EXPOSE 27017 CMD ["mongod"]
通过Dockerfile创建一个镜像,替换成你自己的名字:
docker build -t mongo:3.2 .
使用 mongo 镜像
运行镜像,生成容器
使用 docker run 命令运行镜像 mongo:3.2:
docker run --name my-mongo -p 27017:27017 -v $PWD/db:/data/db -d mongo:3.2
命令说明:
-p 27017:27017 :将主机的27017 端口映射到容器的27017 端口
-v $PWD/db:/data/db :将主机中当前目录下的db挂载到容器的/data/db,作为mongo数据存储目录
查看容器的运行情况:
docker ps
使用 docker exec 命令访问容器 my-mongo:
docker exec -it my-mongo /bin/bash
mongo status
mongo
show dbs;
使用 docker run 命令运行镜像 mongo:latest:
docker run --name my-mongo-latest -p 27017:27017 -v $PWD/db:/data/db -d mongo:latest
使用 docker exec 命令访问容器 my-mongo-latest:
docker exec -it my-mongo-latest /bin/bash
mongo status
show dbs;
目前 Docker Hub 上 mongo:latest 对应的 版本是 4.0.10
PS:
参考:https://www.runoob.com/docker/docker-install-mongodb.html