-
Notifications
You must be signed in to change notification settings - Fork 649
No documentation on accessing ENV variables from docker-entrypoint_initdb.d #257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There is an open issue on Mongo's Jira about this: https://jira.mongodb.org/browse/SERVER-4895 |
Thanks for the link @bchrobot But what is the purpose of defining users in |
If you need environment variables, I'd recommend using a .sh initdb file
instead of .js.
|
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 |
Shell scripts are also executed ( From this SO post it seems like something along the following lines would work:
# Executed during Mongo container startup, concatenates scripts to pass ENV var
mongo --eval "var secureUsername = '$ENV_APP_SERVER_USERNAME'" create_user-js
// Skipped during Mongo container startup due to lack of .js extension.
// Instead, is executed by above shell script.
let res = [
'use admin',
db.createUser({
user: secureUsername,
pwd: '12345',
roles: [
{
role: 'readWrite',
db: 'dummydb'
}
]
})
];
printjson(res); |
@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 |
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. #!/bin/bash
set -e
mongo <<EOF
use admin
db.createUser({
user: '$ENV_APP_SERVER_USERNAME',
pwd: '$ENV_APP_SERVER_PW',
roles: [{
role: 'readWrite',
db: 'dummydb'
}]
})
EOF |
Thanks @yosifkit This script works perfectly fine. |
Thanks @yosifkit |
No need for shebang, it's sourced. |
This works by means of the docker-entrypoint-initdb.d feature of the mongo Docker image: - https://hub.docker.com/_/mongo - https://github.com/docker-library/docs/blob/master/mongo/README.md#initializing-a-fresh-instance This feature runs scripts in the /docker-entrypoint-initdb.d directory when the container is created. These scripts can do things like initialising the database with default data. The scripts can be either MongoDB JavaScript files or shell scripts. JavaScript files (ending in .js) have to be written as explained here: https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/. They are executed as 'mongo <js-file>', as defined in the code here: https://github.com/docker-library/mongo/blob/master/4.1/docker-entrypoint.sh#L321. Shell scripts (ending in .sh) are sourced, as defined in the code here: https://github.com/docker-library/mongo/blob/master/4.1/docker-entrypoint.sh#L320 The problem with using JavaScript files is that they do not allow to read environment variables (see discussion here docker-library/mongo#257). However, with shell scripts this is easily possible. For this reason, the current solution uses a shell script (init-data.sh) to insert the default data into the database. The init-data.sh script is included in the database container image by creating a new custom image on top of the MongoDB image that simply adds the init-data.sh file to the /docker-entrypoint-initdb.d directory. The database and collection to use is defined in the config-db.yaml ConfigMap, which is used to initialise env variables for the database pod (as well as for the webserver pod).
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. 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. |
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 |
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. |
A function named _getEnv("MONGO_INITDB_ROOT_USERNAME") |
But only since 4.7.0 ! |
To access env variables from docker-entrypoint-initdb.d Add env variables to mongo's container either via
db-init.js content would be:
|
Some time ago, I updated my answer. |
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
The text was updated successfully, but these errors were encountered: