简体   繁体   English

docker mongo 用于单个(仅主节点)副本集(用于开发)?

[英]docker mongo for single (primary node only) replica set (for development)?

This is the portion of the dockerfile that has served us well to date.这是迄今为止为我们提供良好服务的 dockerfile 部分。 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

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

I was unable to initiate the replica set via the healthcheck .我无法通过healthcheck启动副本集。 I used the bash script below instead.我改用了下面的 bash 脚本。 For Windows users , be sure to call your DB with the name of your computer.对于Windows 用户,请务必使用您的计算机名称调用您的数据库。 For example: mongodb://DESKTOP-QPRKMN2:27017例如: mongodb://DESKTOP-QPRKMN2:27017

run-test.sh运行-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 docker-compose文件

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这个论坛帖子非常有帮助: https://www.mongodb.com/community/forums/t/docker-compose-replicasets-getaddrinfo-enotfound/14301/4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM