Browse Source

Add initial verison of MongoDB container files

master
Charles Reid 7 years ago
parent
commit
a3c523f5c1
  1. 30
      d-mongodb/Dockerfile.mongo
  2. 72
      d-mongodb/README.md
  3. 23
      d-mongodb/build_mongodb.sh
  4. 14
      d-mongodb/mongod.conf
  5. 54
      d-mongodb/run_mongodb.sh
  6. 31
      d-mongodb/scripts/first_run.sh
  7. 10
      d-mongodb/scripts/run.sh
  8. 4
      d-mongodb/test/quick_test.py
  9. 19
      d-mongodb/test/test_mongo_insert.py
  10. 15
      d-mongodb/test/test_mongo_read.py

30
d-mongodb/Dockerfile.mongo

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
FROM ubuntu
MAINTAINER charles@charlesreid1.com
# Install MongoDB 3.0
RUN DEBIAN_FRONTEND=noninteractive && \
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 && \
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.0.list && \
apt-get update && \
apt-get install -y mongodb-org=3.0.7 mongodb-org-server=3.0.7 mongodb-org-shell=3.0.7 mongodb-org-mongos=3.0.7 mongodb-org-tools=3.0.7 && \
apt-get install -y netcat && \
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Add scripts dir
ADD scripts /scripts
RUN chmod +x /scripts/*.sh
RUN touch /.firstrun
# Copy config file
COPY conf /etc/mongod.conf
# Command to run
ENTRYPOINT ["/scripts/run.sh"]
CMD [""]
# Expose listen port
EXPOSE 27017
# Expose our data volumes
VOLUME ["/data"]

72
d-mongodb/README.md

@ -0,0 +1,72 @@ @@ -0,0 +1,72 @@
# MongoDB Docker Service
This Dockerfile allows for running MongoDB in a Docker container.
## Usage
To build:
```
$ ./build_mongodb.sh
```
To run:
```
$ ./run_mongodb.sh
```
To change the IP address that MongoDB is bound to, you have two options:
* Bind to the IP address at the Docker level
* Bind to the IP address at the MongoDB level
To bind at the Docker level, modify the `docker run` command's `-p` flag.
Change this:
```
-p 27017:27017
```
to this:
```
-p <host-bind-ip-addr>:27017:27017
```
For example, if the MongoDB server is on a private subnet with IP address
10.6.0.2, the flag would be:
```
-p 10.6.0.2:27017:27017
```
To bind at the MongoDB level, edit the MongoDB config file `mongod.conf`
and change the following section:
```
net:
bindIp: 127.0.0.1
port: 27017
```
(If you change the port, you must also update the Dockerfile and the
`docker run` command.)
## Info
Data:
* MongoDB docker file mounts the local host directory `./mongodb-data/` to the container directory `/data/`
Config:
* The `mongod.conf` configuration file is used to configure MongoDB
* This configuration file is copied into the container at `/etc/mongod.conf`
Ports:
* By default, MongoDB listens internally on port 27017
* Container's port 27017 is mapped to host's port 27017
* To change the port, change it at the Docker level by editing the docker run command, or at the MongoDB level by changing the MongoDB configuration file
Networking:
* By default, MongoDB binds to 127.0.0.1 and will only listen for local requests
* To have MongoDB listen on the network, use the config file to bind it to a particular interface

23
d-mongodb/build_mongodb.sh

@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
#!/bin/bash
#
# Build the mongodb docker container using the Dockerfile
#
# https://charlesreid1.com/wiki/Docker/Dockerfiles
function usage {
echo ""
echo "build_mongodb.sh script:"
echo "builds a MongoDB docker container."
echo ""
echo " ./build_mongodb.sh"
echo ""
}
if [[ "$#" -ne 0 ]];
then
usage
else
docker build -t spymongo .
fi

14
d-mongodb/mongod.conf

@ -0,0 +1,14 @@ @@ -0,0 +1,14 @@
systemLog:
destination: file
path: "/data/mongod.log"
logAppend: true
storage:
journal:
enabled: true
processManagement:
fork: true
net:
bindIp: 127.0.0.1
port: 27017
setParameter:
enableLocalhostAuthBypass: false

54
d-mongodb/run_mongodb.sh

@ -0,0 +1,54 @@ @@ -0,0 +1,54 @@
#!/bin/bash
#
# Run the mongodb docker container
#
# Traffic via specified port.
# Mount volume being synced, too: mongodb-data
#
# http://charlesreid1.com/wiki/Docker/Basics
function usage {
echo ""
echo "run_mongodb.sh script:"
echo "run the mongodb docker container."
echo ""
echo " ./run_mongodb.sh"
echo ""
}
MONGODIR="${PWD}/mongodb-data"
INTERACTIVE=false
#INTERACTIVE=true
if [[ "$#" -ne 0 ]];
then
usage
else
if [ "$INTERACTIVE" == true ];
then
mkdir -p $MONGODIR
docker run \
--name philby_mongo \
-p 27017:27017 \
-v ${MONGODIR}:/data \
-ti spymongo \
/bin/bash
else
docker run \
--name philby_mongo \
-p 27017:27017 \
-v ${MONGODIR}:/data \
-d \
-ti spymongo
fi
fi

31
d-mongodb/scripts/first_run.sh

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
#!/bin/bash
USER=${MONGODB_USERNAME:-mongo}
#PASS=${MONGODB_PASSWORD:-$(pwgen -s -1 16)}
PASS=${MONGODB_PASSWORD:-password}
DB=${MONGODB_DBNAME:-admin}
if [ ! -z "$MONGODB_DBNAME" ]
then
ROLE=${MONGODB_ROLE:-dbOwner}
else
ROLE=${MONGODB_ROLE:-dbAdminAnyDatabase}
fi
# Start MongoDB service
/usr/bin/mongod --dbpath /data --nojournal &
while ! nc -vz localhost 27017; do sleep 1; done
# Create User
echo "Creating user: \"$USER\"... password: \"$PASS\""
/usr/bin/mongo $DB --eval "db.createUser({ user: '$USER', pwd: '$PASS', roles: [ { role: '$ROLE', db: '$DB' } ] });"
# Stop MongoDB service
/usr/bin/mongod --dbpath /data --shutdown
echo "========================================================================"
echo "MongoDB User: \"$USER\""
echo "MongoDB Password: \"$PASS\""
echo "MongoDB Database: \"$DB\""
echo "MongoDB Role: \"$ROLE\""
echo "========================================================================"
rm -f /.firstrun

10
d-mongodb/scripts/run.sh

@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
#!/bin/bash
# Initialize first run
if [[ -e /.firstrun ]]; then
/scripts/first_run.sh
fi
# Start MongoDB
echo "Starting MongoDB..."
/usr/bin/mongod --conf /etc/mongod.conf --dbpath /data

4
d-mongodb/test/quick_test.py

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
from pymongo import MongoClient
client = MongoClient('10.6.0.1', 27017)
db = client.test_database
print(db.collection_names())

19
d-mongodb/test/test_mongo_insert.py

@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
from pymongo import MongoClient
import random
from datetime import datetime
client = MongoClient('10.6.0.1', 27017)
db = client.test_database
collection = db.test_collection
docs = []
for i in range(20):
d = {
'timestamp' : datetime.now(),
'channel' : random.randint(1000,8000)
}
docs.append(d)
result = collection.insert_many(docs)
print(result.inserted_ids)

15
d-mongodb/test/test_mongo_read.py

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
from pymongo import MongoClient
"""
Test Read
Tests the ability to read records from the database.
"""
client = MongoClient('10.6.0.1', 27017)
db = client.test_database
collection = db.test_collection
for doc in collection.find():
print(doc)
Loading…
Cancel
Save