0

Recently I'm using Docker and host it to our Dedicated Server everything is working fine,But Issue is we have made and absolute URL for mongodb here is our link below. I put mongodb URL in .env file

mongodb://<username>:<password>@<serverip>:27017/<dbname>?authSource=admin

This URL is accessible without docker, we have existing project in our server with this given URL and it is working. I did not use mongodb in our docker, it is installed globally in our server. Is there any suggestion ??

Md Rezaul Karim
  • 508
  • 7
  • 17

1 Answers1

0

If you are trying to access the MongoDB from inside a container, replace the with the name of the service in you docker-compose file.

So if your docker-compose.yml looks like this

version: "2"

services:
  mymongodb:
    image: mongo
    volumes:
      - ./mongodb:/data/db:z
    ports:
      - 127.0.0.1:27017:27017
    restart: always

your connection string looks like this: mongodb://<username>:<password>@mymongodb:27017/<dbname>?authSource=admin

Thomas K.
  • 121
  • 1
  • 10
  • Hello Thomas, Nope, We have already installed mongodb in our system, I need to connect it from outside of docker container. – Md Rezaul Karim Aug 04 '21 at 12:54
  • Ah I understand, than maybe this solves your problem? https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach/57833573#57833573 – Thomas K. Aug 04 '21 at 15:14