简体   繁体   English

docker-compose NodeJS + Mongodb with mongoose,动态获取mongo容器ip

[英]docker-compose NodeJS + Mongodb with mongoose, dynamically get the mongo container ip

I have a dockerized nodejs app linked with a mongo container. 我有一个与mongo容器链接的dockerized nodejs应用程序。 Here is the dockerfile and docker-compose.yml : 这是dockerfile和docker-compose.yml:

FROM node:boron

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE 3000
CMD [ "npm", "start" ]

version: "2"
services:
  web:
    build: .
    volumes:
      - .:/usr/src/app
    ports:
      - "3000:3000"
    links:
      - mongo
  mongo:
    image: mongo
    ports:
        - "27017:27017"

and here is my app.js handling the mongo connection via the mongoose tool : 这是我的app.js通过mongoose工具处理mongo连接:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');


var index = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

//DB setup
mongoose.connect('mongodb://172.19.0.2/test');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

My app launches, if I go on http://localhost:3000 on my computer my view displays and in my terminal I see that mongo accepted a connection. 我的应用程序启动,如果我在我的计算机上的http:// localhost:3000上显示我的视图,在我的终端中我看到mongo接受了连接。


Problem is : 问题是:

In my app.js I added manually the mongo container ip. 在我的app.js中,我手动添加了mongo容器ip。 I believe if I push this app on github and someone gets it. 我相信如果我在github上推这个应用程序并且有人得到它。 His mongo container ip won't be the same as the one i added manually. 他的mongo容器ip与我手动添加的容器不一样。 Is there some way to dynamise all of this so that my app gets automatically the mongo container ip ? 有没有办法动态所有这一切,以便我的应用程序自动获取mongo容器IP

Check out Networking section in Docker Compose Manual. 查看Docker Compose Manual中的Networking部分。 Especially this interesting paragraph: 特别是这段有趣的段落

Each container can now look up the hostname web or db and get back the appropriate container's IP address. 现在,每个容器都可以查找主机名web或db,并获取相应容器的IP地址。 For example, web's application code could connect to the URL postgres://db:5432 and start using the Postgres database. 例如,web的应用程序代码可以连接到URL postgres:// db:5432并开始使用Postgres数据库。

So given your example, you can freely change line 所以举个例子,你可以随意改变线条

mongoose.connect('mongodb://172.19.0.2/test');

to line 到线

mongoose.connect('mongodb://mongo/test');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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