Home > Net >  how to connect one of my mongo container to another?
how to connect one of my mongo container to another?

Time:07-21

I want to use Typesense with Mongodb. To do so I first need to enable replicaset.
From what I read here and there, to achieve it with docker compose, it must be done in two steps, like this :

version: '3.8'

services:
  mongodb:
    image: mongo
    container_name: 'mongodb'
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
      MONGO_INITDB_DATABASE: testDb
    volumes:
      - ./mongodb-config/mongod.test.conf:/etc/mongod.conf:ro
      - ./mongodb-config/keyfile:/test/keyfile
    ports:
      - '27017:27017'
    expose:
      - '27017'
    entrypoint:
      - bash
      - -c
      - |
        chmod 400 /test/keyfile
        chown 999:999 /test/keyfile
        exec docker-entrypoint.sh $$@
    command: mongod --replSet testRs --keyFile /test/keyfile --bind_ip localhost;

  mongosetup:
    image: mongo
    depends_on:
      - mongodb
    restart: 'no'
    entrypoint:
      [
        'bash',
        '-c',
        "sleep 10 && mongo admin --host mongodb:27017 -u root -p example --verbose --eval 'rs.initiate()'",
      ]

log from mongosetup :

MongoDB shell version v5.0.9
connecting to: mongodb://mongodb:27017/admin?compressors=disabled&gssapiServiceName=mongodb
{"t":{"$date":"2022-07-20T14:58:50.437Z"},"s":"D1", "c":"NETWORK",  "id":20109,   "ctx":"js","msg":"Creating new connection","attr":{"hostAndPort":"mongodb:27017"}}
{"t":{"$date":"2022-07-20T14:58:50.440Z"},"s":"D1", "c":"-",        "id":23074,   "ctx":"js","msg":"User assertion","attr":{"error":"InternalError: couldn't connect to server mongodb:27017, connection attempt failed: SocketException: Error connecting to mongodb:27017 (192.168.0.2:27017) :: caused by :: Connection refused","file":"src/mongo/scripting/mozjs/mongo.cpp","line":745}}
Error: couldn't connect to server mongodb:27017, connection attempt failed: SocketException: Error connecting to mongodb:27017 (192.168.0.2:27017) :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:372:17
@(connect):2:6
{"t":{"$date":"2022-07-20T14:58:50.441Z"},"s":"D1", "c":"-",        "id":23074,   "ctx":"js","msg":"User assertion","attr":{"error":"Location12513: connect failed","file":"src/mongo/shell/shell_utils.cpp","line":537}}
{"t":{"$date":"2022-07-20T14:58:50.441Z"},"s":"I",  "c":"QUERY",    "id":22787,   "ctx":"js","msg":"MozJS GC heap stats","attr":{"phase":"prologue","total":4799817,"limit":0}}
{"t":{"$date":"2022-07-20T14:58:50.444Z"},"s":"I",  "c":"QUERY",    "id":22787,   "ctx":"js","msg":"MozJS GC heap stats","attr":{"phase":"epilogue","total":153,"limit":0}}
{"t":{"$date":"2022-07-20T14:58:50.444Z"},"s":"D1", "c":"-",        "id":23074,   "ctx":"main","msg":"User assertion","attr":{"error":"Location12513: connect failed","file":"src/mongo/scripting/mozjs/proxyscope.cpp","line":310}}
exception: connect failed
exiting with code 1

There must be something wrong with my way to try to connect, because I can do that manually without any problem :

docker exec -it 0188651cfb4b5789e832226094dae48fb8efd92fc318ce7b0286312f9a3d81de bash 
root@0188651cfb4b:/# mongosh -u root -p example
test> rs.initiate()
{
  info2: 'no configuration specified. Using a default configuration for the set',
  me: 'localhost:27017',
  ok: 1
}
testRs [direct: other] test> 

I must be missing something, and since I'm a beginner with docker & mongo, I'm stuck :( Thanks for your help

CodePudding user response:

--bind_ip localhost in the command of mongodb container tells mongo to listen on local interface only. Try to connect to it from mongosetup manually and you will face the same error.

Use --bind_ip_all option instead.

  • Related