MongoDB is a scalable, high-performance open source document-oriented (Document-Oriented) database. It is developed in C ++, supports complex data types and a powerful query language, and provides most of the functions of a relational database. Because of its high performance, easy deployment, and easy use, MongoDB has been widely used in various fields.
① Download the file
Download the Mongodb image project from the GitHub Dockerpool community account:
[[email protected] ~] # git clone https://github.com/DockerPool/Mongodb.git
Cloning into 'Mongodb' ...
remote: Counting objects: 12, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 12 (delta 2), reused 12 (delta 2), pack-reused 0
Unpacking objects: 100% (12/12), done.
View the content, including the written Dockerfile and several scripts, etc .:
[[email protected] ~] # cd Mongodb /
[[email protected] Mongodb] # ls
Dockerfile mongodb-3.2.repo mongodb-linux-x86_64-rhel70-3.6.0.tgz pwgen-2.08.tar.gz run.sh set_mongodb_password.sh
② The content of Dockerfile is:
[[email protected] Mongodb] # cat Dockerfile
#Setting inherit from the sshd image we created earlier
FROM sshd: centos
MAINTAINER waitfish from dockerpool.com
COPY mongodb-linux-x86_64-rhel70-3.6.0.tgz /mongodb-linux-x86_64-rhel70-3.6.0.tgz
COPY pwgen-2.08.tar.gz /pwgen-2.08.tar.gz
RUN yum update -y && yum install -y gcc automake autoconf libtool make
RUN tar -xf pwgen-2.08.tar.gz
WORKDIR pwgen-2.08
RUN ./configure && make && make install
#Define working directory
WORKDIR /
RUN tar -xf mongodb-linux-x86_64-rhel70-3.6.0.tgz
RUN mv mongodb-linux-x86_64-rhel70-3.6.0 / usr / local / mongodb
#Setting environment variables
ENV PATH / usr / local / mongodb / bin: $ PATH
#Delete the archive
RUN rm -rf mongodb-linux-x86_64-rhel70-3.6.0.tgz pwgen-2.08.tar.gz
# Create a folder for mongodb to store data files
RUN mkdir -p / data / db
VOLUME / data / db
ENV AUTH yes
# Add run scripts
ADD run.sh /run.sh
ADD set_mongodb_password.sh /set_mongodb_password.sh
RUN chmod 755 ./*.sh
EXPOSE 27017
EXPOSE 28017
CMD ["/run.sh"]
③ The set_mongodb_password.sh script is mainly responsible for configuring the user name and password of the database. The content is:
[[email protected] Mongodb] # cat set_mongodb_password.sh
#This script mainly sets the database username and password
#! / bin / bash
#Determine if a password has been set
if [-f /.mongodb_password_set]; then
echo "MongoDB password already set!"
exit 0
fi
/ usr / local / mongodb / bin / mongod --smallfiles --nojournal &
PASS = $ {MONGODB_PASS:-$ (pwgen -s 12 1)}
_word = $ ([$ {MONGODB_PASS}] && echo "preset" || echo "random")
RET = 1
while [[RET -ne 0]]; do
echo "=> Waiting for confirmation of MongoDB service startup"
sleep 5
mongo admin --eval "help"> / dev / null 2> & 1
RET = $?
done
#You can see the following output through docker logs + id
echo "=> Creating an admin user with a $ {_ word} password in MongoDB"
mongo admin --eval "db.addUser ({user: 'admin', pwd: '$ PASS', roles: ['userAdminAnyDatabase', 'dbAdminAnyDatabase']});"
mongo admin --eval "db.shutdownServer ();"
echo "=> Done!"
touch /.mongodb_password_set
echo "==================================================== ========================= "
echo "You can now connect to this MongoDB server using:"
echo ""
echo "mongo admin -u admin -p $ PASS --host <host> --port <port>"
echo ""
echo "Please remember to change the above password as soon as possible!"
echo "==================================================== ========================= "
④ The run.sh script is the main startup script with the following content:
[[email protected] Mongodb] # cat run.sh
#! / bin / bash
if [! -f /.mongodb_password_set]; then
/set_mongodb_password.sh
fi
if ["$ AUTH" == "yes"]; then
export mongodb = '/ usr / local / mongodb / bin / mongod --nojournal --auth --httpinterface --rest'
else
export mongodb = '/ usr / local / mondodb / bin / mongod --nojournal --httpinterface --rest'
fi
if [! -f /data/db/mongod.lock]; then
eval $ mongodb
else
export mongodb = $ mongodb '--dbpath / data / db'
rm /data/db/mongod.lock
mongod --dbpath / data / db --repair && eval $ mongodb
fi
⑤ Create a mirror
Create an image mongodb: latest based on the Dockerfile:
[[email protected] Mongodb] # docker build -t mongodb.
[[email protected] ~] # docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mongodb latest adf9a26bb498 15 minutes ago 1.19 GB
...
⑥ Use demonstration:
Start the background container and map ports 27017,28017,22 to the local:
[[email protected] Mongodb] # docker run -d -p 27017: 27017 -p 28017: 28017 -p 37017: 22 mongodb
b38f560ba2b16a4e0c9c5b77c02e2e07b9f45eed8ce7de5dd2d6755d8d962050
[[email protected] Mongodb] # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b38f560ba2b1 mongodb "/run.sh" 4 seconds ago Up 3 seconds 0.0.0.0:27017->27017/tcp, 0.0.0.0:28017->28017/tcp, 0.0.0.0:37017->22/tcp trusting_pasteur
[[email protected] Mongodb] # docker logs b38f560ba2b1
...
======================================================== =======================
You can now connect to this MongoDB server using:
mongo admin -u admin -p NQ1VmsSWUwF1 --host <host> --port <port>
Please remember to change the above password as soon as possible!
======================================================== =======================
...
The NQ1VmsSWUwF1 in the output is the password of the admin user
You can also use environment variables to specify a password when the container starts:
[[email protected] Mongodb] # docker run -d -p 27017: 27017 = p 37017: 22 -e MONGODB_PASS = "mypass" mongodb
Furthermore, no password is required for setup:
[[emai
l protected] Mongodb] # docker run -d 27017: 27017 -p 28017: 28017 -e AUTH = no mongodb
Similarly, the reader can use the -v parameter to map local directories to containers.
⑦ Detailed startup parameters:
There are many startup parameters for Mongodb, including:
--quiet #Quiet output
--port arg #Specify the service port number, the default port 27017
--bind_ip arg #Bind service IP. If it is bound to 127.0.0.1, it can only be accessed by this machine. Do not specify the default local IP
--logpath arg #Specify Mongodb log files, note that the specified file is not a directory
--logappend #Write logs in append mode
--pidfilepath arg #PID File full path, if not set, no PID file
--keyFile arg #The full path of the cluster's private key, only valid for the Replica Set architecture
--unixSocketPrefix arg #UNIX domain socket replacement directory, (default is / tmp)
--fork #Run MongoDB as a daemon, create a server process
--auth #enable authentication
--cpu # Periodically display CPU utilization and iowait
--dbpath arg #Specify the database path
--dialog arg #diaglog option 0 = off 1 = w 2 = R 3 = both 7 = W + some reads
--directoryperdb #Set each database to be saved in a separate directory
--journal #Enable the logging option, MongoDB data operations will be written to the journal folder file
--journalOptions arg #Enable log diagnostic options
--ipv6 #Enable ipv6 option
--jsonp #Allow JSONP access via HTTP (with security implications)
--maxConns args #Maximum simultaneous connection default 2000
--noauth #Do not enable authentication
--nohttpinterface #Close the http interface, and access to port 27018 is disabled by default.
--noprealloc # Disable data file preallocation (often affecting performance)
--noscripting #Disable the scripting engine
--notablescan #Do not allow table scan
--noubixsocket #Disable Unix socket listening
--nssize arg (= 16) #Set the letter database .ns file size (MB)
--objcheck #After receiving customer data, check the validity
--profile arg #profile parameter 0 = off 1 = slow 2 = all
--quota #Limit the number of files per database, the default setting is 8
--quotaFiles arg #number of filess allower per db, requires --quota
--rest #Enable simple rest API
--repair #Repair all databases run repair on all dbs
--repairpath arg #Repair the directory of the files generated by the library, the default is the directory name dbpath
--slowms arg (= 100) #value of slow for profile and console log
--smallfiles #Use smaller default files
--syncdelay arg (= 60) #Data seconds written to disk (0 = never, not recommended)
--sysinfo # print some diagnostic system information
--upgrade #If you need to upgrade the database * Replication parameter
-------------------------------------------------- -------------------------------------------------
--fastsync #Enable the slave replication service from a dbpath. The dbpath database is a snapshot of the master database and can be used to quickly enable synchronization.
--autoresync #Automatic resynchronization if synchronizing data from slave to master is much worse
--oplogSize arg #Set the size of the oplog (MB) * master / slave parameter
-------------------------------------------------- -------------------------------------------------
--master #Main library mode
--slave #From library mode
--source arg #from library port number
--only arg #Specify a single database copy
--slavedelay arg #Set the delay time to synchronize the master with the slave * Replica set option
-------------------------------------------------- ------------------------------------------------
--relSet arg #Set the replica set name * Sharding (sharding) option
-------------------------------------------------- ------------------------------------------------
--configsvr #Declares that this is a cluster config service, the default port is 27019, and the default directory is / data / configdb
--shardsvr #Declares that this is a cluster shard, the default port is 27018
--noMoveParanoia #Turn off paranoid saving for moveChunk data
The above parameters can also be configured directly in the mongod.conf configuration file, for example:
dbpath = / data / mongodb
logpath = /data/mongodb/mongodb.log
logappend = true
fork = true
auth = true
[Series 8] Creating a Centos Docker image with MongoDB using a Dockerfile