MongoDB Docker and Raspberry Pi

MongoDB Docker and Raspberry Pi

1 Set up MongoDB in Docker
Simple Make file
IMAGE=xxxxxxt/public
TAG=xxxxx-mongodb
NAME=xxxxx-mongodb


docker-context:

build: docker-context
docker build -t $(IMAGE):$(TAG) .

run-init:
docker run -d -p 27017:27017 -p 28017:28017 -v /mnt/driver1/data/mongodb:/var/lib/mongodb -v /opt/mongodb/log:/var/log/mongodb -e RUNNING_MODULE=INIT --name $(NAME) $(IMAGE):$(TAG)

run:
docker run -d -p 27017:27017 -p 28017:28017 -v /mnt/driver1/data/mongodb:/var/lib/mongodb -v /opt/mongodb/log:/var/log/mongodb -e RUNNING_MODULE=RUN --name $(NAME) $(IMAGE):$(TAG)

debug:
docker run -ti -p 27017:27017 -p 28017:28017 -v /mnt/driver1/data/mongodb:/var/lib/mongodb -v /opt/mongodb/log:/var/log/mongodb --name $(NAME) $(IMAGE):$(TAG) /bin/bash

clean:
docker stop ${NAME}
docker rm ${NAME}

logs:
docker logs ${NAME}

publish:
docker push ${IMAGE}:${TAG}

fetch:
docker pull ${IMAGE}:${TAG}

Docker file is also simple, because it is an apt-get install
#Set up FTP in Docker

#Prepre the OS
FROM resin/rpi-raspbian:jessie
MAINTAINER Carl Luo <[email protected]>

ENV DEBIAN_FRONTEND noninteractive
ENV DAEMONUSER root
RUN echo "deb http://mirrordirector.raspbian.org/raspbian/ jessie main contrib non-free rpi\n\
deb-src http://archive.raspbian.org/raspbian/ jessie main contrib non-free rpi\n\
" > /etc/apt/sources.list
RUN apt-get -y update
RUN apt-get -y dist-upgrade


#install mongod soft
RUN apt-get -y install mongodb-server

#configure the server
ADD      conf/mongodb.conf  /etc/
ADD      conf/mongodb_auth.conf /etc/mongodb_auth.conf
ADD      conf/mongodb_noauth.conf /etc/mongodb_noauth.conf


#start the application
EXPOSE   27017 28017
RUN     mkdir -p /app/
ADD     start.sh /app/
WORKDIR /app/
CMD [ "./start.sh" ]

For the configuration mongoldb.conf, I only change the auth and bind_ip
bind_ip = 0.0.0.0
port = 27017

The flow will be system start mongo, add admin user, enable auth and restart the mongoDB
start.sh is as follow:
#!/bin/sh -ex


if [ "$RUNNING_MODULE" = "INIT" ]; then
    #start non auth server
    /etc/init.d/mongodb start

    #sleep 2 minutes
    sleep 2m

    # add user
    mongo admin --eval "db.addUser('supper', ‘password')"

    # set auth and restart
    cp /etc/mongodb_auth.conf /etc/mongodb.conf
    /etc/init.d/mongodb restart
else
    # set auth
    cp /etc/mongodb_auth.conf /etc/mongodb.conf
  
    # start server
    /etc/init.d/mongodb start

fi

# Run mongo as the running process, this is required to keep the docker process running
tail -f /dev/null

Check mongoDB AUTH
>
show dbs
admin 0.0625GB
local 0.03125GB
mydb 0.0625GB
test 0.0625GB

2 Switch to use admin DB
> use admin
switched to db admin

check if there is any users in DB
> db.system.users.find();

add user in admin
> db.addUser("super","password")

Verify auth
> db.auth("super","password")
1

Login to the auth user with command
> mongo --host sillycat.ddns.net --port 27017 -usuper -ppassword --authenticationDatabase admin

It may required username and password with params
-u username -p password

Connect to my server
> mongo --host sillycat.ddns.net --port 27017

Choose database
> use mydb;

New/Save/Query data
> j = {name:"mongo"};
> db.things.save(j);
> db.things.find();

dump command all the database
> mongodump -h sillycat.ddns.net -o ~/data/mongodb/

restore all the database
> mongorestore -h sillycat.ddns.net ~/data/mongodb/ --drop

References:
mongoDB
http://yannickloriot.com/2016/04/install-mongodb-and-node-js-on-a-raspberry-pi/
http://blog.51yip.com/nosql/1573.html
http://www.runoob.com/mongodb/mongodb-mongodump-mongorerstore.html

redis
http://andreas-kongelstad.tumblr.com/post/51622770030/part-2-installing-redis-on-raspberry-pi

mongoDB AUTH
http://bubkoo.com/2014/02/07/mongodb-authentication/
http://stackoverflow.com/questions/10743905/how-can-i-use-a-script-to-create-users-in-mongodb
http://stackoverflow.com/questions/22682891/create-a-mongodb-user-from-commandline

你可能感兴趣的:(MongoDB Docker and Raspberry Pi)