Skip to content

Can't connect to Mongo instance after initialization of replica set #249

Closed

Description

@myashchenko

I use mongo:latest image. My docker-compose.yml looks like this:

version: '3.3'

services:
  java-app:
    image: java-app:latest
    depends_on:
      - elasticsearch
      - mongo
    environment:
      - WAIT_HOSTS=elasticsearch:9200,mongo:27017
    ports:
      - "8080:8080"

  elasticsearch:
    image: elasticsearch:latest
    command: elasticsearch

  mongo_connector:
    build:
      ./mongo-connector
    depends_on:
      - mongo
      - elasticsearch
    environment:
      - WAIT_HOSTS=elasticsearch:9200,mongo:27017

  mongo:
    image: mongo:latest
    ports:
      - "27017:27017"

In this case, java-app is able to connect to mongo container. But I need to run mongo with --replSet argument to get mongo-connector working, so I've added this argument using command option:

mongo:
  image: mongo:latest
  command: ["mongod", "--replSet", "rs0"]
  ports:
    - "27017:27017"

After that my application is still able to connect to the instance, but mongo-connector doesn't work since replica set is not initiated. To initiate replica set, I've changed command:

  mongo:
    image: mongo:latest
    command: ["sh", "-c", "mongod --replSet rs0 && mongo admin --eval 'rs.initiate();'"]
    ports:
      - "27017:27017"

I see the following message in output:

mongo_1  | 2018-03-03T20:59:33.783+0000 I NETWORK  [initandlisten] waiting for connections on port 27017

Despite that, the java-app throws an exception because it can't connect to the instance. Also, I can't connect to the mongo from the terminal.

Could you please advise, what's wrong?

Activity

  1. myashchenko commented on Mar 4, 2018

    @myashchenko
    Author

    Output:

    mongo_1 | 2018-03-03T20:59:31.464+0000 I CONTROL  [initandlisten] MongoDB starting : pid=6 port=27017 dbpath=/data/db 64-bit host=7751d36e0b39
    mongo_1 | 2018-03-03T20:59:31.465+0000 I CONTROL  [initandlisten] db version v3.6.3
    mongo_1 | 2018-03-03T20:59:31.465+0000 I CONTROL  [initandlisten] git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
    mongo_1 | 2018-03-03T20:59:31.465+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1t  3 May 2016
    mongo_1 | 2018-03-03T20:59:31.465+0000 I CONTROL  [initandlisten] allocator: tcmalloc
    mongo_1 | 2018-03-03T20:59:31.465+0000 I CONTROL  [initandlisten] modules: none
    mongo_1 | 2018-03-03T20:59:31.465+0000 I CONTROL  [initandlisten] build environment:
    mongo_1 | 2018-03-03T20:59:31.466+0000 I CONTROL  [initandlisten]     distmod: debian81
    mongo_1 | 2018-03-03T20:59:31.466+0000 I CONTROL  [initandlisten]     distarch: x86_64
    mongo_1 | 2018-03-03T20:59:31.466+0000 I CONTROL  [initandlisten]     target_arch: x86_64
    mongo_1 | 2018-03-03T20:59:31.466+0000 I CONTROL  [initandlisten] options: { replication: { replSet: "rs0" } }
    mongo_1 | 2018-03-03T20:59:31.466+0000 W -        [initandlisten] Detected unclean shutdown - /data/db/mongod.lock is not empty.
    mongo_1 | 2018-03-03T20:59:31.467+0000 I -        [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
    mongo_1 | 2018-03-03T20:59:31.467+0000 W STORAGE  [initandlisten] Recovering data from the last clean checkpoint.
    mongo_1 | 2018-03-03T20:59:31.468+0000 I STORAGE  [initandlisten] 
    mongo_1 | 2018-03-03T20:59:31.468+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
    mongo_1 | 2018-03-03T20:59:31.468+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
    mongo_1 | 2018-03-03T20:59:31.469+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=2466M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
    mongo_1 | 2018-03-03T20:59:33.438+0000 I STORAGE  [initandlisten] WiredTiger message [1520110773:438769][6:0x7fa96e15fa00], txn-recover: Main recovery loop: starting at 1/0
    mongo_1 | 2018-03-03T20:59:33.439+0000 I STORAGE  [initandlisten] WiredTiger message [1520110773:439805][6:0x7fa96e15fa00], txn-recover: Recovering log 1 through 2
    mongo_1 | 2018-03-03T20:59:33.554+0000 I STORAGE  [initandlisten] WiredTiger message [1520110773:554470][6:0x7fa96e15fa00], file:index-7--5161945408363194477.wt, txn-recover: Recovering log 2 through 2
    mongo_1 | 2018-03-03T20:59:33.736+0000 I CONTROL  [initandlisten] 
    mongo_1 | 2018-03-03T20:59:33.736+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
    mongo_1 | 2018-03-03T20:59:33.736+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
    mongo_1 | 2018-03-03T20:59:33.736+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
    mongo_1 | 2018-03-03T20:59:33.736+0000 I CONTROL  [initandlisten] 
    mongo_1 | 2018-03-03T20:59:33.736+0000 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
    mongo_1 | 2018-03-03T20:59:33.736+0000 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
    mongo_1 | 2018-03-03T20:59:33.736+0000 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
    mongo_1 | 2018-03-03T20:59:33.736+0000 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
    mongo_1 | 2018-03-03T20:59:33.736+0000 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
    mongo_1 | 2018-03-03T20:59:33.736+0000 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
    mongo_1 | 2018-03-03T20:59:33.736+0000 I CONTROL  [initandlisten] 
    mongo_1 | 2018-03-03T20:59:33.772+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
    mongo_1 | 2018-03-03T20:59:33.778+0000 I REPL     [initandlisten] Did not find local voted for document at startup.
    mongo_1 | 2018-03-03T20:59:33.780+0000 I REPL     [initandlisten] Did not find local replica set configuration document at startup;  NoMatchingDocument: Did not find replica set configuration document in local.system.replset
    mongo_1 | 2018-03-03T20:59:33.783+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
    mongo_1 | 2018-03-03T20:59:34.024+0000 I FTDC     [ftdc] Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost. OK
    
  2. yosifkit commented on Apr 17, 2018

    @yosifkit
    Member
        command: ["sh", "-c", "mongod --replSet rs0 && mongo admin --eval 'rs.initiate();'"]
    

    This will never get to the mongo admin side until the mongod server stops. You need to run the rs.initiate(); separately from the mongo server process. The hacky way is to run something in the background and then start mongod in the foreground.

    #!/bin/bash
    # untested, very hacky
    # set this script as your "entrypoint" and then command: ["--replSet", "rs0"]
    set -e
    
    # Sleep for 30 and then create the replica set
    { sleep 30 && mongo admin --eval 'rs.initiate();' } &
    
    exec docker-entrypoint.sh "$@"

    The less hacky is to use another process that uses service discovery so that you can ensure that you only initiate on the first server and then to join other nodes to the replica set.

    In the future, it'd be better to post questions like this in the Docker Community Forums, the Docker Community Slack, or Stack Overflow.

  3. eloparco commented on Apr 28, 2020

    @eloparco
        command: ["sh", "-c", "mongod --replSet rs0 && mongo admin --eval 'rs.initiate();'"]
    

    This will never get to the mongo admin side until the mongod server stops. You need to run the rs.initiate(); separately from the mongo server process. The hacky way is to run something in the background and then start mongod in the foreground.

    #!/bin/bash
    # untested, very hacky
    # set this script as your "entrypoint" and then command: ["--replSet", "rs0"]
    set -e
    
    # Sleep for 30 and then create the replica set
    { sleep 30 && mongo admin --eval 'rs.initiate();' } &
    
    exec docker-entrypoint.sh "$@"

    The less hacky is to use another process that uses service discovery so that you can ensure that you only initiate on the first server and then to join other nodes to the replica set.

    In the future, it'd be better to post questions like this in the Docker Community Forums, the Docker Community Slack, or Stack Overflow.

    I tried that but it is not working: even after substituting the curly brackets with the round ones (otherwise reporting a syntax error), after the 30 seconds, I get prompted MongoDB shell version v4.2.6 continuously until my PC gets stuck.
    Can you please try the script and tell me if it is working on your side? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions