├── Dockerfile ├── README.md ├── conf ├── mongod_1.conf ├── mongod_2.conf └── mongod_3.conf └── start.sh /Dockerfile: -------------------------------------------------------------------------------- 1 | # 2 | # MongoDB Dockerfile 3 | # 4 | # https://github.com/dockerfile/mongodb 5 | # 6 | 7 | # Pull base image. 8 | FROM dockerfile/ubuntu 9 | 10 | # Install MongoDB. 11 | RUN \ 12 | apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 && \ 13 | echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' > /etc/apt/sources.list.d/mongodb.list && \ 14 | apt-get update && \ 15 | apt-get install -y mongodb-org && \ 16 | rm -rf /var/lib/apt/lists/* 17 | 18 | # Define mountable directories. 19 | VOLUME ["/data/db"] 20 | 21 | # Define working directory. 22 | WORKDIR / 23 | 24 | # Expose ports for each Mongo replica set instance 25 | EXPOSE 27017 26 | EXPOSE 27018 27 | EXPOSE 27019 28 | 29 | # Copy required files over to container 30 | COPY conf/ /conf/ 31 | COPY start.sh /start.sh 32 | 33 | # Run start shell when container launches 34 | CMD ["sh", "start.sh"] 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MongoDB ReplicaSet Dockerfile 2 | ============================= 3 | 4 | A Dockerfile and config for launching a MongoDB contatiner with 3 mongo processes that act as a ReplicaSet. There are config files for each mongo instance that are copied to the container. The shell script handles creation of the data directories and starting mongo. 5 | 6 | ### Notes 7 | 8 | It's probably not a good idea to run this setup in production as each mongo instance should be split across different machines. However for a local development environment this fits our needs. 9 | 10 | ### Base Docker Image 11 | 12 | * [dockerfile/ubuntu](http://dockerfile.github.io/#/ubuntu) 13 | 14 | 15 | ### Installation 16 | 17 | 1. Install [Docker](https://www.docker.com/). 18 | 2. `docker build -t="inlight/mongodb-replica-set" github.com/inlight-media/docker-mongodb-replica-set` 19 | 20 | ### Usage 21 | 22 | Refer to [Dockerfile MongoDB](https://github.com/dockerfile/mongodb) for usage notes not specific to ReplicaSet 23 | 24 | #### Create container from image and open ports for ReplicaSet 25 | 26 | docker run -itd -p 27017:27017 -p 27018:27018 -p 27019:27019 --name mongodb inlight/mongodb-replica-set 27 | 28 | #### Initiate ReplicaSet 29 | 30 | Once the container is running you can initialize the ReplicaSet with the following steps: 31 | 32 | * Access shell of container with `docker exec -it mongodb bash` 33 | * Access mongo shell with `mongo` 34 | * `rs.initiate()` to initiate ReplicaSet 35 | * Use the output of previous step to grab the 'me' (hostname of the machine) property to add to ReplicaSet: 36 | * port 27018: `rs.add('9488f884u84:27018')` 37 | * port 27019: `rs.add('9488f884u84:27019')` 38 | * You can use `rs.status()` to see if your ReplicaSet has started successfully 39 | -------------------------------------------------------------------------------- /conf/mongod_1.conf: -------------------------------------------------------------------------------- 1 | # mongod.conf 2 | 3 | # Where to store the data. 4 | 5 | # Note: if you run mongodb as a non-root user (recommended) you may 6 | # need to create and set permissions for this directory manually, 7 | # e.g., if the parent directory isn't mutable by the mongodb user. 8 | dbpath=/data/db/db-001 9 | 10 | #where to log 11 | logpath=/var/log/mongodb/mongodb-db-001.log 12 | 13 | # process id 14 | fork = true 15 | pidfilepath = /var/run/mongodb-db-001.pid 16 | 17 | logappend=true 18 | 19 | port=27017 20 | smallfiles=true 21 | 22 | # Listen to local interface only. Comment out to listen on all interfaces. 23 | # bind_ip = 127.0.0.1 24 | 25 | # Disables write-ahead journaling 26 | # nojournal = true 27 | 28 | # Enables periodic logging of CPU utilization and I/O wait 29 | #cpu = true 30 | 31 | # Turn on/off security. Off is currently the default 32 | #noauth = true 33 | #auth = true 34 | 35 | # Verbose logging output. 36 | #verbose = true 37 | 38 | # Inspect all client data for validity on receipt (useful for 39 | # developing drivers) 40 | #objcheck = true 41 | 42 | # Enable db quota management 43 | #quota = true 44 | 45 | # Set oplogging level where n is 46 | # 0=off (default) 47 | # 1=W 48 | # 2=R 49 | # 3=both 50 | # 7=W+some reads 51 | #diaglog = 0 52 | 53 | # Ignore query hints 54 | #nohints = true 55 | 56 | # Enable the HTTP interface (Defaults to port 28017). 57 | #httpinterface = true 58 | 59 | # Turns off server-side scripting. This will result in greatly limited 60 | # functionality 61 | #noscripting = true 62 | 63 | # Turns off table scans. Any query that would do a table scan fails. 64 | #notablescan = true 65 | 66 | # Disable data file preallocation. 67 | #noprealloc = true 68 | 69 | # Specify .ns file size for new databases. 70 | # nssize = 71 | 72 | # Replication Options 73 | 74 | # in replicated mongo databases, specify the replica set name here 75 | replSet=dbReplicaSet 76 | # maximum size in megabytes for replication operation log 77 | #oplogSize=1024 78 | # path to a key file storing authentication info for connections 79 | # between replica set members 80 | #keyFile=/path/to/keyfile -------------------------------------------------------------------------------- /conf/mongod_2.conf: -------------------------------------------------------------------------------- 1 | # mongod.conf 2 | 3 | # Where to store the data. 4 | 5 | # Note: if you run mongodb as a non-root user (recommended) you may 6 | # need to create and set permissions for this directory manually, 7 | # e.g., if the parent directory isn't mutable by the mongodb user. 8 | dbpath=/data/db/db-002 9 | 10 | #where to log 11 | logpath=/var/log/mongodb/mongodb-db-002.log 12 | 13 | # process id 14 | fork = true 15 | pidfilepath = /var/run/mongodb-db-002.pid 16 | 17 | logappend=true 18 | 19 | port=27018 20 | smallfiles=true 21 | 22 | # Listen to local interface only. Comment out to listen on all interfaces. 23 | # bind_ip = 127.0.0.1 24 | 25 | # Disables write-ahead journaling 26 | # nojournal = true 27 | 28 | # Enables periodic logging of CPU utilization and I/O wait 29 | #cpu = true 30 | 31 | # Turn on/off security. Off is currently the default 32 | #noauth = true 33 | #auth = true 34 | 35 | # Verbose logging output. 36 | #verbose = true 37 | 38 | # Inspect all client data for validity on receipt (useful for 39 | # developing drivers) 40 | #objcheck = true 41 | 42 | # Enable db quota management 43 | #quota = true 44 | 45 | # Set oplogging level where n is 46 | # 0=off (default) 47 | # 1=W 48 | # 2=R 49 | # 3=both 50 | # 7=W+some reads 51 | #diaglog = 0 52 | 53 | # Ignore query hints 54 | #nohints = true 55 | 56 | # Enable the HTTP interface (Defaults to port 28017). 57 | #httpinterface = true 58 | 59 | # Turns off server-side scripting. This will result in greatly limited 60 | # functionality 61 | #noscripting = true 62 | 63 | # Turns off table scans. Any query that would do a table scan fails. 64 | #notablescan = true 65 | 66 | # Disable data file preallocation. 67 | #noprealloc = true 68 | 69 | # Specify .ns file size for new databases. 70 | # nssize = 71 | 72 | # Replication Options 73 | 74 | # in replicated mongo databases, specify the replica set name here 75 | replSet=dbReplicaSet 76 | # maximum size in megabytes for replication operation log 77 | #oplogSize=1024 78 | # path to a key file storing authentication info for connections 79 | # between replica set members 80 | #keyFile=/path/to/keyfile -------------------------------------------------------------------------------- /conf/mongod_3.conf: -------------------------------------------------------------------------------- 1 | # mongod.conf 2 | 3 | # Where to store the data. 4 | 5 | # Note: if you run mongodb as a non-root user (recommended) you may 6 | # need to create and set permissions for this directory manually, 7 | # e.g., if the parent directory isn't mutable by the mongodb user. 8 | dbpath=/data/db/db-003 9 | 10 | #where to log 11 | logpath=/var/log/mongodb/mongodb-db-003.log 12 | 13 | # process id 14 | fork = true 15 | pidfilepath = /var/run/mongodb-db-003.pid 16 | 17 | logappend=true 18 | 19 | port=27019 20 | smallfiles=true 21 | 22 | # Listen to local interface only. Comment out to listen on all interfaces. 23 | # bind_ip = 127.0.0.1 24 | 25 | # Disables write-ahead journaling 26 | # nojournal = true 27 | 28 | # Enables periodic logging of CPU utilization and I/O wait 29 | #cpu = true 30 | 31 | # Turn on/off security. Off is currently the default 32 | #noauth = true 33 | #auth = true 34 | 35 | # Verbose logging output. 36 | #verbose = true 37 | 38 | # Inspect all client data for validity on receipt (useful for 39 | # developing drivers) 40 | #objcheck = true 41 | 42 | # Enable db quota management 43 | #quota = true 44 | 45 | # Set oplogging level where n is 46 | # 0=off (default) 47 | # 1=W 48 | # 2=R 49 | # 3=both 50 | # 7=W+some reads 51 | #diaglog = 0 52 | 53 | # Ignore query hints 54 | #nohints = true 55 | 56 | # Enable the HTTP interface (Defaults to port 28017). 57 | #httpinterface = true 58 | 59 | # Turns off server-side scripting. This will result in greatly limited 60 | # functionality 61 | #noscripting = true 62 | 63 | # Turns off table scans. Any query that would do a table scan fails. 64 | #notablescan = true 65 | 66 | # Disable data file preallocation. 67 | #noprealloc = true 68 | 69 | # Specify .ns file size for new databases. 70 | # nssize = 71 | 72 | # Replication Options 73 | 74 | # in replicated mongo databases, specify the replica set name here 75 | replSet=dbReplicaSet 76 | # maximum size in megabytes for replication operation log 77 | #oplogSize=1024 78 | # path to a key file storing authentication info for connections 79 | # between replica set members 80 | #keyFile=/path/to/keyfile -------------------------------------------------------------------------------- /start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Create database directories 4 | mkdir /data/db/db-001 5 | mkdir /data/db/db-002 6 | mkdir /data/db/db-003 7 | 8 | # Run mongo replica sets using config files 9 | mongod --config /conf/mongod_1.conf 10 | mongod --config /conf/mongod_2.conf 11 | mongod --config /conf/mongod_3.conf 12 | 13 | # Run mongo as the running process, this is required to keep the docker process running 14 | mongo --------------------------------------------------------------------------------