I am developing a django-react app and using a mongoDB cluster to store data. When I run the app without using docker, I am able to make requests to the database without issue. However, when I run the docker containers (one for my backend and one for my frontend) I run into this error on the backend:
File "/usr/local/lib/python3.9/site-packages/pymongo/topology.py", line 215, in _select_servers_loop
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 5f9ece0f7962ee81cb819b63, topology_type: Single, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:27017: [Errno 111] Connection refused')>]>
I have the mongodb host in both mongo_client.py and settings.py. In settings.py I have:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': '<mydb>',
'HOST': 'mongodb+srv://mike:<mypassword>@cluster0.5u0xf.mongodb.net/<mydb>?retryWrites=true&w=majority',
'USER': 'mike',
'PASSWORD': '<mypassword>',
}
}
My docker-compose yaml looks like:
version: "3.2"
services:
portalbackend:
restart: always
container_name: code
command: bash -c "python manage.py makemigrations &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
build:
context: ./PortalBackend/
dockerfile: Dockerfile
ports:
- "8000:8000"
networks:
- db-net
portal:
restart: always
command : npm start
container_name: front
build:
context: ./portal/
dockerfile: Dockerfile
ports:
- "3000:3000"
stdin_open: true
depends_on:
- portalbackend
networks:
- db-net
networks:
db-net:
driver: bridge
Do I need to create a container for mongodb? I originally tried that with a local mongodb instance but I was running into the same issue, so I tried rolling with a cluster. Still running into the same problem.