2

I have a flask application which is running as docker container. Flask application uses local mongodb. This docker container is not able to connect to local mongodb.

I have tried following option :

  • set --network="host" in docker run ... command

  • set MONGO_URI = "mongodb://host-ip-address:27017/model-service-sample",

  • set MONGO_URI = "mongodb://container-gateway-ip-address:27017/model-service-sample"

None of the above options worked.

Can anyone please suggest a way to accomplish this?

3
  • Are you using MacOS? Commented Aug 6, 2019 at 1:05
  • did you expose port for mongo when docker run -p xxxx:27017 mongoimage ? Commented Aug 6, 2019 at 2:07
  • @ThanhNguyenVan , yes I am using macOS and my mongodb is not running as docker container, my flask application which is running as a docker container needs to access local mongodb Commented Aug 6, 2019 at 2:45

1 Answer 1

2

For MacOS you should use:

host.docker.internal or gateway.docker.internal

for connecting from a container to a service on the host.

refer : https://docs.docker.com/docker-for-mac/networking/#/known-limitations-use-cases-and-workarounds

Sign up to request clarification or add additional context in comments.

2 Comments

I read about this but couldn't quite understand what does host.docker.internal mean. Do I need to write this in my Dockerfile?
no, change MONGO_URI = "mongodb://container-gateway-ip-address:27017/model-service-sample" to MONGO_URI = "mongodb://gateway.docker.internal:27017/model-service-sample"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.