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
{{ message }}
Repository navigation
No documentation on accessing ENV variables from docker-entrypoint_initdb.d #257
As the name said, i couldn't find a way of accessing env variables from docker.
What i need is to access some var that is defined in docker-compose file over here:
let res = [
'use admin',
db.createUser({
user: 'apUser', // it should be this: ENV_APP_SERVER_USERNAME
pwd: '12345',
roles: [
{
role: 'readWrite',
db: 'dummydb'
}
]
})
];
printjson(res);
This is the content of javascript file that gets copied into docker-entrypoint-inidb.d
Reacted by Marc Bornträger, Gaurav , x-yuri, Noufal Valapra, anoriqq, Storm Muller, Shubham Sharma, Alejandro Heredia, ronishraphael, TheBadong and 9 more
But what is the purpose of defining users in docker-entrypoint_initdb.d if you can't even access the password through env variables?
How else would one securely set passwords?
# Executed during Mongo container startup, concatenates scripts to pass ENV var
mongo --eval "var secureUsername = '$ENV_APP_SERVER_USERNAME'" create_user-js
/docker-entrypoint-initdb.d/create_user-js:
// Skipped during Mongo container startup due to lack of .js extension.// Instead, is executed by above shell script.letres=['use admin',db.createUser({user: secureUsername,pwd: '12345',roles: [{role: 'readWrite',db: 'dummydb'}]})];printjson(res);
Reacted by dalyonushkin, IPetrik, Leo Borai, ahmedayyad89, Berk Soysal, Erik Zatloukal and x-yuri
@tianon if the above seems like a reasonable approach I will add it as an example for ENV usage in the 'Initializing a fresh instance' section in my docker-docs PR
Reacted by Marc Bornträger, Damien B., anoriqq, Brahim Afassy and Ilya Kushlianski
I am quite sure you can just put it in one file and just use bash to insert the variables directly into the js. I don't see this as a necessary addition to the docs.
Reacted by Tianon Gravi, Benjamin Chrobot, Marc Bornträger, dalibor, Guillaume Hamoniez, Ryan Bennett, Kellen Schmidt, 范晓, Shawn Moore, Gaurav and 39 more
Reacted by Tianon Gravi, Benjamin Chrobot, Marc Bornträger, Adam, Damien B., Pierre-Louis Gottfrois, Kanji Tanaka, anoriqq, Shubham Sharma, Link and 8 more
Thanks @yosifkit
But may I ask why you don't want to put this in the docs? The fact that we had to open an issue to get this information is proof for me that the info is not easily accessable.
Reacted by Barak BD, Rüdiger Schulz, Damien B., Alexander Zhelyazov, Daniel Weibel, Jas, jakubblaha-ids, Erik Zatloukal, scipilot, Gurmit Teotia and 6 more
edit: sorry I found the stupid error; I had a typo I did not notice and was importing badly my environment variable, sorry for the trouble! I am going to run it now through the entire CI/CD flow and see if it works.
Sorry @yosifkit I am trying your code snippet, however, it does not work. Has this changed?
I have an initDatabase.sh file with the following content:
FWIW, here's my answer on Stack Overflow. Although I'd avoid using Docker Swarm (buggy) and secrets (environment variables are easier to use). Also from a cursory glance at docker-entrypoint.sh, it looks like without MONGO_INITDB_ROOT_USERNAME, MONGO_INITDB_ROOT_PASSWORD authentication is disabled, so no point in creating a user. On the other hand if you do specify the variables, you've got to authenticate first (before creating a user), as I did in my example. Whether it makes sense to create a non-root user if the database is accessed just by one application... I'm not sure.
Thanks @x-yuri Indeed at the end I just placed those variables (INITDB_ROOT_blabla) because as you pointed out, without them, auth is not enabled and I was having issues connecting because of that.
Some time ago, I updated my answer. _getEnv() is undocumented and available for a limited range of versions (around >=4.4, <5 probably). Starting with 6.x you can use process.env. Before that with the help of jq you can achieve pretty much the same result. At least I don't see any issues with it.
Activity
bchrobot commentedon Mar 23, 2018
There is an open issue on Mongo's Jira about this: https://jira.mongodb.org/browse/SERVER-4895
BorntraegerMarc commentedon Mar 23, 2018
Thanks for the link @bchrobot
But what is the purpose of defining users in
docker-entrypoint_initdb.dif you can't even access the password through env variables?How else would one securely set passwords?
tianon commentedon Mar 23, 2018
BorntraegerMarc commentedon Mar 23, 2018
ah so with a shell script it is possible. Thanks for the info @tianon Nice! So to convert the JS into a .sh script I would have written:
Is that correct? Without the whole
printjsonthing...bchrobot commentedon Mar 23, 2018
Shell scripts are also executed (
/docker-entrypoint-initdb.d/*.sh) so you could access ENV variables there.From this SO post it seems like something along the following lines would work:
/docker-entrypoint-initdb.d/create_secure_user.sh:/docker-entrypoint-initdb.d/create_user-js:bchrobot commentedon Mar 23, 2018
@tianon if the above seems like a reasonable approach I will add it as an example for ENV usage in the 'Initializing a fresh instance' section in my docker-docs PR
yosifkit commentedon Mar 23, 2018
I am quite sure you can just put it in one file and just use bash to insert the variables directly into the js. I don't see this as a necessary addition to the docs.
dlipovac012 commentedon Mar 26, 2018
Thanks @yosifkit This script works perfectly fine.
BorntraegerMarc commentedon Mar 26, 2018
Thanks @yosifkit
But may I ask why you don't want to put this in the docs? The fact that we had to open an issue to get this information is proof for me that the info is not easily accessable.
x-yuri commentedon Nov 28, 2018
No need for shebang, it's sourced.
weibeld commentedon Apr 2, 2019
Thanks @x-yuri to point to this location in the code, that's great information.
For short commands, you could also use a pipe instead of a heredoc to send the commands to
mongo's stdin:Automatically create data in DB on startup
javierguzman commentedon Oct 30, 2020
edit: sorry I found the stupid error; I had a typo I did not notice and was importing badly my environment variable, sorry for the trouble! I am going to run it now through the entire CI/CD flow and see if it works.
Sorry @yosifkit I am trying your code snippet, however, it does not work. Has this changed?
I have an initDatabase.sh file with the following content:
I have in my shell the environment variables defined, but if I run the script it does not go ok, the error is:
Error: couldn't add user: User passwords must not be empty
What am I missing?
Thank you very much in advance and regards.
x-yuri commentedon Oct 30, 2020
FWIW, here's my answer on Stack Overflow. Although I'd avoid using Docker Swarm (buggy) and secrets (environment variables are easier to use). Also from a cursory glance at
docker-entrypoint.sh, it looks like withoutMONGO_INITDB_ROOT_USERNAME,MONGO_INITDB_ROOT_PASSWORDauthentication is disabled, so no point in creating a user. On the other hand if you do specify the variables, you've got to authenticate first (before creating a user), as I did in my example. Whether it makes sense to create a non-root user if the database is accessed just by one application... I'm not sure.javierguzman commentedon Nov 1, 2020
Thanks @x-yuri Indeed at the end I just placed those variables (INITDB_ROOT_blabla) because as you pointed out, without them, auth is not enabled and I was having issues connecting because of that.
orgads commentedon Apr 21, 2021
A function named
_getEnvwas added here. Usage:garex commentedon Jul 30, 2021
But only since 4.7.0 !
ogostus commentedon Mar 23, 2023
To access env variables from docker-entrypoint-initdb.d
Add env variables to mongo's container either via
env_fileorenvironmentattributesdb-init.js content would be:
x-yuri commentedon Mar 23, 2023
Some time ago, I updated my answer.
_getEnv()is undocumented and available for a limited range of versions (around >=4.4, <5 probably). Starting with 6.x you can useprocess.env. Before that with the help ofjqyou can achieve pretty much the same result. At least I don't see any issues with it.bacloud22 commentedon Mar 19, 2025