12

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 postgres containers (as I need to link both) but still the django app is not able to connect to mongodb on default settings. My django-compose.yml file contents are copied below:

    db1:
      image: postgres
    db2:
      image: mongo
      ports:
        - "27017:27017"
    web:
      build: .
      command: python manage.py runserver 0.0.0.0:8000
      volumes:
        - .:/code
      ports:
        - "8000:8000"
      links:
        - db1
        - db2

It does connect with postgres with default settings. I can also telnet to the mongodb port locally. Still, I get this error on starting the web container:

File "/usr/local/lib/python2.7/site-packages/mongoengine/connection.py", line 124, in get_connection web_1 | raise ConnectionError("Cannot connect to database %s :\n%s" % (alias, e)) web_1 | mongoengine.connection.ConnectionError: Cannot connect to database default : web_1 | [Errno 111] Connection refused

PS: I had successfully started a django-postgres connected app on my localhost, but it failed connecting to db, on an AWS instance. That is another problem I still need to get to root of.

5 Answers 5

8

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)
  • With DB2 being the name of your service in docker-compose.yml
  • 27017 being the port of the exposed service.
  • More about docker-compose environment variables
  • I'd put that in my settings file. But you are free to put it wherever you think it's appropriate depending on your project architecture

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")
3
  • On host machine execute docker-compose run yourappname env, look for ip address which related to your database image, thats what you use to connect to your database running in a container. Nov 19, 2015 at 11:46
  • this works for me like a charm, and just for the record, when using the same strategy for PORT, remember to transform it with int, something like: ... port=int(MONGODB_PORT)...
    – jfunez
    Feb 26, 2016 at 17:21
  • Where is the canonical place to write this code? In settings.py?
    – Frikster
    Oct 15, 2021 at 3:24
3

You should specify host name like in the docker compose file, instead of IP address.

I've faced similar problems in connecting from a Tornado Web app to Mongo DB. Here is my docker-compose.yml:

web:
  build: .
  ports:
   - "8888:8888"
  volumes:
   - .:/code
  links:
   - db
db:
  image: mongo:3.0

Here is my connection string:

motorengine.connect("db", host='db', port=27017, io_loop=io_loop)

My error was to specify IP address instead of host name (db) like in the docker compose file.

2

I managed to make this connection with the following configuration:

docker-compose.yml

version: "3.9"
services:
mongodb:
  environment:
    MONGO_INITDB_DATABASE: mydatabase
    MONGO_INITDB_ROOT_USERNAME: root
    MONGO_INITDB_ROOT_PASSWORD: password
  hostname: mongodb
  image: mongo
  ports:
    - "27017:27017"
  volumes: 
    - ./data-mongodb:/data/db
  
api:
  build: .
  command: "python manage.py runserver 0.0.0.0:8000"
  volumes:
    - .:/code
  ports:
    - "8000:8000"
  depends_on:
    - mongodb

Dockerfile

# syntax=docker/dockerfile:1
FROM python:3
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . /code/

requirements.txt

asgiref==3.4.1
Django==3.2.7
django-annoying==0.10.6
djangorestframework==3.12.4
djongo==1.3.6
psycopg2==2.9.1
psycopg2-binary==2.9.1
pymongo==3.12.1
pytz==2021.3
six==1.16.0
sqlparse==0.2.4

settings.py

DATABASES = {
    'default': {
      'ENGINE': 'djongo',
      'NAME': 'mydatabase',
      'CLIENT': {
          'host': 'mongodb://mongodb:27017',
          'username': 'root',
          'password': 'password',
          'authSource': 'admin',
          'authMechanism': 'SCRAM-SHA-1',
      }
  }
}

In this, the model used is from Djongo instead of from django.db import models

models.py

from djongo import models

After the build, you need to enter the api container and run the ./manage.py migrate

1

The problem you face is that the application container and the DB container start independently from each other and most importantly, the app container won't wait until the DB container is started. There is as of docker compose 1.1.0 no feature which would allow to consider such dependencies.

This is a well known problem and has been discussed already. Consensus seems to be that this should not be solved on a docker compose level but in docker itself. There is a proposal already for the groundwork in docker.

In my case, I just built this kind of intelligence in the application itself. I check for port connectivity until successful and then start the rest of the application.

4
  • Thanks for reply vanthome. I also realized this problem soon after posting this message and made a tweak to check if the mongo container is ready before the django container connects to it. That may have been another problem, but still I could not connect. Only possible way I could connect was without using docker-compose. I just started these 2 containers independently, with ip of mongo container specified in my django settings. And it works in this case. But then how is docker-compose useful?
    – anuprag
    Mar 24, 2015 at 7:43
  • Docker compose serves a different aspect. It is not an orchestrator it is just a way to launch and link a bunch of containers in a simple way.
    – vanthome
    Mar 24, 2015 at 11:06
  • I am facing a similar problem to @anuprag, and it doesn't seem to be the solution either. If I docker-compose run web /bin/bash and ping 172.17.0.52 (which is the IP of the DB_1_PORT environment variable), I get 92 bytes from 209d72d65b0c (172.17.0.53): Destination Host Unreachable, which would mean it is the whole mongodb container that is not up. Oct 7, 2015 at 4:56
  • I cannot connect ever starting it independently: docker-compose -p 27017: 27017 mongo mongo returns the same error Nov 12, 2015 at 12:47
1

I was able to containerize Django and MongoDB, connect both containers. I used Dockerfiles to build both containers and docker run to start the containers and have them connected. Just follow the steps in this repo. It was necessary for me to use Dockerfiles to have more power over the installed versions of the needed libraries because the latest versions of Django and mongoengine are not compatible. The stable working versions are

Django==1.10.0
pymongo==2.7.1
six==1.10.0
mongoengine==0.9.0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.