Skip to content

Instantly share code, notes, and snippets.

@innyso
Created June 22, 2016 03:04
Show Gist options
  • Save innyso/08daf4ea729d7932d682ae390bfb965b to your computer and use it in GitHub Desktop.
Save innyso/08daf4ea729d7932d682ae390bfb965b to your computer and use it in GitHub Desktop.
Support Arbitrary user ids

When running container in container with arbitrary user id but you want a proper uid to perform task like git pull or any runnable container task.

User nss_wrapper

in Dockerfile

yum install nss_wrapper
.....
command ["./startup.sh"]

in startup.sh

.....
function generate_passwd_file() { 
  USER_ID=$(id -u)
  GROUP_ID=$(id -g)

  if [ x"$USER_ID" != x"0" -a x"$USER_ID" != x"997" ]; then

    NSS_WRAPPER_PASSWD=passwd
    NSS_WRAPPER_GROUP=/etc/group

    cp /etc/passwd $NSS_WRAPPER_PASSWD
    echo "default:x:${USER_ID}:${GROUP_ID}:Default:${HOME}:/sbin/nologin" >> $NSS_WRAPPER_PASSWD

    export NSS_WRAPPER_PASSWD
    export NSS_WRAPPER_GROUP
    export LD_PRELOAD=libnss_wrapper.so
  fi
}

generate_passwd_file
exec java -jar something.jar
Reference
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment