2
votes

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.

  1. docker build -t nitikishu/sampledocker
  2. docker push nitikishu/sampledocker
  3. docker pull nitikishu/sampledocker
  4. 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:

  1. 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');
});
1
Hi N15; it's not clear what's wrong, because you haven't shown us how your application is failing. Does it produce an error, for example?Vince Bowdren
I am unable to access the application with the application username and password while login. when I hit login button it should connect my system db to verify the username password in the sampledb. if success it should redirect to next page. I am not getting any error. page also not redirecting. is there db connection problem?N15

1 Answers

0
votes

You are trying to use the .env file to define environment variables for use in the container:

MONGODB_URI=mongodb://192.168.1.174:27017/Sample_DB
SESSION_SECRET=sample
App_PORT = 4200

But, that's not how it works. The environment variables in the .env file are "used for variable substitution in your Compose file", not for the container's environment.

As the docs go on to say:

Environment variables defined in the .env file are not automatically visible inside containers. To set container-applicable environment variables, follow the guidelines in the topic Environment variables in Compose, which describes how to pass shell environment variables through to containers, define environment variables in Compose files, and more.