Search code examples
mongodbcontainerspymongosingularity-container

Unable to write to mongo instance running as a Singularity Container


I have been following the repo: https://github.com/singularityhub/mongo for building mongo container using Singularity.

By following all the steps mentioned in the repo:

git clone https://www.github.com/singularityhub/mongo
cd mongo
sudo singularity build mongo.sif Singularity
mkdir data/db
singularity run --bind $PWD/data/db:/data/db mongo.sif --auth

I am able to successfully run the container.

Now, using pymongo I can connect to the instance of the MongoDB:

from pymongo import MongoClient
client = MongoClient("mongodb://127.0.0.1:27017")
t = client['temp_db3']
print(client.list_database_names()) ### this returns an empty list
c = t['temp_collection1']
c.insert_one({"data":'123'})

When I try to insert some data into the collection, I get the authorization error:

Error Message: "OperationFailure: not authorized on temp_db3 to execute command { insert: "temp_collection1", ordered: true, lsid: { id: UUID("206a6f86-2465-4740-a92e-2e36b65c70f4") }, $db: "temp_db3", $readPreference: { mode: "primary" } }, full error: {'ok': 0.0, 'errmsg': 'not authorized on temp_db3 to execute command { insert: "temp_collection1", ordered: true, lsid: { id: UUID("206a6f86-2465-4740-a92e-2e36b65c70f4") }, $db: "temp_db3", $readPreference: { mode: "primary" } }', 'code': 13, 'codeName': 'Unauthorized'}"

Error screenshot: https://user-images.githubusercontent.com/43536129/147574920-c30a533a-e535-4430-bd8c-0cec6d610819.png

What should I do such that I am able to write to the collections of this MongoDB instance running as the container?


Solution

  • Getting rid of --auth worked (Just like @Joe suggested in the comments). Need to make sure that data/db directory has correct read-write privileges.