Skip to content

Instantly share code, notes, and snippets.

@SteveBate
Created May 27, 2021 12:18
Show Gist options
  • Save SteveBate/909c890412cc2bce690620077eb7809a to your computer and use it in GitHub Desktop.
Save SteveBate/909c890412cc2bce690620077eb7809a to your computer and use it in GitHub Desktop.
Docker and MongoDb

create a docker data container to share data with mongodb containers

  • docker create -v /data/db --name dbstore mongo bin/true

create a standard container that uses the previously created data container (--volumes-from)

  • docker run -d -p 30001:27017 --name mgodb --volumes-from dbstore mongo mongod

or if we want to run as a replica set first create a network so that all instances can talk to each other:

  • docker create network my-mongo-cluster

then create containers executing mongod with replSet parameter each with their own data container

  • docker run -d -p 30001:27017 --volumes-from dbstore1 --net my-mongo-cluster --name mongo1 mongo mongod --replSet my-mongo-set
  • docker run -d -p 30002:27017 --volumes-from dbstore2 --net my-mongo-cluster --name mongo2 mongo mongod --replSet my-mongo-set
  • docker run -d -p 30003:27017 --volumes-from dbstore3 --net my-mongo-cluster --name mongo3 mongo mongod --replSet my-mongo-set

you conserve space by basing the data container and the app container on the same image

to backup data from the data container volume use mongodump as nomral - no special docker commands required

  • mongodump --host localhost --port 30001 --db test --out documents/mongodata
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment