I am facing problem using docker-compose to link a django container with postgres and mongo containers? I am trying to use \"docker-compose up\" which starts up the mongo and po
I ran into a similar problem but with another service (not MongoDB). I'm not sure of what I'm doing wrong but this is how I could solve it :
import os
import mongoengine
MONGODB_HOST = os.environ.get('DB2_PORT_27017_TCP_ADDR', '127.0.0.1')
mongoengine.connect(host=MONGODB_HOST)
DB2
being the name of your service in docker-compose.yml27017
being the port of the exposed service. UPDATE
Now docker-compose containers are reachable by other services using a hostname similar to their alias. link documentation :
Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified.
And that way you can connect to MongoDB like this:
import mongoengine
mongoengine.connect(host="db2")