Skip to content

Add "docker-entrypoint-initdb.d" behavior which mimics PostgreSQL, including (optional) automated "root" user creation #145

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

Merged
merged 1 commit into from
Mar 21, 2017

Conversation

tianon
Copy link
Member

@tianon tianon commented Mar 15, 2017

Closes #52
Closes #53
Closes #89

This ensures that the initialization scripts will only run once, and only offers the ability to create a user with the role of "root" -- authentication in MongoDB is fairly complex, so explicitly leaving more complex user setup to the user (via /docker-entrypoint-initdb.d/*.js or *.sh) seems prudent (essentially, only solving the most basic pain point of needing some way to invoke that initial db.createUser after enabling --auth).

@tianon
Copy link
Member Author

tianon commented Mar 15, 2017

Not sure what to do about Windows here. 😞

@GrapeBaBa
Copy link

Nice to see

done

if [ -z "$definitelyAlreadyInitialized" ]; then
"$@" --fork --bind_ip 127.0.0.1 --logpath "/proc/$$/fd/1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problem as mysql 😢

  -h [ --help ]                         show this usage information
  --version                             show version information

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh boo, that's cute:

$ docker run -it --rm mongo:3.4 --version
db version v3.4.2
git version: 3f76e40c105fc223b3e5aac3e20dcd026b83b38b
OpenSSL version: OpenSSL 1.0.1t  3 May 2016
allocator: tcmalloc
modules: none
build environment:
    distmod: debian81
    distarch: x86_64
    target_arch: x86_64

/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

db version v3.4.2
git version: 3f76e40c105fc223b3e5aac3e20dcd026b83b38b
OpenSSL version: OpenSSL 1.0.1t  3 May 2016
allocator: tcmalloc
modules: none
build environment:
    distmod: debian81
    distarch: x86_64
    target_arch: x86_64

MongoDB init process complete; ready for start up.

db version v3.4.2
git version: 3f76e40c105fc223b3e5aac3e20dcd026b83b38b
OpenSSL version: OpenSSL 1.0.1t  3 May 2016
allocator: tcmalloc
modules: none
build environment:
    distmod: debian81
    distarch: x86_64
    target_arch: x86_64

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be somewhat solved if we added MongoDB 3.2's check-and-retry-once logic to everything, since we'd then be at least verifying that the daemon actually did start up properly. Perhaps just adding the "check" portion of that would be sufficient?

Do you think we really need to go to the extreme of explicitly detecting --home and/or --version directly? (especially since there are other things that might cause the daemon to not start up correctly too)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check and retry on all versions would be a good addition, but won't it still print multiple times or at least be silly and print error: mongod does not appear to have started up quickly enough ... when a user was just getting --help/--version?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it'll print an extra error, but it'll only print once, and I think it still serves the purpose just fine -- users who don't want to run the daemon explicitly really ought to simply be bypassing our entrypoint entirely, IMO (but at least adding this would provide the expected information):

$ docker run -it --rm mongo:3.2 --version
db version v3.2.12
git version: ef3e1bc78e997f0d9f22f45aeb1d8e3b6ac14a14
OpenSSL version: OpenSSL 1.0.1t  3 May 2016
allocator: tcmalloc
modules: none
build environment:
    distmod: debian81
    distarch: x86_64
    target_arch: x86_64
error: mongod does not appear to have started up quickly enough -- perhaps it had an error?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explicitly? but that is 7 extra characters " mongod" 😉 ¯\_(ツ)_/¯

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR is updated to check whether mongod actually started in all versions now (and I updated 3.2's check to diff cleanly/consistently).

We're still providing the information they asked for, they just get some harmless extra error output (telling them that initdb failed due to the flags they requested we start it with). For interactive users, they'll usually be concerned about getting the flag name they need so they can either update their script or their ad-hoc invocation (which this provides), and if users are actually using --help or --version in a script, I really would doubly recommend they use --entrypoint or something to bypass our extra initdb behavior explicitly, but I don't see a real strong use case for putting those in scripts 😕.

IMO adding an explicit check for --version or --help is overkill (added lines to our script for small benefit), but I am willing to add them if you really insist and feel strongly that they provide useful value to our users (commensurate to the added maintenance burden of debugging/enhancing them if future versions of MongoDB change things, debugging our implementation, etc).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the final output, for context:

$ docker run -it --rm mongo:3.4 --version
db version v3.4.2
git version: 3f76e40c105fc223b3e5aac3e20dcd026b83b38b
OpenSSL version: OpenSSL 1.0.1t  3 May 2016
allocator: tcmalloc
modules: none
build environment:
    distmod: debian81
    distarch: x86_64
    target_arch: x86_64

error: mongod does not appear to have started up -- perhaps it had an error?

$ docker run -it --rm mongo:3.2 --version
db version v3.2.12
git version: ef3e1bc78e997f0d9f22f45aeb1d8e3b6ac14a14
OpenSSL version: OpenSSL 1.0.1t  3 May 2016
allocator: tcmalloc
modules: none
build environment:
    distmod: debian81
    distarch: x86_64
    target_arch: x86_64

error: mongod does not appear to have started up -- perhaps it had an error?

$ docker run -it --rm mongo:3.0 --version
db version v3.0.14
git version: 08352afcca24bfc145240a0fac9d28b978ab77f3

error: mongod does not appear to have started up -- perhaps it had an error?

$ 

@reypm
Copy link

reypm commented Mar 20, 2017

Is there any chance to automate DB creation as well? What's the status of this PR? I was about to open a issue regarding this but just found @tianon is already taking care of this

@tianon
Copy link
Member Author

tianon commented Mar 20, 2017

@reypm so, I don't think automating "db creation" really makes much sense, right? (since MongoDB is fundamentally designed for "create on first use")

Things like setting up indexes is something you'd have to script yourself, but this gives you the tools to do so (via *.sh or *.js files in this special directory).

@reypm
Copy link

reypm commented Mar 20, 2017

@tianon ok waiting on this PR to be merged and then use my own scripts

…cluding (optional) automated "root" user creation
@bchrobot
Copy link

Is https://github.com/docker-library/docs/blob/master/mongo/content.md the place to make a PR against to add documentation for these changes?

@yosifkit

@yosifkit
Copy link
Member

@bchrobot, yes, see #189 for the issue tracking the docs. My comment there is soon to be out of date since @tianon is actively working on a PR to make config files work with the temporary mongo used to create users.

@tianon
Copy link
Member Author

tianon commented Dec 20, 2017

See #225 for the PR which finally adds --config support to this! 🎉 🎈 🎄

@jeff-bothe
Copy link

in my custom /docker-entrypoint-initdb.d/*.js or *.sh script, is there a way to access env variables defined in a .env file? Meaning, env variables other than MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD.

thanks

@tianon
Copy link
Member Author

tianon commented Jun 18, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants