Answer the question
In order to leave comments, you need to log in
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,
}
}
Answer the question
In order to leave comments, you need to log in
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:
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. connect("mongodb://mongodb:27017/");
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question