haoruzhao
By:
haoruzhao

Can not connect mongodb droplet from another docker droplet?

May 19, 2017 182 views
MongoDB Arch Linux

Hi Guys, I have one question that i saw the error in my docker droplet(ip is 104.131.114.98) like, any thing else to make it work? Thank you:

/opt/reaction/dist/bundle/programs/server/node_modules/fibers/future.js:313
                        throw(ex);
                        ^
**MongoError: failed to connect to server [104.236.41.77:27017] on first connect**
    at Object.Future.wait (/opt/reaction/dist/bundle/programs/server/node_modules/fibers/future.js:449:15)
    at new MongoConnection (packages/mongo/mongo_driver.js:211:27)

Can not figure why this happens. my mongodb droplet (ip is 104.236.41.77 ) ufw config is:

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
80                         ALLOW       Anywhere
443                        ALLOW       Anywhere
Anywhere                   ALLOW       104.131.114.98
27017                      ALLOW       Anywhere
22 (v6)                    ALLOW       Anywhere (v6)
80 (v6)                    ALLOW       Anywhere (v6)
443 (v6)                   ALLOW       Anywhere (v6)
27017 (v6)                 ALLOW       Anywhere (v6)

my mongodb configure is:

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 104.236.41.77

1 Answer

@haoruzhao

I have somewhat limited experience with Docker as I much prefer to stick with bare metal or the hypervisor layer (KVM for DigitalOcean), though from that experience, Docker doesn't bind to a specific IP, IIRC. So connecting to a MongoDB instance that is within a container wouldn't normally be possible from the Droplet's public IP by default.

You'd have to setup port forwarding using iptables and forward requests on the host (Droplet) to the container (where MongoDB is) -- such as accepting requests on port 27017 (on the Droplet) and then forwarding it to the exposed port on the container (which could be the same port).

You may have the port open on the host (Droplet), but that doesn't generally translate to a container picking up the connection unless the firewall is configured to forward the request to a specific container. When a request is received, the host node (Droplet) doesn't have anything running on that port and without the firewall, can't route accordingly.

Have another answer? Share your knowledge.