R
R
RabbitRun92022-01-20 21:01:36
Django
RabbitRun9, 2022-01-20 21:01:36

How to connect a database in Docker?

A question. You need to run the Django + Mongo project in docker, in separate containers.
One container is a Django project. The second container is Mongo's base. (you can start without volume, just so that the database connects to the container with django and you can fully work with the project) .
I register in docker-compose and does not see the django mongo base in any way.

in the settings, the base is connected like this:

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'HOST': 'mongodb',
        'PORT': 27017,
    }
}


If anyone did, or there are working ready-made examples, tell me.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rag'n' Code Man, 2022-01-21
@RabbitRun9

version: "3.9"
services:
    server:
        container_name: "server"
        build: ./
        ports:
            - "4200:4200"
        depends_on:
            - mongodb
            - elastic
        restart: always
        links:
            - mongodb
            - elastic
        env_file: .env

    mongodb:
        container_name: "mongodb"
        image: mongo:4.4.7-focal
        restart: always
        volumes:
            - mongodb:/data/db
        environment:
            - MONGO_INITDB_ROOT_USERNAME=$MONGODB_ROOT_USER
            - MONGO_INITDB_ROOT_PASSWORD=$MONGODB_ROOT_PASSWORD
        ports:
            - "27017:27017"
        env_file: .env

    elastic:
        container_name: "elastic"
        image: elasticsearch:7.13.4
        restart: always
        environment:
            - discovery.type=single-node
            - cluster.name=elastic-crm
            - bootstrap.memory_lock=true
            - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
            - ELASTIC_USERNAME=$ELASTIC_USERNAME
            - ELASTIC_PASSWORD=$ELASTIC_PASSWORD
        ports:
            - "9200:9200"
        volumes:
            - elastic:/usr/share/elasticsearch/data
        env_file: .env
        
volumes:
    mongodb:
    elastic:

Here is an example of my config for one of the applications.
With help links, we attach these two services to the backend, and in the backend, instead of localhost, we use the name of the service. Well, we also specify the correct ports.
Accordingly, for Mongo it will be something like this:
connect("mongodb://mongodb:27017/");

S
spaceatmoon, 2022-01-20
@spaceatmoon

You need to set up communication between containers. depends_on only specifies the start order of containers.

//версия указана?
services:
  mongodb:
    image: mongo:latest
    restart: always
    ports: //порт ещё
      - 27017:27017
    networks:
            - site

  web:
    build: ./src
    restart: always
    command: python src/manage.py runserver 0.0.0.0:8000
    ports:
      - 8000:8000
    networks:
            - site

networks:
    site:
        driver: bridge

Inside the web container, you must access mongo db not through localhost, but by specifying the name of the container. Docker will crash.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question