2

Running a django app with mongodb using djongo. This is the docker-compose file:

version: '3.1'

volumes:
    mongo:

services:
  mongodb:
    image: mongo
    restart: always
    volumes:
      - 'mongo:/data/db'
    ports:
      - 27017:27017

  web:
    build: .
    restart: always
    depends_on:
      - 'mongodb'
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/django_mongodb_docker
    ports:
      - 8000:8000
    links:
      - 'mongodb'

The build succeeds and then gets stuck at the following:

web_1      | Watching for file changes with StatReloader
web_1      | Performing system checks...
web_1      | 

Any help would be appreciated!!!

Satwato Dey
  • 21
  • 1
  • 2

1 Answers1

0

I went through this too, 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
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