Skip to content

Create mongodb home dir in useradd command #463

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

Closed

Conversation

ebolingerjc
Copy link

Updates Dockerfile with useradd -m command.

Fixes issue #323.

Users are annoyed when container commands try to save shell history and produce this message:

Error saving history file: FileOpenFailed: Unable to open() file /home/mongodb/.dbshell: No such file or directory

Users create brittle workarounds when the Dockerfile should just create the home directory when creating the mongodb user.

@@ -1,7 +1,7 @@
FROM placeholder

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN groupadd -r mongodb && useradd -r -g mongodb mongodb
RUN groupadd -r mongodb && useradd -m -r -g mongodb mongodb
Copy link
Member

Choose a reason for hiding this comment

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

Rather than creating a home directory for a system account, we should probably just set it somewhere that mongodb can write, like /tmp.

Like this:

Suggested change
RUN groupadd -r mongodb && useradd -m -r -g mongodb mongodb
RUN groupadd -r mongodb && useradd --home-dir /tmp -r -g mongodb mongodb

Or this?
(fully expand the arguments for clarity; adjust now-edited RUN line to match semicolon formatting of other lines)

Suggested change
RUN groupadd -r mongodb && useradd -m -r -g mongodb mongodb
RUN set -eux; \
groupadd --system mongodb; \
useradd --system --gid mongodb --home-dir /tmp mongodb

Maybe even add --gid and --uid with explicit number 999 (the current value) like docker-library/postgres#93 to groupadd and useradd, respectively.

Copy link
Member

Choose a reason for hiding this comment

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

Or maybe we just add ENV HOME /tmp so that any UID that the user runs as will work.

Copy link
Author

Choose a reason for hiding this comment

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

How about a home-dir of /var/mongodb, similar to other system accounts?

> docker run mongo cut -d: -f 6,1 /etc/passwd | sort -t : -k 2
bin:/bin
proxy:/bin
sync:/bin
sys:/dev
mongodb:/home/mongodb
_apt:/nonexistent
nobody:/nonexistent
root:/root
games:/usr/games
daemon:/usr/sbin
backup:/var/backups
man:/var/cache/man
gnats:/var/lib/gnats
list:/var/list
mail:/var/mail
irc:/var/run/ircd
lp:/var/spool/lpd
news:/var/spool/news
uucp:/var/spool/uucp
www-data:/var/www

Copy link
Author

Choose a reason for hiding this comment

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

Still need to create the home directory to address the original issue.
The mongodb user ID should not share a global HOME env var with any other accounts.

@ebolingerjc ebolingerjc changed the title Add -m option to useradd command Create mongodb home dir in useradd command Apr 15, 2021
@murphyj
Copy link

murphyj commented Jul 8, 2021

Hi - this still appears to be a problem in the latest version 4.4.6 I'm using.

We're using it in conjunction with MongoDB TestContainers. Will try a workaround to create the TestContainer using docker compose though this obviously isn't the cleanest workaround.

Any ideas when this will be merged?

@tianon
Copy link
Member

tianon commented Jul 8, 2021

As noted in #323, we're still looking for a reliable reproducer of an error:

From what I've seen, the error doesn't prevent anything from working, so it's really more of a warning -- is someone else seeing behavior different from that?

@tianon
Copy link
Member

tianon commented Jun 8, 2022

Fixed via #541 👍

@tianon tianon closed this Jun 8, 2022
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.

Error saving history file: FileOpenFailed: Unable to open() file /home/mongodb/.dbshell: Unknown error
4 participants