Mongo Express在Docker Compose中等待MongoDB集群

dldeef67  于 2023-04-20  发布在  Docker
关注(0)|答案(1)|浏览(125)

我正在尝试使用Docker Compose设置一个本地开发,该开发使用MongoDB集群作为数据库。我选择Mongo Express作为数据库管理用户界面,以便我可以检查MongoDB数据库内部。集群接受连接确实需要一些时间,我将3 db容器作为depends_on的一部分,但似乎我必须做更多的基于Docker Compose文档here.我似乎找不到一个很好的例子等待MongoDB集群.有人已经想通了吗?请分享,那太好了。2先谢谢你!
下面是docker-compose.yml文件:

version: '3.9'

services:
  mongodb-primary:
    image: 'bitnami/mongodb:latest'
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=mongodb-primary
      - MONGODB_REPLICA_SET_MODE=primary
      - MONGODB_ROOT_PASSWORD=password
      - MONGODB_REPLICA_SET_KEY=replicasetkey
    volumes:
      - 'mongodb_master_data:/bitnami'

  mongodb-secondary:
    image: 'bitnami/mongodb:latest'
    depends_on:
      - mongodb-primary
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=mongodb-secondary
      - MONGODB_REPLICA_SET_MODE=secondary
      - MONGODB_INITIAL_PRIMARY_HOST=mongodb-primary
      - MONGODB_INITIAL_PRIMARY_PORT_NUMBER=27017
      - MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD=password
      - MONGODB_REPLICA_SET_KEY=replicasetkey

  mongodb-arbiter:
    image: 'bitnami/mongodb:latest'
    depends_on:
      - mongodb-primary
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=mongodb-arbiter
      - MONGODB_REPLICA_SET_MODE=arbiter
      - MONGODB_INITIAL_PRIMARY_HOST=mongodb-primary
      - MONGODB_INITIAL_PRIMARY_PORT_NUMBER=27017
      - MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD=password
      - MONGODB_REPLICA_SET_KEY=replicasetkey

  dbadmin:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    depends_on:
      - mongodb-primary
      - mongodb-secondary
      - mongodb-arbiter
    environment:
      ME_CONFIG_MONGODB_URL: mongodb://root:password@mongodb-primary:27017,mongodb-secondary:27017,mongodb-arbiter:27017?replicaSet=replicaset
      ME_CONFIG_BASICAUTH_USERNAME: admin
      ME_CONFIG_BASICAUTH_PASSWORD: mexpress

volumes:
  mongodb_master_data:
    driver: local
7d7tgy0s

7d7tgy0s1#

如果仍然相关:我建议为您尝试连接的mongodb(可能是主节点)添加healthcheck。示例:

services:
  mongo:
    image: mongo
    restart: always
    healthcheck:
      test: echo 'db.runCommand({serverStatus:1}).ok' | mongo admin -u $MONGO_INITDB_ROOT_USERNAME -p $MONGO_INITDB_ROOT_PASSWORD --quiet | grep 1
      interval: 10s
      timeout: 10s
      retries: 3
      start_period: 20s
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

相关问题