# å¦ä½ä½¿ç¨ docker å建å¤æç mongodb é群
##### ä½è
ï¼[Sebastian Voss](https://twitter.com/sebastian_voss)
##### è¯è
ï¼[éµé](http://www.weibo.com/dysj4099)
***
æ¬ææè¿°äºå¦ä½ä½¿ç¨ docker å建 mongodb shard é群ãæ们å°ç´æ¥å±ç¤º mongodb é群ä¸åç»ä»¶ï¼å¯æ¬é replica sets ãé
ç½®æå¡å¨ config servers 以åèµæºè·¯ç± routes ï¼æå¯¹åº dockerfile æ件以å容å¨çå®ä¾åè¿ç¨ã
## å建Dockerfileæ件
é¦å
æ们éè¦å建两个 dockerfile ï¼ä¸ä¸ªæ¯ mongod èå¦ä¸ä¸ªæ¯ mongos ãå
æ¥ç mongod ï¼
```
FROM ubuntu:latest
# Add 10gen official apt source to the sources list
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/10gen.list
# Install MongoDB
RUN apt-get update
RUN apt-get install mongodb-10gen
# Create the MongoDB data directory
RUN mkdir -p /data/db
EXPOSE 27017
ENTRYPOINT ["usr/bin/mongod"]
```
为 mongos åå¤ç dockerfile å°±ç¸å½ç®åäºï¼å 为å®åºäºåé¢å好çéåã
```
FROM dev24/mongodb:latest
EXPOSE 27017
ENTRYPOINT ["usr/bin/mongos"]
```
请确ä¿ä½ å·²ç»å° dockerfile åå¨å¨å¯¹åºçä½ç½®äºï¼å¦ä¸ï¼
```
docker_mongodb_cluster
|
|-- mongod
| |-- Dockerfile
|
|-- mongos
|-- Dockerfile
```
åæ¢å° docker_mongodb_cluster è·¯å¾ä¸å¹¶æ§è¡ä»¥ä¸è¿äºå½ä»¤å建 docker éåï¼
```
sudo docker build \
-t dev24/mongodb mongod
sudo docker build \
-t dev24/mongodb mongos
```
> ##### è¯æ³¨ï¼æ¤å¤ä½è
çæä½æ¥éª¤æé®é¢ï¼ä¸é¢çä¸æ¡è¯å¥ä½¿ç¨ mongod/Dockerfile 建ç«äºå为 dev24/mongodb çéåï¼å½ä½¿ç¨ docker run è¿è¡è¯¥éåæ¶ï¼æ ¹æ® dockerfile å®ä¹ï¼ä¼é»è®¤å¯å¨ /usr/bin/mongod ãè第äºæ¡è¯å¥å¨ dev24/mongodb éåçåºç¡ä¸å°é»è®¤å¯å¨è®¾ç½®ä¸º /usr/bin/mongos 并å°æ°çæçéåå½å为 dev24/mongodb ä»èè¦çäºç¬¬ä¸ä¸ªéåãæ¬äººäº²æµå¨å¯å¨å¯æ¬éçæ¶åä¼åºéï¼å æ¤æ¬äººå¨æ¤å°ä¸è¿°ä¸¤å¥ä»£ç ä¿®æ¹ä¸ºä»¥ä¸çå½¢å¼ï¼çæ dev24/mongodb 以å dev24/mongos 两个éåï¼å¦æ读è
对æ¤æä¸åæè§ï¼å¯ä»¥çè¨æä¸è¯è
æ²éï¼é常æè°¢ã
```
sudo docker build \
-t dev24/mongodb mongod
#è¯è
æ´æ£
sudo docker build \
-t dev24/mongos mongos
```
## å建å¯æ¬éReplica Sets
é群ä¸ï¼æ¯ä¸ªåçé½éè¦å¯æ¬é以ä¿è¯æ°æ®å¯é æ§ãå¨è¿éæ们éè¦ä¸ºæ¯ä¸ªåçå建两个å¯æ¬ä»¥ææä¸ä¸ªèç¹çå¯æ¬éã以ä¸è¿äºå½ä»¤å°ä¼å®ä¾åä¸ä¸ª mongod 容å¨å¹¶ä¸å°ä»ä»¬ç»æå¯æ¬é rs1 ãåæ° --noprealloc å --smallfiles æ¯å¯é项ï¼ç¨æ¥é¿å
mongodb æ¶èæè¿å¤å
å(å¨æµè¯æ¶ååææ)ã
```
sudo docker run \
-P -name rs1_srv1 \
-d dev24/mongodb \
--replSet rs1 \
--noprealloc --smallfiles
sudo docker run \
-P -name rs1_srv2 \
-d dev24/mongodb \
--replSet rs1 \
--noprealloc --smallfiles
sudo docker run \
-P -name rs1_srv3 \
-d dev24/mongodb \
--replSet rs1 \
--noprealloc --smallfiles
```
å°å¯æ¬éçå称æ¿æ¢ä¸º rs2 ä¹åï¼æ们就è½å¤ç¨åæ ·çå½ä»¤å建第äºä¸ªå¯æ¬éäºã
```
sudo docker run \
-P -name rs2_srv1 \
-d dev24/mongodb \
--replSet rs2 \
--noprealloc --smallfiles
sudo docker run \
-P -name rs2_srv2 \
-d dev24/mongodb \
--replSet rs2 \
--noprealloc --smallfiles
sudo docker run \
-P -name rs2_srv3 \
-d dev24/mongodb \
--replSet rs2 \
--noprealloc --smallfiles
```
## åå§åå¯æ¬é
é¦å
请记ä¸ä¸æ¯ä¸ª docker 容å¨ç ip å°å以åç»å®ç«¯å£ã
```
sudo docker inspect rs1_srv1
sudo docker inspect rs1_srv2
...
```
### åå§åå¯æ¬é1
è¿æ¥å°è¿è¡äºå®¹å¨ rs1_srv1 ç mongodb (ä½ éè¦å°æ¬å°ç«¯å£ç»å®å° 27017/tcp ï¼å¦æçé®è¯·æ¥çåè¿°æ¥éª¤)ã
```
mongo --port