Mongodb+dockerdocker Configuring a MongoDB replica set

Source: Internet
Author: User
Tags curl install mongodb mkdir mongodb
Dockerfile
#base images from
ubuntu:latest
maintainer yao.3.li@uconn.edu

#install mongodb
RUN apt-get Update
RUN apt-get-y Install Curl
Run Curl-o https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz
run TAR-ZXVF mongodb-linux-x86_ 64-3.0.6.tgz
run MV mongodb-linux-x86_64-3.0.6//usr/local/mongodb
Run export path=/usr/local/mongodb/bin:$ PATH
ADD./run.sh/run.sh
run chmod 777/run.sh

#database directory
run mkdir-p/data/db
run Mkdir-p/data/log
Run touch/data/log/r.log
run mkdir-p/data/config

#port
EXPOSE 27017

entrypoint ["/run.sh"]

DOCKER-COMPOSE.YML:
Version: ' 3.2 '
services:
  primary:
    image:mongodb
    ports:
      -"27017:27017"
    Environment:
      -Role=prime
    volumes:
      -./config/r0.conf:/data/config/c.conf
      -./runprime.sh:/runprime.sh


  Secondary1:
    image:mongodb
    ports:
      -"27018:27017"
    Environment:
       -Role=secondary
    Volumes:
      -./config/r1.conf:/data/config/c.conf

  Secondary2:
    image:mongodb
    ports:
      -" 27019:27017 "
    Environment:
       -role=secondary
    volumes:
      -./config/r1.conf:/data/config/c.conf

R0.conf and r1.conf are temporarily the same:
#数据文件夹
dbpath=/data/db

#日志文件夹, you must specify LogPath Logpath=/data/log/r.log if you are running in a future mode

#以追加而非覆盖方式写入日志
logappend=true

#端口
port=27017

#以后台方式运行  
Run.sh:
#!/bin/bash
/usr/local/mongodb/bin/mongod-f/data/config/c.conf--replset "Rs0"

Runprime.sh:
#!/bin/bash
MONGO Localhost:27017/admin conf.js
Conf.js:
config={_id: "Rs0", 
	members:[{_id:0,host: "PRIMARY", priority:1}, 
				{_id:1,host: "Secondary1", priority:0}, 
				{_id:2,host: "Secondary2", priority:0}]
		}

Rs.initiate (config)
rs.conf ()

rs.add ("Secondary1")
rs.add ("Secondary2")

Run the process: first build the Image:
Docker Build-t MongoDB.
To start the container:
Docker-compose up-d
Log in to the main library, use the script configuration, or run runprime.sh to configure the replication set:
./runprime.sh

You can then verify by logging in:
MONGO--port 27017


The prompt has a primary, indicating that the configuration was successful. Of course, you can write further in the main library, read from the library, but need to be used from the library:
Db.setslaveok ()

To read.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.