Here is my docker-compose.production.yml.
version: "3.7"
services:
##############################
# Back-End Container
##############################
backend:
container_name: mern_backend
init: true
environment:
- MONGO_URI=mongodb://root_username:super_secret_pass@mongo_db:27017/mern_db?authSource=admin
# - MONGO_URI=mongodb://mongo_db:27017/mern_db
restart: always
depends_on:
- db
networks:
- server
##############################
# MongoDB Container
##############################
db:
container_name: mongo_db
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root_username
MONGO_INITDB_ROOT_PASSWORD: super_secret_pass
volumes:
- dbdata:/data/db/
networks:
- server
networks:
server:
volumes:
dbdata:
In my app.js from the node.js/express container, I do the following.
// Database Setup
mongoose.connect((process.env.MONGO_URI), { useNewUrlParser: true });
// Mongoose Setup
const db = mongoose.connection;
db.on('error', console.error.bind( console, 'Connection Error:' ))
.once('open', function() { console.log('Database Connected!') });
Sometimes I get this error:
Connection Error: admin { MongoNetworkError: failed to connect to server [mern_db:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 172.27.0.2:27017]
Pool.<anonymous> (/home/node/node_modules/mongodb-core/lib/topologies/server.js:564:11)
Pool.emit (events.js:197:13)
Connection.<anonymous> (/home/node/node_modules/mongodb-core/lib/connection/pool.js:317:12)
Object.onceWrapper (events.js:285:13)
Connection.emit (events.js:197:13)
Socket.<anonymous> (/home/node/node_modules/mongodb-core/lib/connection/connection.js:246:50)
Object.onceWrapper (events.js:285:13)
Socket.emit (events.js:197:13)
emitErrorNT (internal/streams/destroy.js:82:8)
emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
processTicksAndRejections (internal/process/next_tick.js:76:17)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
Sometimes I get this error:
Connection Error: admin { MongoError: Authentication failed.
/home/node/node_modules/mongodb-core/lib/connection/pool.js:581:63
authenticateStragglers (/home/node/node_modules/mongodb-core/lib/connection/pool.js:504:16)
Connection.messageHandler (/home/node/node_modules/mongodb-core/lib/connection/pool.js:540:5)
emitMessageHandler (/home/node/node_modules/mongodb-core/lib/connection/connection.js:310:10)
Socket.<anonymous> (/home/node/node_modules/mongodb-core/lib/connection/connection.js:453:17)
Socket.emit (events.js:197:13)
addChunk (_stream_readable.js:288:12)
readableAddChunk (_stream_readable.js:269:11)
Socket.Readable.push (_stream_readable.js:224:10)
TCP.onStreamRead (internal/stream_base_commons.js:145:17)
ok: 0,
errmsg: 'Authentication failed.',
code: 18,
codeName: 'AuthenticationFailed',
name: 'MongoError',
[Symbol(mongoErrorContextSymbol)]: {} }
I followed this tutorial, as well, to no avail. I tried reading up on the mongo image documentation, but I don't understand how it all works, to be honest. I recently learned of the fact that OS X can't map the data directory to the host, but the data volume still doesn't work properly, I think.
I did docker-compose up
and it comes on without errors sometimes, and others with errors. I do docker system prune
, tried removing the volumes line from the docker-compose.yml file, but nothing worked so far. I used both MONGO_URI environment variables and each gives me the one of the errors.