You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I've opened a SO thread, but I guess I can ask it here as well.
I want to create an Image that would use mongo:latest, but would initialize mongod with --replSet, so that I can later (in the custom Dockerfile) call rs.initiate() in the mongo shell.
Hi @gioragutt. Have you resolved this ticket?
I have a similar requirement (run mongo with --replSet and initiate replica set), made several attempt to get it working, but it's still not working. See #249 for details
Ok now I understand and here is my answer 2.0.
I think you can't do that because this configuration is written inside the database itself which would require to start MongoDB before actually starting MongoDB...
In theory you could replace the CMD by a bash script, detect the first run and start this command once then start the mongod. But it really violates the Docker philosophy and you don't really want to go through this.
On top of that, the command line has to change (because it contains the host name) and the 3 nodes would not be identical because this has to be sent to only one of the 3 servers.
Moreover, if I understood correctly your real problem : you want to run MongoDB on AWS and MongoDB already has a DBaaS : MongoDB Atlas.
It's a fully managed service and as tons of advantages versus running it yourself on AWS.
Since the data is normally in a volume (/data/db), you'll need to define a different folder in order to be able to docker commit it. Warning that this could be extremely bad for performance since the DB files will be part of the copy-on-write filesystem employed by docker images.
FROM mongo:3.6
CMD [ "--dbpath", "/some/path/outside/data/db/", "--replSet", "myrepl" ]
As far as embedding the data using a RUN line, it would be a fairly complex set of running the mongod server in the background (probably using the entrypoint script) and then waiting for it to finish starting, calling rs.initiate, and then shutting down then backgrounded mongod. You might be able to use the /docker-entrypoint-initdb.d/ folder to have the entrypoint automatically run your js file for rs.initiate, but then you'll have to guess when the entrypoint is done and mongo is ready to be stopped.
@yosifkit the only thing that is hard here is the part of running mongod with --replSet. I want it to be done in the Dockerfile, so that I don't have to do anything after the container is running.
AFAIK, running commands on the mongod instance from Dockerfile isn't really an issue.
If you don't want to commit the data to the image then this will work for your first cluster member (since the initdb.d scripts are only run when the data directory is empty).
FROM mongo:3.6
RUN echo "rs.initiate();" > /docker-entrypoint-initdb.d/replica-init.js
CMD [ "--replSet", "myrepl" ]
Reacted by Lev Shagalov, Boboss74, Johannes Wienke, dhilipb, Sam de Vries, Kirill Khazan, Suleman Hasib, Viktor Isaev, Ash, Suleman Chikhalia and 1 more
Reacted by Youfu Zhang, Wojciech Gębiś and Joshua Barnett
This will work only for a standalone node started as a ReplicaSet.
You can't start the 3 nodes that way. They won't connect to each other. That would just create 3 one node replica sets
@MaBeuLux88 that's basically all I needed for now (or to be correct, then, since I don't even work in the company anymore).
@yosifkit can you elaborate on what's going on in the Dockerfile? I get lines 1 and 2, not sure why 3 is working. I'm not really proficient in Dockerfiles, so I don't understand what exactly happens in the CMD command.
So yes, you will need something more complex for the other nodes. But no, my hurried Dockerfile will not actually work since the host field in the replica config will be set to 127.0.0.1:27017.
@gioragutt, There is an entrypoint script that checks the args for a flag (like --replSet) and prepends mongod to the args.
When you do this (or the CMD from my Dockerfile above):
$ docker run -d --name mongo mongo:3.6 --replSet myrepl
What really is being run is the entrypoint script with the CMD as the arguments:
$ docker-entrypoint.sh --replSet myrepl
$ # which, after initialization, eventually ends with exec so that the bash process executing the shell script is replaced by mongod
$ # ie pid 1 of the contianer becomes mongod
$ exec mongod --replSet myrepl
Adding automation for setting up a replica set is not something we want to add to the image since it requires an external service like consul in order to reliably coordinate which is the first and where to join.
If you don't want to commit the data to the image then this will work for your first cluster member (since the initdb.d scripts are only run when the data directory is empty).
FROM mongo:3.6
RUN echo "rs.initiate();" > /docker-entrypoint-initdb.d/replica-init.js
CMD [ "--replSet", "myrepl" ]
This should be in documentation. A lot of devs will start mongo in docker and change streams (that requires replica set) is feature by it self, without multiple nodes.
Reacted by Boboss74, uday-awv, Sam de Vries, Giora Guttsait, Daithi Coombes, André Stylianos Ramos, Grant Carthew, Tobias Bergkvist, James Andrew-Smith, Ash and 4 more
Reacted by Youfu Zhang, Wojciech Gębiś, Joshua Barnett and Pablo Erhard
If you don't want to commit the data to the image then this will work for your first cluster member (since the initdb.d scripts are only run when the data directory is empty).
FROM mongo:3.6
RUN echo "rs.initiate();" > /docker-entrypoint-initdb.d/replica-init.js
CMD [ "--replSet", "myrepl" ]
Do you happen to know why if I run it as a Dockerfile (and then try to reconnect to the container) everything is fine and the prompt says (in my case) rs0:PRIMARY> while if I put it in a docker-compose.yml like in the following a get a rs0:OTHER>
I did it and I get Our replica set config is invalid or we are not a member of it.
I think the problem is related to what is written in this issue #249, but the solution provided is not working for me.
Do you have any suggestions?
Here is a little wait-for-it pattern written if python is already installed on your test container and you don't want to install mongo straight in that container. May be useful for some.
Here's another (probably cleaner than above) solution using a wait-for-it pattern. Idea is to mount the volume with mongo executable from the mongo container into the waiting container, so that there is no need to have mongo installed on that container image.
But no, my hurried Dockerfile will not actually work since the host field in the replica config will be set to 127.0.0.1:27017
If I remember correctly, this would make the node inaccessible from other containers and only work from the Docker host with an exposed port on 27017 (docker run --p 27017:27017), so this would not be useful for the docs.
I've tried this every which way to no avail. I'm looking for a docker-compose that can setup my app with replica set with the following:
1 - initdb - create some database that my app will use with its initial user
2- initialize a replica set
3- ensure one node (the only node needed for dev) can authenticate with itself (had errors that pointed to the need for -auth option)
I had 1 and 2 working, but I noticed when calling createIndex, I had a huge backlog of long running commands with db.currentOp(true).
Activity
myashchenko commentedon Mar 4, 2018
Hi @gioragutt. Have you resolved this ticket?
I have a similar requirement (run mongo with --replSet and initiate replica set), made several attempt to get it working, but it's still not working. See #249 for details
crapthings commentedon Apr 16, 2018
@gioragutt @YashchenkoN
same here
MaBeuLux88 commentedon Apr 17, 2018
I really don't see the problem guys. I did it like this.
Problem solved?
Most of the options on my
docker runlines are not mandatory.Works like a charm for me at least on Debian.
You could also create your own image (with a Dockerfile) and insert a new YML conf file and use it to start your MongoDB.
gioragutt commentedon Apr 17, 2018
@MaBeuLux88 this is a script. Our question is how do we create a Dockerfile for an image that does this out of the box.
MaBeuLux88 commentedon Apr 17, 2018
Ok now I understand and here is my answer 2.0.
I think you can't do that because this configuration is written inside the database itself which would require to start MongoDB before actually starting MongoDB...
In theory you could replace the CMD by a bash script, detect the first run and start this command once then start the mongod. But it really violates the Docker philosophy and you don't really want to go through this.
On top of that, the command line has to change (because it contains the host name) and the 3 nodes would not be identical because this has to be sent to only one of the 3 servers.
Moreover, if I understood correctly your real problem : you want to run MongoDB on AWS and MongoDB already has a DBaaS : MongoDB Atlas.
It's a fully managed service and as tons of advantages versus running it yourself on AWS.
Example of features you would miss :
yosifkit commentedon Apr 17, 2018
Since the data is normally in a volume (
/data/db), you'll need to define a different folder in order to be able todocker commitit. Warning that this could be extremely bad for performance since the DB files will be part of the copy-on-write filesystem employed by docker images.As far as embedding the data using a
RUNline, it would be a fairly complex set of running the mongod server in the background (probably using the entrypoint script) and then waiting for it to finish starting, callingrs.initiate, and then shutting down then backgrounded mongod. You might be able to use the/docker-entrypoint-initdb.d/folder to have the entrypoint automatically run yourjsfile forrs.initiate, but then you'll have to guess when the entrypoint is done and mongo is ready to be stopped.gioragutt commentedon Apr 17, 2018
@yosifkit the only thing that is hard here is the part of running
mongodwith--replSet. I want it to be done in theDockerfile, so that I don't have to do anything after the container is running.AFAIK, running commands on the
mongodinstance fromDockerfileisn't really an issue.yosifkit commentedon Apr 17, 2018
If you don't want to commit the data to the image then this will work for your first cluster member (since the initdb.d scripts are only run when the data directory is empty).
MaBeuLux88 commentedon Apr 17, 2018
This will work only for a standalone node started as a ReplicaSet.
You can't start the 3 nodes that way. They won't connect to each other. That would just create 3 one node replica sets
gioragutt commentedon Apr 17, 2018
@MaBeuLux88 that's basically all I needed for now (or to be correct, then, since I don't even work in the company anymore).
@yosifkit can you elaborate on what's going on in the
Dockerfile? I get lines1and2, not sure why3is working. I'm not really proficient inDockerfiles, so I don't understand what exactly happens in theCMDcommand.yosifkit commentedon Apr 17, 2018
So yes, you will need something more complex for the other nodes. But no, my hurried Dockerfile will not actually work since the
hostfield in the replica config will be set to127.0.0.1:27017.@gioragutt, There is an entrypoint script that checks the args for a flag (like
--replSet) and prependsmongodto the args.When you do this (or the CMD from my Dockerfile above):
$ docker run -d --name mongo mongo:3.6 --replSet myreplWhat really is being run is the entrypoint script with the
CMDas the arguments:yosifkit commentedon May 8, 2018
Docs on using a config file or flags added in docker-library/docs#1127.
Adding automation for setting up a replica set is not something we want to add to the image since it requires an external service like
consulin order to reliably coordinate which is the first and where to join.This blog seems useful on setting up a replset (with
mongosconfig servers too): https://dzone.com/articles/composing-a-sharded-mongodb-on-docker.Edit: this one seems pretty simple: https://medium.com/lucjuggery/mongodb-replica-set-on-swarm-mode-45d66bc9245
Lewik commentedon Mar 4, 2019
This should be in documentation. A lot of devs will start mongo in docker and change streams (that requires replica set) is feature by it self, without multiple nodes.
14 remaining items
eloparco commentedon Apr 26, 2020
Do you happen to know why if I run it as a Dockerfile (and then try to reconnect to the container) everything is fine and the prompt says (in my case)
rs0:PRIMARY>while if I put it in adocker-compose.ymllike in the following a get ars0:OTHER>Is there a workaround to fix this and set the node as primary also when using docker-compose?
tianon commentedon Apr 27, 2020
I'd recommend running
rs.status()in your interactive shell to try and get more information about your replica set and what's going wrong.eloparco commentedon Apr 27, 2020
I did it and I get
Our replica set config is invalid or we are not a member of it.I think the problem is related to what is written in this issue #249, but the solution provided is not working for me.
Do you have any suggestions?
jdclarke5 commentedon May 12, 2020
Here is a little wait-for-it pattern written if python is already installed on your test container and you don't want to install mongo straight in that container. May be useful for some.
jdclarke5 commentedon May 13, 2020
Here's another (probably cleaner than above) solution using a wait-for-it pattern. Idea is to mount the volume with
mongoexecutable from the mongo container into the waiting container, so that there is no need to have mongo installed on that container image.shanmukha511 commentedon Dec 15, 2020
Hi @yosifkit
Is the below code can work with mongo:3.4.20
FROM mongo:3.4.20
RUN echo "rs.initiate();" > /docker-entrypoint-initdb.d/replica-init.js
CMD [ "--replSet", "myrepl" ]
it is not initialised by default as compared to 3.6?
tianon commentedon Dec 15, 2020
jshbrntt commentedon Dec 18, 2020
This gives me what I want.
docker run -it --rm mongo:4.0.20@sha256:dd4cbe24eb8233db92b871cc556b77efcc7f9e67bc9516579796d4d08818273e bash -c "set -m ; mongod --bind_ip_all --replSet rs0 & while ! 2> /dev/null > '/dev/tcp/0.0.0.0/27017'; do sleep 1; done ; mongo --eval 'rs.initiate()' ; fg 1"thready commentedon Apr 6, 2021
I've tried this every which way to no avail. I'm looking for a docker-compose that can setup my app with replica set with the following:
1 - initdb - create some database that my app will use with its initial user
2- initialize a replica set
3- ensure one node (the only node needed for dev) can authenticate with itself (had errors that pointed to the need for -auth option)
I had 1 and 2 working, but I noticed when calling createIndex, I had a huge backlog of long running commands with db.currentOp(true).
Thanks for any guidance!
refactor(api): modernize Docker and dev workflow
refactor(api): modernize Docker and dev workflow
refactor(api): modernize Docker and dev workflow
refactor(api): modernize Docker and dev workflow
refactor(api): modernize Docker and dev workflow
refactor(api): modernize Docker and dev workflow (#573)