Accessing mongodb with docker-compose?

 version: '3' services: mongo: restart: always image: 'mongo' ports: - '27017:27017' redis: restart: always image: 'redis' ports: - '6379:6379' backend: build: dockerfile: Dockerfile.dev context: ./backend volumes: - /app/node_modules - ./backend:/app environment: - REDIS_HOST=redis - REDIS_PORT=6379 - MONGO_USERNAME=mongo - MONGO_PASSWORD=mongo - MONGO_HOSTNAME=mongo - MONGO_PORT=27017 - MONGO_DB=mongo depends_on: - redis - mongo I have this line: const url = `mongodb://${keys.mongoUsername}:${keys.mongoPassword}@${keys.mongoHostname}:${keys.mongoPort}/${keys.mongoDb}`; 

gives me: mongodb://mongo:[email protected]:27017/mongo

and I can't connect to it.

I get:

 MongoNetworkError: failed to connect to server [mongodb:27017] on first connect [MongoError: Authentication failed. backend_1 | at Function._getError (/app/node_modules/mongodb/lib/core/auth/scram.js:141:14) backend_1 | at /app/node_modules/mongodb/lib/core/auth/scram.js:191:31 backend_1 | at _callback (/app/node_modules/mongodb/lib/core/connection/connect.js:349:5) backend_1 | at Connection.messageHandler (/app/node_modules/mongodb/lib/core/connection/connect.js:378:5) backend_1 | at Connection.emit (events.js:315:20) backend_1 | at processMessage (/app/node_modules/mongodb/lib/core/connection/connection.js:384:10) backend_1 | at Socket.<anonymous> (/app/node_modules/mongodb/lib/core/connection/connection.js:553:15) backend_1 | at Socket.emit (events.js:315:20) backend_1 | at addChunk (_stream_readable.js:302:12) backend_1 | at readableAddChunk (_stream_readable.js:278:9) backend_1 | at Socket.Readable.push (_stream_readable.js:217:10) backend_1 | at TCP.onStreamRead (internal/stream_base_commons.js:186:23) { backend_1 | [Symbol(mongoErrorContextSymbol)]: {} backend_1 | }] backend_1 | at Pool.<anonymous> (/app/node_modules/mongodb/lib/core/topologies/server.js:438:11) backend_1 | at Pool.emit (events.js:315:20) backend_1 | at /app/node_modules/mongodb/lib/core/connection/pool.js:561:14 backend_1 | at /app/node_modules/mongodb/lib/core/connection/pool.js:994:11 backend_1 | at callback (/app/node_modules/mongodb/lib/core/connection/connect.js:97:5) backend_1 | at /app/node_modules/mongodb/lib/core/connection/connect.js:396:21 backend_1 | at /app/node_modules/mongodb/lib/core/auth/auth_provider.js:66:11 backend_1 | at /app/node_modules/mongodb/lib/core/auth/scram.js:193:16 backend_1 | at _callback (/app/node_modules/mongodb/lib/core/connection/connect.js:349:5) backend_1 | at Connection.messageHandler (/app/node_modules/mongodb/lib/core/connection/connect.js:378:5) backend_1 | at Connection.emit (events.js:315:20) backend_1 | at processMessage (/app/node_modules/mongodb/lib/core/connection/connection.js:384:10) backend_1 | at Socket.<anonymous> (/app/node_modules/mongodb/lib/core/connection/connection.js:553:15) backend_1 | at Socket.emit (events.js:315:20) backend_1 | at addChunk (_stream_readable.js:302:12) backend_1 | at readableAddChunk (_stream_readable.js:278:9) { backend_1 | [Symbol(mongoErrorContextSymbol)]: {} backend_1 | } 

Now, I am wondering if the db gets created by docker with the username or all, or I need to create it myself. It wasn't really explained to me. I am thinking it gets created on its own, but I might be mistaken.

submitted by /u/jackofallsnakes
[link] [comments]
Source: Reddit