Skip to content

Instantly share code, notes, and snippets.

@anis-campos
Forked from renzok/Dockerfile
Last active October 17, 2018 19:30
Show Gist options
  • Save anis-campos/7bfb72333fd653abd3e3909c4557435b to your computer and use it in GitHub Desktop.
Save anis-campos/7bfb72333fd653abd3e3909c4557435b 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 imageName .
docker run --name uid-map --rm -ti -e UID=22222 -e GID=22222 imageName
#!/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 "${UID}" -a -z "${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=${UID:=$USER_ID}
USER_GID=${GID:=$USER_GID}
usermod -u USER_ID ${USER}
groupmod -g USER_GID ${USER}
usermod -g USER_ID ${USER}
exec su - "${USER}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment