简体   繁体   中英

Cannot access mongodb inside docker container

I am currently containerizing my application inside docker. I have two images one for my node application and another for mongodb. I have link two using docker-compose. But whenever I try accessing mongodb inside container using the port 27017. But it displays like this: **

It looks like you are trying to access MongoDB over HTTP on the native driver port.

** I have mongodb config as:

 "url": "mongodb://mongoDB/astroDB"

My docker-compose file is as:

version: '3.0'
services:
web:
    image: astrobot-node
    build: .
    command: "yarn start"
    ports: 
        - "80:3601"
    depends_on:
        - "mongo"
mongo:
    image: mongo
    ports:
        - "27017:27017"

Can anyone tell me what's actually wrong. Thanks in advance.

the standard mangodb connection url should look like this

mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]] https://docs.mongodb.com/manual/reference/connection-string/

In you case the mongoDB in your url string should indicate the db host, To make it points to the mangodb container you should change it to "url": "mongodb://mongo/astroDB" , cause your service named mango not mangoDB .

you can also achieve that by provide a static IP to your container and write this IP directly, you can also find you container network IP by using the docker container inspect <container-IP> to test it manually.

EDIT

Here is a thread that should help you debugging your problem. https://forums.docker.com/t/how-mongodb-work-in-docker-how-to-connect-with-mongodb/44763/8

But whenever I try accessing mongodb inside container using the port 27017. But it displays like this: **

It looks like you are trying to access MongoDB over HTTP on the native driver port.

This is expected behavior if you try to send HTTP requests to the MongoDB port. What this says to me is you got your network connectivity figured out and that part is working correctly.

Next you just need to use a MongoDB driver to talk to the database from the application instead of hitting it with curl or similar.

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