方法一、通過 Dockerfile 構(gòu)建
創(chuàng)建Dockerfile
首先,創(chuàng)建目錄mongo,用于存放后面的相關(guān)東西。
w3cschool@w3cschool:~$ mkdir -p ~/mongo ~/mongo/db
db目錄將映射為mongo容器配置的/data/db目錄,作為mongo數(shù)據(jù)的存儲目錄
進(jìn)入創(chuàng)建的mongo目錄,創(chuàng)建Dockerfile
FROM debian:wheezy # 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 \ numactl \ && rm -rf /var/lib/apt/lists/* # grab gosu for easy step-down from root ENV GOSU_VERSION 1.7 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 # gpg: key 7F0CEB10: public key "Richard Kreuter <richard@10gen.com>" imported RUN apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys 492EAFE8CD016A07919F1D2B9ECBEC467F0CEB10 ENV MONGO_MAJOR 3.0 ENV MONGO_VERSION 3.0.12 RUN echo "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/$MONGO_MAJOR main" > /etc/apt/sources.list.d/mongodb-org.list 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 RUN mkdir -p /data/db /data/configdb \ && chown -R mongodb:mongodb /data/db /data/configdb VOLUME /data/db /data/configdb COPY docker-entrypoint.sh /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] EXPOSE 27017 CMD ["mongod"]
通過Dockerfile創(chuàng)建一個(gè)鏡像,替換成你自己的名字
w3cschool@w3cschool:~/mongo$ docker build -t mongo:3.2 .
創(chuàng)建完成后,我們可以在本地的鏡像列表里查找到剛剛創(chuàng)建的鏡像
w3cschool@w3cschool:~/mongo$ docker images mongo:3.2 REPOSITORY TAG IMAGE ID CREATED SIZE mongo 3.2 282fd552add6 9 days ago 336.1 MB
方法二、docker pull mongo:3.2
查找Docker Hub上的mongo鏡像
w3cschool@w3cschool:~/mongo$ docker search mongo NAME DESCRIPTION STARS OFFICIAL AUTOMATED mongo MongoDB document databases ... 1989 [OK] mongo-express Web-based MongoDB admin int... 22 [OK] mvertes/alpine-mongo light MongoDB container 19 [OK] mongooseim/mongooseim-docker MongooseIM server the lates... 9 [OK] torusware/speedus-mongo Always updated official Mon... 9 [OK] jacksoncage/mongo Instant MongoDB sharded cluster 6 [OK] mongoclient/mongoclient Official docker image for M... 4 [OK] jadsonlourenco/mongo-rocks Percona Mongodb with Rocksd... 4 [OK] asteris/apache-php-mongo Apache2.4 + PHP + Mongo + m... 2 [OK] 19hz/mongo-container Mongodb replicaset for coreos 1 [OK] nitra/mongo Mongo3 centos7 1 [OK] ackee/mongo MongoDB with fixed Bluemix p... 1 [OK] kobotoolbox/mongo https://github.com/kobotoolb... 1 [OK] valtlfelipe/mongo Docker Image based on the la... 1 [OK]
這里我們拉取官方的鏡像,標(biāo)簽為3.2
w3cschool@w3cschool:~/mongo$ docker pull mongo:3.2
等待下載完成后,我們就可以在本地鏡像列表里查到REPOSITORY為mongo,標(biāo)簽為3.2的鏡像。
使用mongo鏡像
運(yùn)行容器
w3cschool@w3cschool:~/mongo$ docker run -p 27017:27017 -v $PWD/db:/data/db -d mongo:3.2 cda8830cad5fe35e9c4aed037bbd5434b69b19bf2075c8626911e6ebb08cad51 w3cschool@w3cschool:~/mongo$
命令說明:
-p 27017:27017 :將容器的27017 端口映射到主機(jī)的27017 端口
-v $PWD/db:/data/db :將主機(jī)中當(dāng)前目錄下的db掛載到容器的/data/db,作為mongo數(shù)據(jù)存儲目錄
查看容器啟動(dòng)情況
w3cschool@w3cschool:~/mongo$ docker ps CONTAINER ID IMAGE COMMAND ... PORTS NAMES cda8830cad5f mongo:3.2 "/entrypoint.sh mongo" ... 0.0.0.0:27017->27017/tcp suspicious_goodall
使用mongo鏡像執(zhí)行mongo 命令連接到剛啟動(dòng)的容器,主機(jī)IP為172.17.0.1
w3cschool@w3cschool:~/mongo$ docker run -it mongo:3.2 mongo --host 172.17.0.1 MongoDB shell version: 3.2.7 connecting to: 172.17.0.1:27017/test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user >
更多建議: