Reputation: 3267
I have done an experiment to check this out. The following is my Dockerfile:
FROM ubuntu:bionic
RUN echo $UID > a.out && echo "hello" > a.out
RUN useradd -m user
RUN echo $UID > b.out
USER user
The result of a.out and b.out is shown below.
user@39370ee77a16:/$ cat a.out
hello
user@39370ee77a16:/$ cat b.out
My question is:
Thanks!
Upvotes: 4
Views: 3451
Reputation: 1709
What has not been fully addressed in other answers (so far) is your second question:
Why $UID in Dockerfile shows nothing?
$UID
is a bash internal variable. When the image is created from the dockerfile, bash is not used, so $UID
is not defined, hence using it returns an empty string.
To make this answer complete, even if it has already been answered:
Is root the default user?
That depends on how the image in the FROM
statement was setup.
Upvotes: 3
Reputation: 59946
It depend upon the base image, but in you case the default user is root. you can verify from the base image or inside Dockerfile.
FROM ubuntu:bionic
RUN whoami
In your case, you see hello
because you override the content, try with
RUN echo $UID >> a.out && echo "hello" >> a.out
You can change user using
USER my_user
Upvotes: 5