MongoDB container image which can be linked to other containers.
3.6K
docker pull romeoz/docker-mongodb
Alternately you can build the image yourself.
git clone https://github.com/romeoz/docker-mongodb.git
cd docker-mongodb
docker build -t="$USER/mongodb" .
Run mongodb container:
docker run --name mongodb -d romeoz/docker-mongodb
The simplest way to login to the app container is to use the docker exec command to attach a new process to the running container.
docker exec -it mongodb mongo
You can customize the launch command of MongoDB server by specifying arguments to mongod on the docker run command. For example the following command for use "WiredTiger" storage engine:
docker run --name mongodb -d \
romeoz/docker-mongodb --storageEngine wiredTiger
For data persistence a volume should be mounted at /var/lib/mongodb.
SELinux users are also required to change the security context of the mount point so that it plays nicely with selinux.
mkdir -p /to/path/data
sudo chcon -Rt svirt_sandbox_file_t /to/path/data
The updated run command looks like this.
docker run --name mongodb -d \
-v /host/to/path/data:/var/lib/mongodb \
romeoz/docker-mongodb
This will make sure that the data stored in the database is not lost when the container is stopped and started again.
The backup is made over mongodump.
Create a temporary container for backup:
docker run -it --rm \
--net mongo_net \
-e 'MONGO_MODE=backup' -e 'MONGO_HOST=mongodb' \
-v host/to/path/backup:/tmp/backup \
romeoz/docker-mongodb
Archive will be available in the /host/to/path/backup.
Algorithm: one backup per week (total 4), one backup per month (total 12) and the last backup. Example:
backup.last.tar.tar.gz,backup.1.tar.tar.gzand/backup.dec.tar.tar.gz.
You can disable the rotation by using env SPHINX_ROTATE_BACKUP=false.
Also you can set command-line arguments for mongodump:
docker run -it --rm \
--net mongo_net \
-e 'MONGO_MODE=backup' \
-v host/to/path/backup:/tmp/backup \
romeoz/docker-mongodb --host mongodb -u backuper -p pass
As the check-parameter is used a collection name COLLECTION_NAME. If necessary, you can specify a database name DB_NAME.
docker run -it --rm \
-e 'MONGO_CHECK=default' \
-e 'COLLECTION_NAME=foo' \
-v /host/to/path/backup:/tmp/backup \
romeoz/docker-mongodb
Default used the /tmp/backup/backup.last.tar.gz.
The backup is made over mongoresore.
docker run --name mongodb -d \
-e 'MONGO_RESTORE=default' \
-v /host/to/path/backup:/tmp/backup \
romeoz/docker-mongodb
You can set command-line arguments for mongod:
docker run --name mongodb -d \
-e 'MONGO_RESTORE=default' \
-v /host/to/path/backup:/tmp/backup \
romeoz/docker-mongodb --storageEngine wiredTiger
Create replica set:
docker network create mongo_net
docker run --name node_1 -d --net mongo_net romeoz/docker-mongodb --replSet "rs"
docker run --name node_2 -d --net mongo_net romeoz/docker-mongodb --replSet "rs"
docker run --name node_3 -d --net mongo_net romeoz/docker-mongodb --replSet "rs"
Configure replica set:
docker exec -i node_1 mongo <<EOF
rs.initiate()
rs.add("node_2:27017")
rs.add("node_3:27017")
EOF
Docker will assign an auto-generated hostname for containers and by default MongoDB will use this hostname while initializing the replica set. As we need to access the nodes of the replica set from outside the container we will use IP adresses instead.
Use these MongoDB shell commands to change the hostname to the IP address:
docker exec -i node_1 mongo <<EOF
cfg = rs.conf()
cfg.members[0].host = "node_1:27017"
rs.reconfig(cfg)
rs.status()
EOF
More information you can see here.
Create a shards:
docker network create mongo_net
docker run --name node_1 -d --net mongo_net romeoz/docker-mongodb
docker run --name node_2 -d --net mongo_net romeoz/docker-mongodb
Configure a Sharded Cluster:
# Create a Config Server
docker run --name cnf -d \
--net mongo_net
romeoz/docker-mongodb \
--port 27017 --configsvr
Create a Router (mongos):
docker run --name mongos -d \
--net mongo_net
-e 'MONGO_MODE=mongos' \
romeoz/docker-mongodb \
--configdb cnf:27017
Configure a Router (mongos):
docker exec -i mongos mongo <<EOF
sh.addShard("node_1:27017")
sh.addShard("node_2:27017")
sh.status()
EOF
More information you can see here.
MONGO_BACKUP_DIR: Set a specific backup directory (default "/tmp/backup").
MONGO_BACKUP_FILENAME: Set a specific filename backup (default "backup.last.tar.gz").
MONGO_MODE: Set a specific mode. Takes on the values backup or mongos.
MONGO_RESTORE: Defines name of backup-file to initialize the database. Note that the scripts must be inside the container, so you may need to mount them. You can specify as default that is equivalent to the /tmp/backup/backup.last.tar.gz
RESTORE_OPTS: Defines options of mongorestore.
MONGO_CHECK: Defines name of backup-file to initialize the database. Note that the dump must be inside the container, so you may need to mount them. You can specify as default that is equivalent to the /tmp/backup/backup.last.tar.gz
MONGO_ROTATE_BACKUP: Determines whether to use the rotation of backups (default "true").
DB_NAME: Set a specific name of database for checking (default "test")
COLLECTION_NAME: Set a specific name of collection for checking
All the logs are forwarded to stdout and sterr. You have use the command docker logs.
docker logs mongodb
####Split the logs
You can then simply split the stdout & stderr of the container by piping the separate streams and send them to files:
docker logs mongodb > stdout.log 2>stderr.log
cat stdout.log
cat stderr.log
or split stdout and error to host stdout:
docker logs mongodb > -
docker logs mongodb 2> -
####Rotate logs
Create the file /etc/logrotate.d/docker-containers with the following text inside:
/var/lib/docker/containers/*/*.log {
rotate 31
daily
nocompress
missingok
notifempty
copytruncate
}
Optionally, you can replace
nocompresstocompressand change the number of days.
MongoDB docker image is open-sourced software licensed under the MIT license.
Content type
Image
Digest
sha256:4bbb25203…
Size
136.1 MB
Last updated
almost 8 years ago
docker pull romeoz/docker-mongodb