Skip to content

Instantly share code, notes, and snippets.

@guillecro
Created November 16, 2021 15:19
Show Gist options
  • Save guillecro/cc7333c04377f3c47bf0490755fbebb8 to your computer and use it in GitHub Desktop.
Save guillecro/cc7333c04377f3c47bf0490755fbebb8 to your computer and use it in GitHub Desktop.
If you need to install multiple mongo (or any, like Maria) databases in the same OS

Sometimes you might need multiple instances of mongo or mysql installed. Instead of installing them locally, just user the dockerimage

Having the databases on docker containers gives me flexibility in swaping databases pretty easily.

Before starting, to persist the databases, we need to store the volumes somewhere. I like to make a "databases" folder in my home folder, but you can put them wherever you want. Just make sure to persist them

Example: Installing mongo3.6 & mongo3.2
docker run -d --name mongo3.6 -p 27017:27017 -v ~/databases/mongo3.6:/data/db mongo:3.6
# Docker downloads mongo:3.6
# Docker runs mongo.
docker stop mongo3.6
# Next time you can start the container by doing: docker start mongo3.6

Same thing for mongo3.2 (make sure you are not running the mongo3.6 container

docker run -d --name mongo3.2 -p 27017:27017 -v ~/databases/mongo3.2:/data/db mongo:3.2
# Docker downloads mongo:3.2
# Docker runs mongo.
docker stop mongo3.2
# Next time you can start the container by doing: docker start mongo3.2

If you want multiple instances of mongo running at the same time just make sure to define a different port in your pc.

This same thing could work for databases like MariaDB

docker run -d --name maria -p 3306:3306 -v ~/databases/maria:/var/lib/mysql mariadb:10.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment