Mongodb: assigning docker gateway to mongo.conf

I’m trying to use my local mongodb inside my docker container.

To allow access, I have edited my mongo.conf as follows:

but when running mongo db restart, I get this in the mongodb.log:

  destination: file
  path: /opt/homebrew/var/log/mongodb/mongo.log
  logAppend: true
  dbPath: /opt/homebrew/var/mongodb
  ipv6: false

Can’t assign requested address

{"t":{"$date":"2022-09-21T15:35:17.125+03:00"},"s":"E",  "c":"CONTROL",  "id":20568,   "ctx":"initandlisten","msg":"Error setting up listener","attr":{"error":{"code":9001,"codeName":"SocketException","errmsg":"Can't assign requested address"}}}

and running brew services restart mongodb-community && brew services list

mongodb-community error  12288 ~/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist

Hi @rawand_ahmad and welcome to the MongoDB Community forums! :wave:

This means that you’re trying to bind the mongod process to an IP address that it can’t bind to. belongs to the docker networking stack for containers to talk to each other. The local mongod cannot bind to that IP as that IP space is not accessible outside of Docker. Remove that from the list of bindIPs and you should be able to restart the mongod process using brew services rrestart mongodb-community.

You can use the host.docker.internal address to connect from your Docker container to processes running on your local machine. The below screenshot shows this in action:

In the screenshot we see the following:

  • We connect to the container
  • We see that we don’t have anything listening in the container on port 27017
  • We use the mongosh tool to connect to host.docker.internal which is our locally running mongod process on our Mac
1 Like

Thank you, thank you, you saved me hours of work.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.