§2023-11-03
- Setting the User in a Docker Container From the Host
These commands retrieve the user’s UID and GID and export them as shell environment variables named UID and GID. Furthermore, let’s look at the command to provide these values to the docker run:
$ export UID=$(id -u) --> UID is read-only, you can not change it
$ export GID=$(id -g)
$ docker run --rm \
--user $UID:$GID \
--workdir /home/$USER \
--volume /etc/group:/etc/group \
--volume /etc/passwd:/etc/passwd \
--volume /etc/shadow:/etc/shadow \
-i -t ubuntu
alexlai@905e45db2494:~$ id alexlai
uid=1026(alexlai) gid=1026(alexlai) groups=1026(alexlai),998(wheel),967(docker)
alexlai@905e45db2494:~$ pwd
/home/alexlai
alexlai@905e45db2494:~$ ls /home/
alexlai
alexlai@905e45db2494:~$ whoami
alexlai
alexlai@905e45db2494:~$ exit
exit