docker mongo for single (primary node only) replica set (for development)?
Asked Answered
B

3

1

This is the portion of the dockerfile that has served us well to date. However, now I need to convert this to be a single node replica set (for transactions to work). I don't want any secondary or arbiter - just the primary node. What am I missing to get this working?

mongo:
  image: mongo:4.4.3
  container_name: mongo
  restart: unless-stopped
  environment:
    MONGO_INITDB_ROOT_USERNAME: root
    MONGO_INITDB_ROOT_PASSWORD: myPass
  command: mongod --port 27017
  ports:
    - '27017:27017'
  volumes:
    - ./data/mongodb:/data/db
    - ./data/mongodb/home:/home/mongodb/
    - ./configs/mongodb/mongo-init.sh:/docker-entrypoint-initdb.d/mongo-init.sh:ro
Buckbuckaroo answered 22/3, 2021 at 22:12 Comment(2)
That looks like a standalone configuration, what did you do to make it into a replica set?Aguish
The bitnami MongoDB will provide that for you. Everything you need is here: github.com/bitnami/bitnami-docker-mongodbBuckbuckaroo
B
3

Got it working. I inserted the following into the block in my question above:

hostname: mongodb
volumes:
  - ./data/mongodb/data/log/:/var/log/mongodb/
# the healthcheck avoids the need to initiate the replica set
healthcheck:
  test: test $$(echo "rs.initiate().ok || rs.status().ok" | mongo -u root -p imagiaRoot --quiet) -eq 1
  interval: 10s
  start_period: 30s
Buckbuckaroo answered 23/3, 2021 at 1:5 Comment(0)
V
2

Updated answer for Mongo 6 (which drops mongo for mongosh, see https://www.mongodb.com/docs/mongodb-shell/#the-mdb-shell-versus-the-legacy-mongo-shell).

hostname: mongodb
image: mongo:latest
restart: unless-stopped
command: ["--replSet", "rs0", "--bind_ip_all"]
volumes:
  - mongodb-data:/data/db
healthcheck:
  test: 'test $$(mongosh --eval "rs.initiate().ok || rs.status().ok" --quiet) -eq 1'
  interval: 10s
  start_period: 30s
Vescuso answered 18/4, 2023 at 16:29 Comment(5)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Ash
doesn't work without keyfileSesquicentennial
@Sesquicentennial I don't remember having done anything with a keyfile. Can you explain? This is basically the most upvoted answer in this thread, but updated with the new cli.Vescuso
it doesn't start because you didn't specify a key file but it is required nowSesquicentennial
Starting with which version of mongo? I tested it with 6.0.6, and he doesn't complain. At which point does he request the key file? This is for a dev database, without security, might that influence the requirement?Vescuso
D
0

I was unable to initiate the replica set via the healthcheck. I used the bash script below instead. For Windows users, be sure to call your DB with the name of your computer. For example: mongodb://DESKTOP-QPRKMN2:27017

run-test.sh

#!/bin/bash

echo "Running docker-compose"
docker-compose up -d

echo "Waiting for DB to initialize"
sleep 10

echo "Initiating DB"
docker exec mongo_container mongo --eval "rs.initiate();"

echo "Running tests"

# test result
if go test ./... -v
then
  echo "Test PASSED"
else
  echo "Test FAILED"
fi

# cleanup
docker-compose -f docker-compose.test.yml down

docker-compose file

version: '3.8'
services:
  mongo:
    hostname: $HOST
    container_name: mongo_container
    image: mongo:5.0.3
    volumes:
      - ./test-db.d
    expose:
      - 27017
    ports:
      - "27017:27017"
    restart: always
    command: ["--replSet", "test", "--bind_ip_all"]

This forum post was very helpful: https://www.mongodb.com/community/forums/t/docker-compose-replicasets-getaddrinfo-enotfound/14301/4

Diehard answered 16/5, 2022 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.