Skip to content

Mongo user in dockerfile added as default system user uid. #181

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
jamesongithub opened this issue May 19, 2017 · 10 comments
Closed

Mongo user in dockerfile added as default system user uid. #181

jamesongithub opened this issue May 19, 2017 · 10 comments

Comments

@jamesongithub
Copy link

The dockerfile adds mongodb user as system user which in turn creates a user with a uid of 999.

image

On our host we happen to have another default system user with a uid of 999.

image

When we run the docker container it gets mapped to this user.

image

I'm wondering what people have done to get around this. Basically the container has all the rights of the mapped user now.

@yosifkit
Copy link
Member

While the mongo user is user 999, it can only access things given to it inside the container.

#81 added the ability to run as an arbitrary user, docker run --user some-other-user-id:some-group-id (up to you to ensure chosen user/group has access to any bind mounted files).

@jamesongithub
Copy link
Author

in this case. user 999 can access the container tho right.

i guess we'll go with a random uid then. thanks.

@jamesongithub
Copy link
Author

@yosifkit getting this running as another user

17:51:29 2017-05-22T17:51:28.895+0000 I CONTROL  [initandlisten] options: {}
17:51:29 2017-05-22T17:51:28.912+0000 I STORAGE  [initandlisten] exception in initAndListen: 98 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating

i'm not using a host volume

does the startup handle chowning the data directory to the user passed it?

@yosifkit
Copy link
Member

It cannot chown. If you start the container with --user to docker run, then it is not root and cannot chown files owned by another user. It seems that #81 did not chmod 777 the directory in the Dockerfile since that would be insecure for database files. So I think if you want to run as a specific user, you have to provide a data directory that it can write to.

We did add some notes for arbitrary users in postgres, but have not added anything for mongo, mysql, mariadb, or percona.

@jamesongithub
Copy link
Author

in that case, think id prefer to run it as the user starting the container, however in that case that user needs to exist in the container then

@mangalbhaskar
Copy link

mangalbhaskar commented Jun 18, 2019

Old thread, but posting the solution in case someone finds it useful. Currently, it has different options to get this working, but you need to justify your case. For example, in my case, the data volume is mapped to the mongodb docker container created using official mongodb docker image, it always creates the mongodb user with uid=999 and gid=999, which translates to the following problem.

Problem

  • In specific case of Kali Linux (this is what I am using): uid, gid 999 maps to user: systemd-coredump
  • /etc/passwd entry: systemd-coredump:x:999:999:systemd Core Dumper:/:/sbin/nologin
  • It's may not be the real problem because only the specific user only can edit/modify the data, but not sure about the side effects of using the systemd-coredump user outside the container but which was mapped to mongodb user inside the container

Solution
a) create a mongodb user and group on the host machine
b) pass the user, group, uid, gid as arguments to the official mongodb dockerfile
b) build the image by passing the values to the respective arguments

Details and scripts here

@superstes
Copy link

If someone comes across this again:
You can see how to re-build the official docker-image, to use a custom UID/GID, with minimal changes here:

Greetings

@tianon
Copy link
Member

tianon commented May 19, 2023

If that's really necessary, wouldn't something like this be simpler?

FROM mongo:6
RUN set -eux; \
	groupmod --gid 1234 mongodb; \
	usermod --uid 1234 mongodb

(At most, then adding chown -R mongodb:mongodb /var/log/mongodb /data/configdb /data/db afterwards 😅)

@superstes
Copy link

Could be a simpler solution, yes. Will test it.
Thanks for the feedback.

@superstes
Copy link

Tested and added the option + examples to my repo.
Thanks

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

No branches or pull requests

5 participants