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

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"?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source