Home > Mobile >  echo $UID gives host ID and not container root user ID (docker/linux)
echo $UID gives host ID and not container root user ID (docker/linux)

Time:02-19

My current user ID on my host (ubuntu) is:

$ echo "Host UID = $UID"
Host UID = 1000

But when I run this docker container I get the same UID:

$ docker run --rm --name test-container centos:7.9.2009 /bin/bash -c "echo $UID"
1000

If I instead run the container interactively I get 0 as expected:

$ docker run -it --rm --name test-container centos:7.9.2009
[root@4a79cb82abec /]# echo $UID
0

Why do I get the wrong (my host user) UID when I run the container with /bin/bash -c "echo $UID"?

CodePudding user response:

You want to get UID from inside the container. However the variable UID is expanded on the host's shell itself. Use single quote to avoid it getting expanded:

docker run --rm --name test-container centos:7.9.2009 /bin/bash -c 'echo $UID'
  • Related