Can not connect mongodb droplet from another docker droplet?

May 19, 2017 70 views
MongoDB Arch Linux

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

**MongoError: failed to connect to server [] 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 ) ufw config is:

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
80                         ALLOW       Anywhere
443                        ALLOW       Anywhere
Anywhere                   ALLOW
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.
  dbPath: /var/lib/mongodb
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

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

# network interfaces
  port: 27017

1 Answer


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.