Skip to content

Instantly share code, notes, and snippets.

@renzok
Last active September 6, 2023 04:55
Show Gist options
  • Save renzok/29c9e5744f1dffa392cf to your computer and use it in GitHub Desktop.
Save renzok/29c9e5744f1dffa392cf to your computer and use it in GitHub Desktop.
docker: mapping host uid and gid to user inisde container
FROM debian:jessie
ENV USER=boatswain USER_ID=1000 USER_GID=1000
# now creating user
RUN groupadd --gid "${USER_GID}" "${USER}" && \
useradd \
--uid ${USER_ID} \
--gid ${USER_GID} \
--create-home \
--shell /bin/bash \
${USER}
COPY user-mapping.sh /
RUN chmod u+x user-mapping.sh
ENTRYPOINT ["/user-mapping.sh"]
docker build -t renzok/uid-map .
docker run --name uid-map --rm -ti -e HOST_USER_ID=22222 -e HOST_USER_GID=22222 renzok/uid-map
or
docker run --name uid-map --rm -ti -e HOST_USER_ID=$(id -u) -e HOST_USER_GID=$(id -g) -v ${HOME}/devel:/devel renzok/uid-map
#!/bin/bash
if [ -z "${USER}" ]; then
echo "We need USER to be set!"; exit 100
fi
# if both not set we do not need to do anything
if [ -z "${HOST_USER_ID}" -a -z "${HOST_USER_GID}" ]; then
echo "Nothing to do here." ; exit 0
fi
# reset user_?id to either new id or if empty old (still one of above
# might not be set)
USER_ID=${HOST_USER_ID:=$USER_ID}
USER_GID=${HOST_USER_GID:=$USER_GID}
LINE=$(grep -F "${USER}" /etc/passwd)
# replace all ':' with a space and create array
array=( ${LINE//:/ } )
# home is 5th element
USER_HOME=${array[4]}
sed -i -e "s/^${USER}:\([^:]*\):[0-9]*:[0-9]*/${USER}:\1:${USER_ID}:${USER_GID}/" /etc/passwd
sed -i -e "s/^${USER}:\([^:]*\):[0-9]*/${USER}:\1:${USER_GID}/" /etc/group
chown -R ${USER_ID}:${USER_GID} ${USER_HOME}
exec su - "${USER}"
@harschware
Copy link

"RUN chmod u+x user-mapping.sh" should be "RUN chmod u+x /user-mapping.sh" or will get "chmod: cannot access 'user-mapping.sh': No such file or directory"

@zirubak
Copy link

zirubak commented Oct 21, 2017

Thanks so much. It works well.

@Alger7w
Copy link

Alger7w commented May 17, 2018

So good, perfectly solved my problem

@juthikashenoy
Copy link

Can I pass in a different user in the run command ? I want to use a dummy user in the dockerfile and pass in the actual user .

@fikovnik
Copy link

fikovnik commented Jul 3, 2019

Thanks!

Is there any difference in directly editing the user file or using the following:

groupmod -g ${USER_GID} ${USER}
usermod -u ${USER_ID} -g ${USER_GID} ${USER}

@penyuan
Copy link

penyuan commented Feb 21, 2021

Awesome solution, thanks @renzok! To make it safe to use/share, do you mind adding a one-liner SPDX license info to these scripts? Just something really simple like this would be helpful:

# SPDX-License-Identifier: AGPL-3.0-or-later

@tq-niebelm
Copy link

Awesome solution, thanks @renzok! To make it safe to use/share, do you mind adding a one-liner SPDX license info to these scripts? Just something really simple like this would be helpful:

# SPDX-License-Identifier: AGPL-3.0-or-later

Also Many thanks for sharing this, please add a license info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment