在Mongoose / Express上使用Docker遇到mongo bson错误

时间:2019-07-02 03:01:21

标签: mongodb docker mongoose docker-compose bson

我正在做一个小型测试项目,一切正常,直到我添加了date-logger使用mongoose的“服务”。

运行docker-compose up --build时出现以下错误:

Error: Cannot find module 'mongodb/node_modules/bson'
Require stack:
- /app/node_modules/mongoose/lib/drivers/node-mongodb-native/objectid.js
...

其他所有内容都可以启动并正常运行,但此服务会在到达require('mongoose')时立即失败。

以下是一些重要文件:

docker-compose.yml

version: '3'
services:
  postgres:
    image: 'postgres:latest'
  redis:
    image: 'redis:latest'
  api:
    build: 
      dockerfile: Dockerfile.dev
      context: ./server
    volumes:
      - /app/node_modules
      - ./server:/app
    environment: 
      - REDIS_HOST=${REDIS_HOST}
      - REDIS_PORT=${REDIS_PORT}
      - PG_USER=${PG_USER}
      - PG_HOST=${PG_HOST}
      - PG_DB=${PG_DB}
      - PG_PASSWORD=${PG_PASSWORD}
      - PG_PORT=${PG_PORT}
  date-logger:
    build: 
      dockerfile: Dockerfile.dev
      context: ./date-logger
    volumes:
      - /app/node_modules
      - ./date-logger:/app
    environment: 
      - EXPRESS_PORT=${EXPRESS_PORT}
    depends_on:
      - "mongo"
  client:
    build:
      dockerfile: Dockerfile.dev
      context: ./client
    volumes:
      - /app/node_modules
      - ./client:/app
  worker:
    build:
      dockerfile: Dockerfile.dev
      context: ./worker
    volumes:
      - /app/node_modules
      - ./worker:/app
    environment: 
      - REDIS_HOST=${REDIS_HOST}
      - REDIS_PORT=${REDIS_PORT}
  nginx:
    restart: always
    build:
      dockerfile: Dockerfile.dev
      context: ./nginx
    ports:
      - '3050:80'
  mongo:
    image: mongo
    restart: always
    container_name: mongo

Dockerfile.dev on date-logger

FROM node:alpine
WORKDIR '/app'
COPY ./package.json .
RUN npm install
COPY . .
CMD ["npm", "run", "dev"]

package.json on date-logger

{
  "dependencies": {
    "express": "~4.17.0",
    "mongoose": "~5.5.11",
    "cors": "2.8.4",
    "body-parser": "~1.19.0"
  },
  "devDependencies": {
    "nodemon": "1.19.0"
  },
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon"
  }
}

index.js on date-logger


let express = require('express');
let bodyParser = require('body-parser');

// This console.log works
console.log('date-logger hello ---');

// ****!!!! IT FAILS IMMEDIATELY ON THIS REQUIRE !!!****
let mongoose = require('mongoose');

let app = express();

app.use(
  bodyParser.urlencoded({
    extended: true
  })
);
app.use(bodyParser.json());

mongoose.connect('mongodb://mongo/date-logger');

const ItemSchema = new mongoose.Schema({
  value: {
    type: String,
    required: true
  },
  date: {
    type: Date,
    default: Date.now
  }
});
Item = mongoose.model('item', ItemSchema);

var port = process.env.EXPRESS_PORT || 8080;

app.get('/', (req, res) => res.send('Hello World with Express'));
app.get('/date', (req, res) => res.send('data'));

app.get('/date/all', (req, res) => {
  Item.find({}).exec().then(results => res.send(results))
});

app.post('/date/add', (req, res) => {
  const newItem = new Item({
    value: req.body.value
  });

  newItem.save().then(item => res.send('success'));
});

app.listen(port, function() {
  console.log('Running RestHub on port ' + port);
});

我是devops世界的新手,因此非常感谢所有/任何帮助!谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您的问题与猫鼬有关,而不与docker有关, enter image description here