简体   繁体   中英

Connecting to MongoDB in Docker from external app

Is it possible to connect to a docker container running a MongoDB image from an external nodejs application running locally? I've tried connecting via localhost:27017. Here's the docker compose file I'm using:

version: '3'
services:
  mongodb:
    image: 'bitnami/mongodb:3.6.8'
    ports:
      - "27017:27017"
    environment:
      - MONGODB_ROOT_PASSWORD=$MONGODB_ROOT_PASSWORD
      - MONGODB_USERNAME=$MONGODB_USERNAME
      - MONGODB_PASSWORD=$MONGODB_PASSWORD
      - MONGODB_DATABASE=$MONGODB_DATABASE
    volumes:
      - /data/db:/bitnami

I try connecting to it with the following url with no luck:

mongodb://${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWORD}@localhost:27017

EDIT: Connecting via mongodb://localhost:27017 works, but the authentication url errors out. I printed out the result of this string and there's nothing particularly wrong with it. I verified that the username and password match the users inside mongo in the docker container.

app.listen(port, () => {
  console.log(`Example app listening on port ${port}!`);
  const url = (() => {
    if(process.env.MONGODB_USERNAME && process.env.MONGODB_PASSWORD) {
      return `mongodb://${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWORD}@localhost:27017/`;
    }
    console.log('could not find environment vars for mongodb');
  })();

  MongoClient.connect(url, (err, client) => {
    if(err) {
      console.log('DB connection error');
    } else {
      console.log("Connected successfully to server");
      client.close();
    }
  });
});

If the external nodejs application is also running in a docker container then you need to link the containers. Here is an example of a docker run cmd that links containers. I added environment variables to illustrate what host name and port you would use from inside the container.

    docker run -d -it -e DEST_PORT=27017 -e DEST_HOST='mongodb' --link mongodb external-application:latest

It's important to always check the result of docker logs <container-name> --tail 25 -f . From my point of view, I think it is an issue related to permissions on this directory '/bitnami/mongodb'. Check out sameersbn comment how to fix this permission issue.

I'll assume it's the compose specification then. Try the following configuration

 environment:
   MONGODB_ROOT_PASSWORD:$MONGODB_ROOT_PASSWORD
   MONGODB_USERNAME:$MONGODB_USERNAME
   MONGODB_PASSWORD:$MONGODB_PASSWORD
   MONGODB_DATABASE:$MONGODB_DATABASE
 volumes:
   - '/data/db:/data/db'

The issue turned out to be that I had changed the password in MONGODB_PASSWORD (it had an @ in it so I thought it would have interfered with the string parsing, so I consequently changed it). The problem is, when the container restarts it references the same volume (as it should), so the users were never updated and as a result I was logging in with the wrong credentials.

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