I'm not able to solve MongoParseError.
First I had tried this
mongoose.connect(`mongodb://${process.env.DB_USER}:${process.env.DB_PASSWORD}@db:27017/${process.env.DB_NAME}`, {useNewUrlParser: true});
made me ending up in this error
MongoParseError: Unescaped slash in userinfo section
Then I escaped all the slashes
mongoose.connect(`mongodb:\\/\\/${process.env.DB_USER}:${process.env.DB_PASSWORD}@db:27017\\/${process.env.DB_NAME}`, {useNewUrlParser: true});
and I ended up in this error which I'm not able to solve
MongoParseError: Invalid connection string
This is my index.js
'use strict';
const mongoose = require('mongoose');
mongoose.connect(`mongodb:\\/\\/${process.env.DB_USER}:${process.env.DB_PASSWORD}@db:27017\\/${process.env.DB_NAME}`, {useNewUrlParser: true});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('ok');
});
const express = require('express');
// Constants
const PORT = 8080;
const HOST = 'localhost';
var kittySchema = new mongoose.Schema({
name: String
});
var Kitten = mongoose.model('Kitten', kittySchema);
var silence = new Kitten({ name: 'Test1234' });
// App
const app = express();
app.get('/', (req, res) => {
res.send(silence.name);
});
app.listen(PORT);
console.log(`Running on http://${HOST}:${PORT}`);
And here's the docker-compose.yml
version: '3.7'
services:
web:
build: .
command: npm start
volumes:
- ./src:/usr/app/
- ./src/node_modules:/usr/app/node_modules
ports:
- 80:8080
environment:
DB_NAME: websitedb
DB_USER: /run/secrets/db_user
DB_PASSWORD: /run/secrets/db_password
secrets:
- db_user
- db_password
db:
image: mongo:latest
volumes:
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
ports:
- "27017:27017"
environment:
MONGO_INITDB_DATABASE: websitedb
MONGO_INITDB_ROOT_USERNAME_FILE: /run/secrets/db_user
MONGO_INITDB_ROOT_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_user
- db_password
volumes:
mongo:
secrets:
db_user:
file: ./db_user.txt
db_password:
file: ./db_password.txt
Can anyone tell me what I'm doing wrong?
Looking through the guide on managing secrets in docker compose, it looks like any secrets are passed into the container as a file mounted in /run/secrets/SECRET_NAME, and the suggestion is to pass the path to this file as an environment variable and then read the file in your app:
When you deploy, Docker mounts a file under /run/secrets/ in the services. These files are never persisted in disk, but are managed in memory.
Each service uses environment variables to specify where the service should look for that secret data.
You have passed the path to these secrets as process.env.DB_USER, but to get the actual secret you need to read the file:
const fs = require('fs');
const mongoose = require('mongoose');
let dbUser = fs.readFileSync(process.env.DB_USER);
let dbPassword = fs.readFileSync(process.env.DB_PASSWORD);
mongoose.connect(`mongodb://${dbUser}:${dbPassword}@db:27017/${process.env.DB_NAME}`, {useNewUrlParser: true});