I am trying to add non-root user to my docker container. Thanks to some great previous posts, I think I was able to make a valid non-root user by using docker file attached below.
Then I attempted to create a .bashrc in that user's home directory through the same dockerfile. Building the image was successful without any problems. However, I could not create the .bashrc to under $HOME_DIR by running the created image. As a note, the creation of the user and the home directory works fine.
Could you please tell me why it doesn't work?
FROM ubuntu:20.04
ENV TZ=Asia/Tokyo
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
ENV DEBCONF_NOWARNINGS=yes
ARG USERNAME=test
ARG GROUPNAME=test
ARG UID=1004
ARG GID=1004
ARG PASSWORD=test
ARG HOME_DIR=/home/$USERNAME/
RUN groupadd -g $GID $GROUPNAME && \
useradd -m -s /bin/bash -u $UID -g $GID -G sudo $USERNAME && \
echo $USERNAME:$PASSWORD | chpasswd
USER $USERNAME
WORKDIR $HOME_DIR
####
## The following lines do not work for me
####
RUN echo test >> $HOME_DIR/.bashrc
EDIT: adding command used to run the image:
sudo docker run --name [container name] \
-v /home/test/Work:/home/test -i -t --shm-size 30G [image name]
CodePudding user response:
When you build the image, you create a file called .bashrc in /home/test.
However, when you run the image, you map a directory on the host to /home/test.
When you do that, all the files in the image that are in /home/test become 'hidden' and replaced with the directory you map into the image.
You can verify that the file is in the image by running it without mapping the directory to /home/test. Then you will be able to see the .bashrc file in /home/test.
A solution could be to have the .bashrc file in the /home/test/Work directory on the host machine.