Search code examples
mongodbdockerweb-applicationslocal

docker-compose cant connect to my local momgodb


I have been searching around on the similar questions and tried to find a solution for mine; Unfortunately, it's still not working. Everything necessary seems have been done.

Ok, my question is, my AngularCLI app, 'aied', accesses a local MongoDB named 'mockup'. The docker-compose.yml is as following,

version: '3' # specify docker-compose version

# Define the services/containers to be run
services:
  aied: #name of the second service
    build: .
    environment:
      - MONGO_URI=mongodb://db:27017/mockup
    volumes:
      - .:/usr/src/app
    ports:
      - "3000:3000" #specify ports forewarding
    depends_on:
      - db
    links:
      - db
  db:
    image: mongo
    environment:
      - MONGODB_USER=username
      - MONGODB_PASS=admin
      - MONGO_INITDB_DATABASE=mockup
      - MONGO_DATA_DIR=/data/db
    ports:
      - "27017:27017"
    volumes:
      - ./data/db:/data/db
    command: mongod --smallfiles --logpath=/dev/null # --quiet

volumes:
  db:

The part of server.js is as following

    //const db = "mongodb://user:pass@ds247347.mlab.com:47347/mockup";
    const db= "mongodb://db:27017/mockup";

    mongoose.Promise = global.Promise;

    mongoose.connect(db, function(err){
      if(err){
        console.error("can not receive data from mongodb ... " );
        console.error("Error! " + err);
      } else { console.log("... connected to "+db.toString());}
    });

    router.get('/models', function(req, res){
      console.log('Get request for all models');
      mdl.find({})
        .exec(function(err, mdls){
          if (err){
            console.log("Error retrieving models");
          }else {
            res.json(mdls);
          }
          console.log('mdls length: ' + mdls.length);
        });
    });

After "docker-compose up", I got the following,

Attaching to mockup_db_1, mockup_aied_1
aied_1  | 
aied_1  | > mock-up@0.0.0 start /usr/src/app
aied_1  | > node server
aied_1  | 
aied_1  | (node:15) DeprecationWarning: `open()` is deprecated in     mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mongo-client
aied_1  | Server running on localhost:3000
aied_1  | ... connected to mongodb://db:27017/mockup
aied_1  | Get request for all models
aied_1  | mdls length: 0

The problem is, docker-compose works fine with my remote MongoDB at mLab, it just cannot access with my local MongoDB.

First, I use "docker ps" to check the running containers, for example,

bdcebd0b0e0b        mockup_aied                                              
"npm start"              About an hour ago   Up 17 seconds       
0.0.0.0:3000->3000/tcp     mockup_aied_1
583f719d9f4a        mongo                                                    
"docker-entrypoint.s…"   About an hour ago   Up 18 seconds       
0.0.0.0:27017->27017/tcp   mockup_db_1

Second, by using " docker exec -it mockup_db_1 mongo", I get into the mongo shell,

  > show dbs
 admin   0.000GB
 config  0.000GB
 local   0.000GB

 > use mockup
 switched to db mockup
 > show collections
 > db.collection.count()
 0

The problem is clear, there is no collection (data) be added into the running mongodb container.

Could anyone help me? Thank you very much.


Solution

  • Yes, with a friend's help, I finally solved it. The changes are: - use another port for mongo, e.g., 27027; - mongodump out my data from local at port 27017 - mongorestore drop the data at port 27027

    It works then.