如何通过节点容器将猫鼬安全地连接到mongo docker容器进行生产?

时间:2019-02-19 10:36:07

标签: node.js mongodb docker

这是我的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:

在来自node.js / express容器的app.js中,执行以下操作。

// 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!') });

有时我会收到此错误:

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)]: {} }

有时我会收到此错误:

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)]: {} }

我也遵循this tutorial,但无济于事。我尝试阅读mongo image documentation,但老实说,我不明白这是如何工作的。我最近了解到OS X无法将数据目录映射到主机的事实,但是我认为数据量仍然无法正常工作。


我做了docker-compose up,它有时正确无误,而其他错误有误。我做了docker system prune,尝试从docker-compose.yml文件中删除卷行,但到目前为止没有任何效果。我使用了两个MONGO_URI环境变量,每个变量都给了我一个错误。

0 个答案:

没有答案