Mongo docker official site https://hub.docker.com/_/mongo/
Use the included file to build your own docker image and copy the mongod.conf into this image. You may also add your own initial scripts into the folder docker-entrypoint-initdb.d to create the sample database every time you start the container. To persist the data, please refer the command below to use the mounted local storage.
docker login -u [yourdockerhubaccount] --password-stdin
Build image with tag, don't forgot . as the current docker file folder. You run the command where the Dockerfile is.
docker build -t <hub-user>/<repo-name>[:<tag>] --build-arg MONGO_INITDB_ROOT_USERNAME=root --build-arg MONGO_INITDB_ROOT_PASSWORD=example .
docker build -t lujasper/devops:mongo --build-arg MONGO_INITDB_ROOT_USERNAME=root --build-arg MONGO_INITDB_ROOT_PASSWORD=example .
docker push lujasper/devops:mongo
Run Mongo with local data storage, which will persist even the container is purged. The folders /var/data and /var/configdb need to be mapped to your local folders that must be created before you run the command, for example c:\devops\mongo\data and c:\devops\mongo\configdb.
For windows, check Docker mounting in windows
docker run -d --rm -v c:/devops/mongo/data/db:/data/db -v c:/devops/mongo/data/configdb:/data/configdb -p 27017:27017 lujasper/devops:mongo
above command may not work in windows environment due to imcompatible file mode, use docker volumn to fix it. The drawback is that data is stored inside Docker not local driver. Please find the section at the end of this document on how to extract the volume.
you may need to remove existing one before create a new volumn. docker system prune. docker volume rm -f --name=mongodata.
docker volume create --name=mongodata
docker rm mongodb
docker run -d --rm -v mongodata:/data/db -p 27017:27017 --name mongodb lujasper/devops:mongo
docker ps
docker exec -ti mongodb bash
mongo localhost/mydb
Enable security via mongodb.conf file. Let's add it to the image(path: /etc/mongo/mongod.conf) in the Dockerfile.
\# mongod.conf file
security:
authorization: enabled
docker volume create --name=mongodata
Run without config file first and set up admin account first; You may create database and correspoding user by this way;
docker rm mymongodb
docker run -v mongodata:/data/db -p 27017:27017 --name mymongodb -ti --rm lujasper/devops:mongo
docker ps #open another termina and check whether mymongodb is running or not.
docker exec -ti mymongodb bash
root@aa01ffdb6f25:/# mongo mongodb://localhost:27017
>use admin
>db.createUser(
{
user: "useradmin",
pwd: "admin123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
docker rm mymongodb
docker run -v mongodata:/data/db -p 27017:27017 -ti --rm --name mymongodb lujasper/devops:mongo --config /etc/mongo/mongod.conf
docker rm mymongodb
docker run -v /usr/jlu/mongo/data/db:/data/db -v /usr/jlu/mongo/data/dbconfig:/data/dbconfig -p 27017:27017 --name mymongodb -ti --rm lujasper/devops:mongo
docker ps #open another termina and check whether mymongodb is running or not.
docker exec -ti mymongodb bash
root@aa01ffdb6f25:/# mongo mongodb://localhost:27017
>use admin
>db.createUser(
{
user: "useradmin",
pwd: "admin123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
docker rm mymongodb
docker run -v /usr/jlu/mongo/data/db:/data/db -v /usr/jlu/mongo/data/dbconfig:/data/dbconfig -p 27017:27017 -ti --rm --name mymongodb lujasper/devops:mongo --config /etc/mongo/mongod.conf
Sharing Directories using Volumes In the "Backup, restore, or migrate data volumes" section you have:
BACKUP:
sudo docker run --rm --volumes-from DATA -v $(pwd):/backup busybox tar cvf /backup/backup.tar /data
--rm: remove the container when it exits
--volumes-from DATA: attach to the volumes shared by the DATA container
-v $(pwd):/backup: bind mount the current directory into the container; to write the tar file to
busybox: a small simpler image - good for quick maintenance
tar cvf /backup/backup.tar /data: creates an uncompressed tar file of all the files in the /data directory
RESTORE:
$ sudo docker create -v /data --name DATA2 busybox true
$ sudo docker run --rm --volumes-from DATA2 -v $(pwd):/backup busybox tar xvf /backup/backup.tar
data/
data/sven.txt
$ sudo docker run --rm --volumes-from DATA -v `pwd`:/backup busybox ls /data
sven.txt
