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
Mike
  • 609
  • 12
  • 36
  • That looks like a standalone configuration, what did you do to make it into a replica set? – D. SM Mar 22 '21 at 22:32
  • 1
    The bitnami MongoDB will provide that for you. Everything you need is here: https://github.com/bitnami/bitnami-docker-mongodb – Mike Oct 06 '21 at 22:17

3 Answers3

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
Mike
  • 609
  • 12
  • 36
1

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
  • 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](/help/how-to-answer). – Community Apr 22 '23 at 04:13
  • doesn't work without keyfile – Vlad Jun 14 '23 at 18:14
  • @Vlad 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. – Mark Nelissen Jun 16 '23 at 07:06
  • it doesn't start because you didn't specify a key file but it is required now – Vlad Jun 16 '23 at 11:10
  • 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? – Mark Nelissen Jun 19 '23 at 10:14
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

Jonathan Bro
  • 77
  • 1
  • 7