A simple docker compose file to run a mongodb database in docker with replica set on.
- Install Docker and docker compose if you don't have them already.
- Clone the repository:
git clone https://github.com/bipicar/mongors.git && cd mongors
- Copy default configuration to .env file:
cp .env.example .env
- Apply the configuration you want to the .env file:
RS
-> The replicas set name (Default: rs0)HOST
-> The replica set host (Default: 127.0.0.1)PORT
-> The replica set port (Default: 27017)DATA
-> The folder where the database information will be stored (Default: ./data/db)
- Start the container
docker compose up -d
- Initialize the replica set (Only the first time you run the container)
docker exec -it rs0 mongosh # This will open the mongo shell
rs.initiate({
_id: "rs0",
members: [{ _id: 0, host: "127.0.0.1:27017" }]
}) // This will initialize the replica set
rs.status() // This will show the replica set status
exit // This will exit the mongo shell
If you are updating from a previous version, you will need to remove the old container and create a new one. To do so, follow these steps:
- Stop the container
docker compose down
- Remove the container
docker rm rs0
- Check mongodb version in
compose.yml
file and update it if needed - Start the container
docker compose up -d