Reputation: 395
I'm trying to get a local docker setup running with a mongodb instance for a node API that I'm working on. I'm using the docker image found here.
My Dockerfile
looks as follows:
FROM mongo
ENV MONGO_INITDB_ROOT_USERNAME root
ENV MONGO_INITDB_ROOT_PASSWORD pw
ENV MONGO_INITDB_DATABASE foo
ADD scripts/init.js /docker-entrypoint-initdb.d/
What I would like to achieve, is on startup of my docker container, I would like to insert some dummy data to the mongodb, something like:
db.collection('bar').insertOne({foo: 'foo'});
db.collection('bar').insertOne({foo: 'bar'});
db.collection('bar').insertOne({foo: 'baz'});
In the official documentation, it mentions that
When a container is started for the first time it will execute files with extensions .sh and .js that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. .js files will be executed by mongo using the database specified by the MONGO_INITDB_DATABASE variable, if it is present, or test otherwise. You may also switch databases within the .js script.
Im running into several problems with this. Running db.collection('bar)
will tell me that db.collection is not a function
.
I've already looked at and tried many different solutions on stackoverflow (switching to docker-compose, using different scripts, creating new users?, etc etc), but I still don't have anything running locally.
How can I simply insert some dummy data in the mongo db on startup? I'd very much prefer to stick with a simple Dockerfile
, and not switch to anything different. I would just like to get my init script to run, and insert the data with a couple of db.collection('bar').insertOne({})
.
Additionally, in the Docker dashboard (on mac), when I click on my container, and click on the "CLI" button, and type the following:
⚡ docker exec -it 8430a5d39fbfd5e77a034bb563e3ebcaabbd6736c62121740420a190804fcef /bin/sh; exit
# mongo
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("58a59bd0-2fe5-4345-95a3-b78c70f65707") }
MongoDB server version: 4.4.1
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
> show dbs
> show collections
Warning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus
> db.auth("root", "pw")
Error: Authentication failed.
0
>
I cant seem to authenticate, even though I'm setting the MONGO_INITDB_ROOT_USERNAME
and MONGO_INITDB_ROOT_PASSWORD
in my Dockerfile
, I would like to understand why this is happening and how to fix it.
Thanks for reading!
Upvotes: 1
Views: 5137
Reputation: 12827
Looking at the latest mongodb documentation, I cannot find any reference to db.collection(collectionName)
.
However, I do find references to db.createCollection(collectionName)
and db.getCollection(collectionName)
.
By modifying your script to use those functions instead, I can get the docker instance to start correctly. This is probably due to the fact that you use the FROM mongo:latest
image.
You can find the complete pull request here but in short, your Dockerfile is correct and the script should only use functions that are visible in the documentation.
The way to run a script on first startup in mongodb in indeed to use the COPY scripts/init.js /docker-entrypoint-initdb.d/
command.
What I am guessing happens is that the node library you are used to and the db
capabilities that the docker image use are different.
As for connecting to the database, if you want to avoid the admin login you should use the mongodb://user:password@mongourl:port/dbname
format.
In this specific case, something like mongodb://user:password@localhost:27017/customelements
Upvotes: 1