u123
u123

Reputation: 16287

Failure to connect to configured mongo instance (Connection refused)

Based on this guide:

https://docs.opsmanager.mongodb.com/current/tutorial/install-simple-test-deployment/

I am installing MongoDB and MongoDB Manager. I have created a docker image for each application and start them on the same virtual network:

docker network create --driver bridge mongo-network

with:

MongoDB:

 docker run -ti -d --network mongo-network -p 27017:27017 --name mongodb-container mongodb-image
 docker exec -ti -u mongod mongodb-container "mongod --port 27017 --dbpath /data/appdb --logpath /data/appdb/mongodb.log --wiredTigerCacheSizeGB 1 --fork"

And verified that its up and running with:

$  docker exec -ti -u mongod mongodb-container tail -f /data/appdb/mongodb.log

2019-04-21T15:26:05.208+0000 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2019-04-21T15:26:05.208+0000 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2019-04-21T15:26:05.208+0000 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2019-04-21T15:26:05.208+0000 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2019-04-21T15:26:05.208+0000 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2019-04-21T15:26:05.208+0000 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2019-04-18T06:23:35.268+0000 I CONTROL  [initandlisten] 
2019-04-18T06:23:35.269+0000 I STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: c0736278-72ec-4dfc-893c-8105eefa0ba8
2019-04-18T06:23:35.320+0000 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 4.0
2019-04-18T06:23:35.341+0000 I STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: 397c17a3-3c5e-4605-b4dc-8a936dd9e40e
2019-04-18T06:23:35.394+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/appdb/diagnostic.data'
2019-04-18T06:23:35.396+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
2019-04-18T06:23:35.397+0000 I STORAGE  [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: ac7bdb6e-4a60-430f-b1a4-34b09012e6da
2019-04-18T06:23:35.475+0000 I INDEX    [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
2019-04-18T06:23:35.475+0000 I INDEX    [LogicalSessionCacheRefresh]     building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2019-04-18T06:23:35.477+0000 I INDEX    [LogicalSessionCacheRefresh] build index done.  scanned 0 total records. 0 secs

MongoDB Manager:

  docker run -ti -d --network mongo-network -p 8080:8080 --name mongodb-manager-container mongodb-manager-image  
  docker exec -ti -u root mongodb-manager-container "/opt/mongodb/mms/bin/mongodb-mms start"

Below error message:

Generating new Ops Manager private key...
Starting pre-flight checks
    Failure to connect to configured mongo instance:Config{
       loadBalance=false,
       encryptedCredentials=false,
       ssl='false',
       dbNames='   [
          mmsdb,
          mmsdbprovisionlog,
          mmsdbautomation,
          mmsdbserverlog,
          mmsdbpings,
          mmsdbprofile,
          mmsdbrrd,
          mmsdbconfig,
          mmsdblogcollection,
          mmsdbjobs,
          mmsdbagentlog,
          mmsdbbilling,
          backuplogs,
          automationcore,
          monitoringstatus,
          mmsdbautomationlog,
          automationstatus,
          ndsstatus,
          cloudconf,
          backupdb,
          mmsdbprovisioning,
          mmsdbqueues
       ]   ',
       uri=mongodb://mongodb-container:27017/?maxPoolSize=150
    }Error:Timed out after 30000 ms while waiting to connect. Client view of cluster state is{
       type=UNKNOWN,
       servers=   [
          {
             address=mongodb-container:27017,
             type=UNKNOWN,
             state=CONNECTING,
             exception=         {
                com.mongodb.MongoSocketOpenException:Exception opening socket
             },
             caused by         {
                java.net.ConnectException:Connection refused (Connection refused)
             }
          }
       ]

And for mongodb - based on suggestions below - I am now using:

/etc/mongod.conf:

# network interfaces
net:
  port: 27017
  #bindIp: 127.0.0.1  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
  bindIp: 0.0.0.0,::

and for MongoDB manager I am specifying the name of the mongodb container in:

/opt/mongodb/mms/conf/conf-mms.properties

#mongo.mongoUri=mongodb://127.0.0.1:27017/?maxPoolSize=150
#mongo.mongoUri=mongodb://0.0.0.0:27017/?maxPoolSize=150
mongo.mongoUri=mongodb://mongodb-container:27017/?maxPoolSize=150

I have verified that I can ping mongodb-container from mongodb-manager-container with:

docker exec -it -u root mongodb-manager-container bash
[root@e23a34bf2161 /]# ping mongodb-container
PING mongodb-container (172.18.0.2) 56(84) bytes of data.
64 bytes from mongodb-container.mongo-network (172.18.0.2): icmp_seq=1 ttl=64 time=0.077 ms
64 bytes from mongodb-container.mongo-network (172.18.0.2): icmp_seq=2 ttl=64 time=0.059 ms
64 bytes from mongodb-container.mongo-network (172.18.0.2): icmp_seq=3 ttl=64 time=0.052 ms
^C
--- mongodb-container ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2042ms
rtt min/avg/max/mdev = 0.052/0.062/0.077/0.013 ms
[root@e23a34bf2161 /]# 

What am I missing?

EDIT:

Based on below suggestions I have now tried:

docker network create --driver bridge mongo-network
docker run -ti -d --network mongo-network -p 27017:27017 --name mongodb-container mongodb-image
# Copy modified version of mongod.conf file to container before starting mongodb    
docker cp mongod.conf mongodb-container:/etc/mongod.conf

docker exec -ti -u mongod mongodb-container "mongod --port 27017 --dbpath /data/appdb --logpath /data/appdb/mongodb.log --wiredTigerCacheSizeGB 1 --fork"
docker run -it --rm --net container:mongodb-container nicolaka/netshoot ss -lnt

Which gives:

State    Recv-Q    Send-Q        Local Address:Port        Peer Address:Port    
LISTEN   0         128              127.0.0.11:36001            0.0.0.0:*       
LISTEN   0         128               127.0.0.1:27017            0.0.0.0:*   

Not sure if this is expected/good output and why I need to spin up container from the nicolaka/netshoot image...

EDIT 2:

As suggested below if I pass: --bind_ip_all on the command line for starting mongod it works:

docker exec -ti -u mongod mongodb-container "mongod --bind_ip_all --port 27017 --dbpath /data/appdb --logpath /data/appdb/mongodb.log --wiredTigerCacheSizeGB 1 --fork"

So it seems when running as a docker container it completely ignores the /etc/mongod.conf file and you need to specify all the options in the docker exec command instead :-(

Upvotes: 2

Views: 1632

Answers (3)

u123
u123

Reputation: 16287

The problem was that it was ignoring the configuration in /etc/mongod.conf file. After googling e.g.:

https://jira.mongodb.org/browse/SERVER-36572

I found that you need to pass the --config parameter to mongod e. to get it to read the mongod.conf file e.g.:

mongod --config /etc/mongod.conf

and with docker:

docker exec -ti -u mongod mongodb-container "mongod --config /etc/mongod.conf"

After doing the above I can now get it to listen on all interfaces with the below configuration in /etc/mongod.conf:

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

Upvotes: 1

BMitch
BMitch

Reputation: 263637

DNS on the container name will resolve to the container ip. To connect to mongo on that name, even from inside the container, you need to have mongo listening on all interfaces:

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0,::

Upvotes: 2

Vasilii Angapov
Vasilii Angapov

Reputation: 9022

You are running two separate containers and expect them to talk to each other over localhost? That never gonna work. You have to add "--link mongodb-container:mongo" to second docker run command and then use address mongodb://mongo:27017 in manager container.

Upvotes: 0

Related Questions