简体   繁体   中英

How can I get a nodejs server running under docker get connected by mongoose to mongodb?

I'm struggling trying to connect my nodejs server (running within a docker container) to mongodb by mongoose

server.js:

import * as express from 'express';
import { Request, Response } from 'express';
import { encode } from 'jwt-simple';
import * as bcrypt from 'bcrypt';
import { connect } from 'mongoose';
import { UserModel, User } from './models';

const app: express.Application = express();
const PORT = 3333;

app.use(express.json());

app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*")
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
  next();
});

// some code here

connect(`mongodb://0.0.0.0:27017/user-db`, {
  useNewUrlParser: true,
  useUnifiedTopology: true
})
.then(() => {
  console.log('successfully connected to the database');
})
.catch(err => {
  console.log('error connecting to the database');
  process.exit();
});

app.listen(PORT, () => {
  console.log(`Server is listening at port - ${PORT}`);
});

Then I have two images one for the server and one for mongodb, after run mongodb image I run the server one, which get served (already compiled from.ts) to docker, then i run:

1 docker run -it --name server -p 3333:3333 my:server sh

then within "server" container i run:

2 node server.js

server routes work fine, but after few seconds mongoose.connect falls into the catch

You have two option to connect with Mongo DB container.

  • Host IP (Where Mongo DB container is running)
  • Same Docker network

For the first option, you can pass the Host IP as an environment variable to your nodejs container as 0.0.0.0 means all interfaces as mentioned by @David which will not work in this case. make the following changes

// set some default value if ENV is not set
const mongo_host=process.env.MONGO_HOST || "localhost"

connect(mongo_host, {
  useNewUrlParser: true,
  useUnifiedTopology: true
})

then run the container and pass the Mongo Host IP

docker run -it -e MONGO_HOST=192.168.x.x --name server -p 3333:3333 my:server sh

or if the Host is Mac or window you also use special DNS host.docker.internal


connect('host.docker.internal', {
  useNewUrlParser: true,
  useUnifiedTopology: true
})
  • The second option is the Docker Network
version: "3"
services:
  app:
    container_name: nodejs-mongo
    image: nodejs_app
    environment:
      - MONGO_HOST=mongo
    ports:
      - "3000:3000"
    depends_on:
      - mongo
  mongo:
    container_name: mongo
    image: mongo
    ports:
      - "27017:27017"

in this case, we pass MONGO_HOST=mongo which docker can resolve this for connecting with Mongo container in the same network.

docker-compose networking

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM