I have developed sample meanstack application (sampledocker). I want to run this application in docker. And I want to connect my system database(my system ip address:192.168.1.174) Mongodb database into that docker application.
- docker build -t nitikishu/sampledocker
- docker push nitikishu/sampledocker
- docker pull nitikishu/sampledocker
- docker run -p 4200:4200 nitikishu/sampledocker
Now Docker container has sampledocker application image. The server page has mongoose.connect("mongodb://mongo:27017/dbname")
From this env file I am passing this mongodb path into server.js (app.js)
My project structure is:
- SAMPLE
- node_modules
- public
- controller
- samplecontroller.js
- index.html
- index1.html
- dockerignore.txt
- Dockerfile
- env.Config
- docker-compose.yml
- package.json
- server.js
How to connect mongodb with sampledocker container in docker? This db I want to connect with sampledocker container.
Unable to login to the application
Dockerfile:
FROM node:latest
RUN mkdir -p C:\Users\user2\Sample
WORKDIR C:\Users\user2\Sample
COPY package.json C:\Users\user2\Sample
RUN npm install
COPY . .
EXPOSE 4200
CMD [ “npm”, “start” ]
docker-compose.file
version : '3'
services :
sampledocker:
build: .
ports:
- "4200:4200"
depends_on:
- mongo
mongo:
image: mongo:3.4.2
ports:
- "27017:27017"
volumes:
- data:/data/db
volumes:
data:
server.js(main.js)
var express = require('express');
var session = require('express-session');
const bodyParser = require('body-parser');
const logger = require('morgan');
const chalk = require('chalk');
const errorHandler = require('errorhandler');
const dotenv = require('dotenv');
const path = require('path');
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo')(session);
const passport = require('passport');
const expressValidator = require('express-validator');
var app = express();
var multer = require('multer');
var request = require('request');
var fs = require('fs');
dotenv.load({ path: '.env.Config' });
app.use(express.static(__dirname + "/public"));
app.set('views', __dirname + '\\public');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(bodyParser.json());
mongoose.connect("mongodb://mongo:27017/dbname");
mongoose.connection.on('error', () =>
{
console.log(error);
process.exit();
});
app.use(session({
resave: true,
saveUninitialized: true,
secret: process.env.SESSION_SECRET,
store: new MongoStore({
url:"mongodb://mongo:27017/dbname",
autoReconnect: true
})
}));
app.set('port', process.env.App_PORT || 3000);
app.listen(app.get('port'), () => {
console.log('%s server running on port', chalk.green('✓'), app.get('port'));
console.log(' Press CTRL-C to stop\n');
});