replica Set mongo docker-compose
I had a similar issue and resolved it with the following compose file:
version: "3.8"services: mongo1: image: mongo:4.2 container_name: mongo1 command: ["--replSet", "my-replica-set", "--bind_ip_all", "--port", "30001"] volumes: - ./data/mongo-1:/data/db ports: - 30001:30001 healthcheck: test: test $$(echo "rs.initiate({_id:'my-replica-set',members:[{_id:0,host:\"mongo1:30001\"},{_id:1,host:\"mongo2:30002\"},{_id:2,host:\"mongo3:30003\"}]}).ok || rs.status().ok" | mongo --port 30001 --quiet) -eq 1 interval: 10s start_period: 30s mongo2: image: mongo:4.2 container_name: mongo2 command: ["--replSet", "my-replica-set", "--bind_ip_all", "--port", "30002"] volumes: - ./data/mongo-2:/data/db ports: - 30002:30002 mongo3: image: mongo:4.2 container_name: mongo3 command: ["--replSet", "my-replica-set", "--bind_ip_all", "--port", "30003"] volumes: - ./data/mongo-3:/data/db ports: - 30003:30003
with the following in my /etc/hosts
file:
127.0.0.1 mongo1127.0.0.1 mongo2127.0.0.1 mongo3
I documented it in a GitHub repo and with a little blog post here:
https://github.com/UpSync-Dev/docker-compose-mongo-replica-set
https://www.upsync.dev/2021/02/02/run-mongo-replica-set.html
Update: This does not work! You do need to run rs.initiate()
With MongoDB 4.0, you don't need a 4th container to run a setup script. It is really simple to bring up a replicaSet of 3 containers:
version: "3"services: mongo1: hostname: mongo1 container_name: localmongo1 image: mongo:4.0-xenial expose: - 27017 restart: always entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ] mongo2: hostname: mongo2 container_name: localmongo2 image: mongo:4.0-xenial expose: - 27017 restart: always entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ] mongo3: hostname: mongo3 container_name: localmongo3 image: mongo:4.0-xenial expose: - 27017 restart: always entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
More info here: https://github.com/msound/localmongo/tree/4.0
I would adivse you to have a look at khezen/mongo.
You can deploy a mongo replica set across a 3 nodes docker swarm with the following:
version: '3'services: replica1: image: khezen/mongo:slim deploy: mode: replicated replicas: 1 update_config: parallelism: 1 delay: 10s restart_policy: condition: on-failure palcement: node.hostname: node-1 environment: RS_NAME: shard1 SHARD_SVR: 'y' AUTH: 'y' volumes: - /data/mongo/replica1:/data/db networks: - mongo_cluster replica2: image: khezen/mongo:slim deploy: mode: replicated replicas: 1 update_config: parallelism: 1 delay: 10s restart_policy: condition: on-failure palcement: node.hostname: node-2 environment: RS_NAME: shard1 SHARD_SVR: 'y' AUTH: 'y' volumes: - /data/mongo/replica2:/data/db networks: - mongo_cluster replica3: image: khezen/mongo:slim deploy: mode: replicated replicas: 1 update_config: parallelism: 1 delay: 10s restart_policy: condition: on-failure palcement: node.hostname: node-3 environment: RS_NAME: shard1 SHARD_SVR: 'y' MASTER: replica3 SLAVES: replica1 replica2 AUTH: 'y' volumes: - /data/mongo/replica3:/data/db networks: - mongo_clusternetworks: mongo_cluster: driver: overlay
disclaimer: I am the maintainer of this image.