Replicaset and Containers

Hi team,

I would like to know if it is possible to create a ReplicaSet of 3 nodes using 3 containers installed on 3 different virtual servers.

Regards
Olivier

Of course you may.

You may put your 3 nodes where ever you want, as long as they see each others and each client can reach all of them.

Ok. But if I use the container available on the MongoDB repository, I need to have the root password.

Do you know it ?

Source for installation:
Install MongoDB Community with Docker - MongoDB Manual v7.0

Which root password?

No. I know the root passwords of the machines I manage. That’s all.

Which steps is not working for you?

The step to start MongoDB as replicatset.

This commande :

# docker run -p 27017:27017 -d mongodb/mongodb-community-server:latest --name mongodb --replSet myReplicaSet
# docker ps -a
CONTAINER ID   IMAGE                                     COMMAND                  CREATED         STATUS                     PORTS                       NAMES
71562264d542   mongodb/mongodb-community-server:latest   "python3 /usr/local/…"   6 seconds ago   Exited (1) 4 seconds ago                               admiring_matsumoto
# mongosh
Current Mongosh Log ID: 6627c290f8acb181b2c934dc
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.4
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017

For some reasons your mongodb docker terminates:

So since no process are now listening to 217017 you get:

Did you hit Ctrl-C or something like that?

Do you have any error message?

No Ctrl-C. The error comes during the start of the container with the commande :

# docker run -p 27017:27017 -d mongodb/mongodb-community-server:latest --name mongodb --replSet myReplicaSet
34050cbebc0c83432cb1f2e8d68146cab2668913242686c17cc28c664b20e528
[root@svx-lab-03m ~]# docker ps -a
CONTAINER ID   IMAGE                                     COMMAND                  CREATED         STATUS                     PORTS                       NAMES
34050cbebc0c   mongodb/mongodb-community-server:latest   "python3 /usr/local/…"   6 seconds ago   Exited (1) 5 seconds ago                               condescending_moore

And the error is : 
# docker logs condescending_moore
Warning: File MONGO_INITDB_ROOT_USERNAME_FILE is deprecated. Use MONGODB_INITDB_ROOT_USERNAME_FILE instead.
Warning: File MONGO_INITDB_ROOT_PASSWORD_FILE is deprecated. Use MONGODB_INITDB_ROOT_PASSWORD_FILE instead.
Error parsing command line: unrecognised option '--name'
try '/usr/bin/mongod --help' for more information
Traceback (most recent call last):
  File "/usr/local/bin/docker-entrypoint.py", line 637, in <module>
    subprocess.run(get_final_command_line_args(), check=True)
  File "/usr/lib/python3.10/subprocess.py", line 526, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/usr/bin/mongod', '--name', 'mongodb', '--replSet', 'myReplicaSet', '--bind_ip_all']' returned non-zero exit status 2.

Thanks for your help
Regard

This seems to indicate the order of parameters is wrong. Try with the command published in the documentation your shared which is:

docker run --name mongodb -p 27017:27017 -d mongodb/mongodb-community-server:latest

Hi,

After made some new tests, now, I can run the container with activate the option --replicaset.

I use the following commande :

#docker run --name mongodb -p 27017:27017 -d mongodb/mongodb-community-server:latest --replSet myReplicaSet

So you move the --name option at the right place like I mentioned.

You may then mark my post as the solution.