Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

how to run mongodb replica set in docker compose

Writer Matthew Martinez

I have tried to run mongodb replicaSet in local with mongoldb-community in my Mac I follow mongodb doc I can run it by this command

mongod --port 27017 --dbpath /usr/local/var/mongodb --replSet rs0 --bind_ip localhost,127.0.0.1

but it doesn't run on background, so every time I want to start replica set mongodb I should run that command, before I run it I should stop mongo first, then on the next tab console I should run mongo --eval "rs.initiate()" to create to replicaSet again

here is my docker compose:

version: "3.7"
services: mongodb_container: image: mongo:latest ports: - 27017:27017 volumes: - mongodb_data_container:/data/db
volumes: mongodb_data_container:

how to convert that into docker-compose ? is it possible ?

can I do docker exec CONTAINER_ID [commands] ? to run command mongo like above , but must stop the mongodb run in that docker ?

1 Answer

You can have a mongodb replica-set with this docker-compose services:

mongodb-primary: image: "bitnami/mongodb:4.2" user: root volumes: - ./mongodb-persistence/bitnami:/bitnami networks: - parse_network environment: - MONGODB_REPLICA_SET_MODE=primary - MONGODB_REPLICA_SET_KEY=123456789 - MONGODB_ROOT_USERNAME=admin-123 - MONGODB_ROOT_PASSWORD=password-123 - MONGODB_USERNAME=admin-123 - MONGODB_PASSWORD=password-123 - MONGODB_DATABASE=my_database ports: - 27017:27017 mongodb-secondary: image: "bitnami/mongodb:4.2" depends_on: - mongodb-primary environment: - MONGODB_REPLICA_SET_MODE=secondary - MONGODB_REPLICA_SET_KEY=123456789 - MONGODB_PRIMARY_HOST=mongodb-primary - MONGODB_PRIMARY_PORT_NUMBER=27017 - MONGODB_PRIMARY_ROOT_USERNAME=admin-123 - MONGODB_PRIMARY_ROOT_PASSWORD=password-123 networks: - parse_network ports: - 27027:27017 mongodb-arbiter: image: "bitnami/mongodb:4.2" depends_on: - mongodb-primary environment: - MONGODB_ADVERTISED_HOSTNAME=mongodb-arbiter - MONGODB_REPLICA_SET_MODE=arbiter - MONGODB_PRIMARY_HOST=mongodb-primary - MONGODB_PRIMARY_PORT_NUMBER=27017 - MONGODB_PRIMARY_ROOT_PASSWORD=password-123 - MONGODB_REPLICA_SET_KEY=123456789 networks: - parse_network ports: - 27037:27017
networks: parse_network: driver: bridge ipam: driver: default
volumes: mongodb_master_data: driver: local
19

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.