Hi,
I’m trying to do a simple docker app with NodeJS and MongoDB.
This is my docker-compose.yml file:
version: '3'
services:
web:
build:
context: .
dockerfile: ./Dockerfile
container_name: wiki-app
working_dir: /usr/src/app
env_file: ./.env
volumes:
- .:/usr/src/app
ports:
- 3000:3000
depends_on:
- mongo
command:
- /bin/sh
- -c
- |
make -C /usr/src/app/ start-local
networks:
- wiki-network
mongo:
image: mongo:latest
container_name: wiki-db
ports:
- 27017:27017
volumes:
- wiki-db:/data/db
networks:
- wiki-network
networks:
wiki-network:
driver: bridge
volumes:
wiki-db:
My app.js:
'use strict';
const express = require('express');
const https = require('https');
const bodyParser = require('body-parser');
const process = require('process');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const path = require('path');
const mongoose = require('mongoose');
// Constants
const PORT = 3000;
const HOST = '0.0.0.0';
const app = express();
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended: true}));
// get env variable
const envFilePath = path.resolve(__dirname, './.env');
const env = require("dotenv").config({ path: envFilePath });
if (env.error) {
throw new Error(`Unable to load the .env file from ${envFilePath}. Please copy .env.example to ${envFilePath}`);
}
//MongoDB Connect
// mongoose.connect('mongodb://wiki:wiki@localhost:27017/wiki', {useNewUrlParser: true, useUnifiedTopology: true});
//Set up default mongoose connection
var mongoDB = 'mongodb://localhost:27017/wiki';
mongoose.connect(mongoDB, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
});
//Get the default connection
var db = mongoose.connection;
//Bind connection to error event (to get notification of connection errors)
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
// const articleSchema = {
// title: String,
// content: String
// };
//
// const Article = mongoose.model("Article", articleSchema);
//Server listening
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
My package.js
{
"name": "wiki",
"version": "1.0.0",
"description": "nodejs wiki",
"author": "",
"license": "MIT",
"main": "app.js",
"private": true,
"scripts": {
"start": "nodemon app.js"
},
"keywords": [
"nodejs",
"express"
],
"dependencies": {
"body-parser": "^1.19.0",
"cookie-parser": "~1.4.4",
"dotenv": "^8.2.0",
"ejs": "^3.1.6",
"express": "~4.17.1",
"mongoose": "^5.13.3",
"morgan": "~1.9.1",
"nodemon": "2.0.12",
"query-string": "^7.0.0",
"require": "^0.4.4"
}
}
When I run docker-compose up, after about 15 seconds I got this error:
wiki-app | MongoDB connection error: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
wiki-app | at NativeConnection.Connection.openUri (/usr/src/app/node_modules/mongoose/lib/connection.js:846:32)
wiki-app | at /usr/src/app/node_modules/mongoose/lib/index.js:351:10
wiki-app | at /usr/src/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
wiki-app | at new Promise ()
wiki-app | at promiseOrCallback (/usr/src/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
wiki-app | at Mongoose._promiseOrCallback (/usr/src/app/node_modules/mongoose/lib/index.js:1149:10)
wiki-app | at Mongoose.connect (/usr/src/app/node_modules/mongoose/lib/index.js:350:20)
wiki-app | at Object. (/usr/src/app/app.js:32:10)
wiki-app | at Module._compile (node:internal/modules/cjs/loader:1095:14)wiki-app | at Object.Module._extensions…js (node:internal/modules/cjs/loader:1124:10)
wiki-app | at Module.load (node:internal/modules/cjs/loader:975:32)
wiki-app | at Function.Module._load (node:internal/modules/cjs/loader:816:12)
wiki-app | at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
wiki-app | at node:internal/main/run_main_module:17:47 {
wiki-app | reason: TopologyDescription {
wiki-app | type: ‘Single’,
wiki-app | setName: null,
wiki-app | maxSetVersion: null,
wiki-app | maxElectionId: null,
wiki-app | servers: Map(1) { ‘localhost:27017’ => [ServerDescription] },
wiki-app | stale: false,
wiki-app | compatible: true,
wiki-app | compatibilityError: null,
wiki-app | logicalSessionTimeoutMinutes: null,
wiki-app | heartbeatFrequencyMS: 10000,
wiki-app | localThresholdMS: 15,
wiki-app | commonWireVersion: null
wiki-app | }
wiki-app | }
wiki-app | node:internal/process/promises:246
wiki-app | triggerUncaughtException(err, true /* fromPromise */);
wiki-app | MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
wiki-app | at NativeConnection.Connection.openUri (/usr/src/app/node_modules/mongoose/lib/connection.js:846:32)
wiki-app | at /usr/src/app/node_modules/mongoose/lib/index.js:351:10
wiki-app | at /usr/src/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
wiki-app | at new Promise ()
wiki-app | at promiseOrCallback (/usr/src/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
wiki-app | at Mongoose._promiseOrCallback (/usr/src/app/node_modules/mongoose/lib/index.js:1149:10)
wiki-app | at Mongoose.connect (/usr/src/app/node_modules/mongoose/lib/index.js:350:20)
wiki-app | at Object. (/usr/src/app/app.js:32:10)
wiki-app | at Module._compile (node:internal/modules/cjs/loader:1095:14)
wiki-app | at Object.Module._extensions…js (node:internal/modules/cjs/loader:1124:10)
wiki-app | at Module.load (node:internal/modules/cjs/loader:975:32)
wiki-app | at Function.Module._load (node:internal/modules/cjs/loader:816:12)wiki-app | at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
wiki-app | at node:internal/main/run_main_module:17:47 {
wiki-app | reason: TopologyDescription {
wiki-app | type: ‘Single’,
wiki-app | setName: null,
wiki-app | maxSetVersion: null,
wiki-app | maxElectionId: null,
wiki-app | servers: Map(1) {
wiki-app | ‘localhost:27017’ => ServerDescription {
wiki-app | address: ‘localhost:27017’,
wiki-app | error: Error: connect ECONNREFUSED 127.0.0.1:27017
wiki-app | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1142:16) {
wiki-app | name: ‘MongoNetworkError’
wiki-app | },
wiki-app | roundTripTime: -1,
wiki-app | lastUpdateTime: 18872928,
wiki-app | lastWriteDate: null,
wiki-app | opTime: null,
wiki-app | type: ‘Unknown’,
wiki-app | topologyVersion: undefined,
wiki-app | minWireVersion: 0,
wiki-app | maxWireVersion: 0,
wiki-app | hosts: ,
wiki-app | passives: ,
wiki-app | arbiters: ,
wiki-app | tags:
wiki-app | }
wiki-app | },
wiki-app | stale: false,
wiki-app | compatible: true,
wiki-app | compatibilityError: null,
wiki-app | logicalSessionTimeoutMinutes: null,
wiki-app | heartbeatFrequencyMS: 10000,
wiki-app | localThresholdMS: 15,
wiki-app | commonWireVersion: null
wiki-app | }
wiki-app | }
wiki-app | [nodemon] app crashed - waiting for file changes before starting…
Thanks for you help.