Docker replicate UID/GID in container from host

2019-02-07 08:11发布

When creating Docker containers I keep running into the issue of the UID/GID not being reflected in the container (I realize this is by design). What I am looking for is a way to keep host permissions reasonable and / or to replicate the UID/GID from the host user / group accounts in my Docker container. For instance:

host -

woot4moo:x:504:504:woot4moo:/home/woot4moo:/bin/bash

I would like this same behavior in the Docker container. That being said, is this even the right way to do this type of thing? My belief is I could simply run:

useradd -u 504 -g 504 woot4moo

as part of my Dockerfile, but I am not sure if that is valid.

2条回答
We Are One
2楼-- · 2019-02-07 08:31

You wouldn't want to run that as part of the image build process (in your Dockerfile), because the host on which someone is running a container is often not the host on which you are building the image.

One way of solving this is passing in UID/GID information via environment variables:

docker run -e APP_UID=100 -e APP_GID=100 ...

And then have an ENTRYPOINT script that includes something like the following before running the CMD:

useradd -c 'container user' -u $APP_UID -g $APP_GID appuser
chown -R $APP_UID:$APP_GID /app/data
查看更多
唯我独甜
3楼-- · 2019-02-07 08:37

I had similar issues and typically included entrypoint scripts in every image as it has already been mentioned (using https://github.com/ncopa/su-exec for interactive terminal programs). However, I kept repeating the same steps in multiple Dockerfiles. But after I used "docker.inside" from Jenkins Pipeline which does the user id handling auto-magically, I decided to build a Python 3 package based on docker-py to do this in a (hopefully) similar way (with some extended features I found helpful):

https://github.com/boon-code/docker-inside

I realize that the post is rather old; Maybe it's still helpful to someone with the same problem...

查看更多
登录 后发表回答