Basically, the title already says it all: I would like to run MongoDB as a single-node replica set using the official mongo Docker image, but I don't get how to do this.
First of all, I have started the Docker image with the additional --replSet "rs0"
parameter. Inside of the /docker-entrypoint-initdb.d/
directory I have added a startup script, which runs:
rs.initiate();
However, no matter whether I provide options or not, MongoDB starts (and it seems to start successfully), but I am note able to connect to it using the Node.js MongoDB driver. If I set the useUnifiedTopology
flag to true
, nothing happens at all (connecting just hangs), if I set it to false
, I get errors telling me that the connection timed out after 30 seconds.
Is there anything I am doing wrong fundamentally? Is it possible at all to run MongoDB as a replica set using the Docker image? Can anyone provide me some guidance?
UPDATE 1
When I run the database as described above, and then run the following command:
$ docker exec -it test-mongo bash
And then inside the container I run:
$ mongo
I get connected to the database. I then run the following commands:
var database = db.getSiblingDB('test-db');
database.auth('user', 'password');
database.getCollectionNames();
The first two succeed without any problems, the third tells me that listCollections
failed due to an uncaught exception with error message not master and slaveOk=false
.
This sounds as if the single database server is not its own master, is it?