- Create a docker compose file with that name docker-compose.yml along with the content below:
services:
mongo:
container_name: mongo
image: mongo:6.0
command: ["--replSet", "konbit-rs", "--bind_ip_all"]
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=
- MONGO_INITDB_ROOT_PASSWORD=
ports:
- 27017:27017
volumes:
- data_vol:/data/db
- configdb_vol:/data/configdb
- Run docker compose command to start mongodb container in either one of these modes:
Foreground mode for debugging purpose or first time glance and live log output
or Daemon mode for normal and regular operation
#will start the mongodb container in foreground docker compose up
#will start mongodb container in background docker compose up -d
- Use mongosh in order to connect to your local mongodb instance
Setup your replica once and for all with these instructions by typing them on your your mongosh session prompt:
mongosh
Check your replica statusrs.initiate()
rs.status()
Now your Mongodb setup is over and is ready to be used
Use this mongodb URI below either in your application or Mongodb Compass to be able to connect successfuly to this local replica:
mongodb://127.0.0.1:27017/?directConnection=true