Avatar of Bruce Gust
Bruce Gust
Flag for United States of America asked on

Docker / Node -> Can't Connect to Database. Why?

I'm new to Docker.

I'm working with an app that's got a Dockerfile and all of the bells and whistles that should fire up immediately assuming I've got Docker installed, which I do.

But I can't connect to my database and the thing that's galling is that it says it's "undefined..."

b.gust@AHA-DT-BGUST2 MINGW64 /c/wamp64/www/bSmart (code-cleanup)
$ yarn start
yarn run v1.19.1
$ sh ./bin/start.sh
./bin/start.sh: line 3: [: =: unary operator expected
(node:37640) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
User and company already exist: {"_id":"5e2a0b8b374a731400c45b9b"}
You may log in with email accounts@appliedhealth.net and password C@tch0fTheDay!
Start making of the Form 5500 Collections.
  throw new Error(`unable to connect to database: ${mongoUri}`);

Error: unable to connect to database: undefined/undefined
    at NativeConnection.<anonymous> (C:\wamp64\www\bSmart\bin\create-f5500:14:9)
    at NativeConnection.emit (events.js:210:5)
    at C:\wamp64\www\bSmart\node_modules\mongoose\lib\connection.js:832:37
    at processTicksAndRejections (internal/process/task_queues.js:75:11)
[nodemon] 2.0.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json,twig
[nodemon] starting `node index.js`
(node:33528) UnhandledPromiseRejectionWarning: MongoWriteConcernError: No write concern mode named 'majority/dev-bsmart-bgust' found in replica set configuration
    at MessageStream.messageHandler (C:\wamp64\www\bSmart\node_modules\mongoose\node_modules\mongodb\lib\cmap\connection.js:256:20)
    at MessageStream.emit (events.js:210:5)
    at processIncomingData (C:\wamp64\www\bSmart\node_modules\mongoose\node_modules\mongodb\lib\cmap\message_stream.js:144:12)
    at MessageStream._write (C:\wamp64\www\bSmart\node_modules\mongoose\node_modules\mongodb\lib\cmap\message_stream.js:42:5)
    at doWrite (_stream_writable.js:431:12)
    at writeOrBuffer (_stream_writable.js:415:5)
    at MessageStream.Writable.write (_stream_writable.js:305:11)
    at TLSSocket.ondata (_stream_readable.js:727:22)
    at TLSSocket.emit (events.js:210:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at TLSSocket.Readable.push (_stream_readable.js:224:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:182:23)
(node:33528) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:33528) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code..

Open in new window

throw new Error(`unable to connect to database: ${mongoUri}`);

I've run this app before in the context of a different branch, so I'm certain the database name and credentials are credible, yet I can't connect and I don't know why.

I have this in my config.js file:

// require and configure dotenv, will load vars in .env in PROCESS.ENV

So, that gives me every reason to believe that my credentials are going to be coming from the .env file and I'm certain the right creds are documented. Still, no connection. Why?

What am I missing? Where else can I look?


Avatar of undefined
Last Comment
David Favor

8/22/2022 - Mon
David Favor

First fix ./bin/start.sh, which contains syntax errors. All bets off till this is fixed.

Then add installation of sshd in your Docker container + ssh into the container after it starts to ensure all your data volumes are correct.


Docker != LXD

Which means your database must be running elsewhere (outside your Docker container) for data to service container stop/teardown. LXD maintains data across container stop/restart operations.

If your database is already running somewhere outside the container, likely either your socket routing requires attention if you're using a TCP socket or some permission must be opened wider if you're using a UNIX domain socket.

In other words, Mongo is running somewhere + you have to arrange for your Docker instance to connect.

Loading up sshd + root ssh into your container while it's running is usually the fastest way to determine the problem + fix.
Bruce Gust


Thanks for weighing in!

One thing that distresses me is that my whole team is using this branch as they're starting point and I haven't heard anyone having problems. I don't understand why my dynamic is proving problematic, but that's OK! We'l get it done!

Here's my "start.sh" file:
#!/usr/bin/env bash

if [ $NODE_ENV = "production" ]; then
    node index.js
    nodemon index.js --legacy-watch --ext js,mjs,json,twig

What i have in bold is what's generating the "[: =: unary operator expected" error. What's wrong? What do I need to change?

Your comment about how Docker is not equal to LXD - what do you mean by that?

I'm wanting to lock that down because I adjusted the .env file to reflect my personal database dynamic. Initially, it was something different and I'm wondering if I made a mistake in changing it because, by default, a container should "contain" the database as well.

Finally, you mentioned, "Loading up sshd + root ssh into your container while it's running is usually the fastest way to determine the problem + fix..."

How do you do that and what is it?

Bruce Gust

Morning, David!

I googled the "[: =: unary operator expected" and found a solution that recommended using double brackets instead of one. I'm not even sure what kind of syntax this is so I'm just throwing stuff up against the wall hoping something sticks, but this seemed to work:

#!/usr/bin/env bash

if [[ $NODE_ENV = "production" ]]; then // (as opposed to "if [ $NODE_ENV = "production" ])
    node index.js
    nodemon index.js --legacy-watch --ext js,mjs,json,twig

Why would that make a differrence?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
David Favor

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Bruce Gust

David, you've seen the last couple of questions you were kind enough to weigh in on, so you know that this has been resolved.

But, as far as the database, once Docker is running, I just needed to click on "Connect" in Compass to see the database that was running within Docker. So, there was no special connection to look at, just what was there by default once Docker was successfully humming.

David Favor

You're welcome!