I am trying to use MongoDB with Docker compose in Windows powershell(Docker Desktop) with following docker-compose.yml:
version: '3.7'
services:
mongodb_container:
image: mongo:latest
container_name: mongodb
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=root
- MONGO_INITDB_DATABASE=test
ports:
- 27017:27017
volumes:
- mongodb_data_container:/data/db
volumes:
mongodb_data_container:
When I tried to authenticate database by using docker-compose up -d --build, docker exec -it <container_id> /bin/bash and mongo -u root -p root --authenticationDatabase test, It produced the following error with the log:
MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27017/?authSource=test&compressors=disabled&gssapiServiceName=mongodb
2020-04-23T14:22:56.890+0000 E QUERY [js] Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-04-23T14:22:56.893+0000 F - [main] exception: connect failed
2020-04-23T14:22:56.894+0000 E - [main] exiting with code 1
and when I tried to download a data from local database scheduled with airflow set up by docker-compose, it produced error with following logs:
[2020-04-23 14:30:15,763] {taskinstance.py:1128} ERROR - 127.0.0.1:27017: [Errno 111] Connection refused
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/airflow/models/taskinstance.py", line 966, in _run_raw_task
result = task_copy.execute(context=context)
File "/usr/local/lib/python3.5/dist-packages/airflow/operators/python_operator.py", line 113, in execute
return_value = self.execute_callable()
File "/usr/local/lib/python3.5/dist-packages/airflow/operators/python_operator.py", line 118, in execute_callable
return self.python_callable(*self.op_args, **self.op_kwargs)
File "/usr/local/airflow/dags/missing_title.py", line 32, in download_db
df = pd.DataFrame(list(collection3.find())).drop(['_id'], axis=1)
File "/usr/local/lib/python3.5/dist-packages/pymongo/cursor.py", line 1156, in next
if len(self.__data) or self._refresh():
File "/usr/local/lib/python3.5/dist-packages/pymongo/cursor.py", line 1050, in _refresh
self.__session = self.__collection.database.client._ensure_session()
File "/usr/local/lib/python3.5/dist-packages/pymongo/mongo_client.py", line 1810, in _ensure_session
return self.__start_session(True, causal_consistency=False)
File "/usr/local/lib/python3.5/dist-packages/pymongo/mongo_client.py", line 1763, in __start_session
server_session = self._get_server_session()
File "/usr/local/lib/python3.5/dist-packages/pymongo/mongo_client.py", line 1796, in _get_server_session
return self._topology.get_server_session()
File "/usr/local/lib/python3.5/dist-packages/pymongo/topology.py", line 485, in get_server_session
None)
File "/usr/local/lib/python3.5/dist-packages/pymongo/topology.py", line 209, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:27017: [Errno 111] Connection refused
How can I access the MongoDB database with docker-compose? (When I tried to download a data from the MongoDB database locally with pymongo and Jupiter notebook outside docker, I can)