ENV USERNAME ros
RUN adduser --ingroup sudo --disabled-password --gecos "" --shell /bin/bash --home /home/$USERNAME $USERNAME
RUN bash -c 'echo $USERNAME:ros | chpasswd'
ENV HOME /home/$USERNAME
My goal is to have an account with sudo privileges.
The context is that I am trying to create a Docker image with a working installation of ROS. The ROS installation is complicated and includes running many commands and scripts. I know the above statement is probably wrong because it is creating files with owner ros
and group sudo
.
Although I agree with the comments saying that in the majority of cases you avoid sudo in Docker, I also had the same situation where I needed to have a non-root user with passwordless sudo privileges inside Docker.
This is my Dockerfile that defines a sudo user
This is the relevant part from it:
# Set a passwordless sudoer user
RUN adduser --disabled-password --gecos "" ubuntu && \
usermod -aG sudo ubuntu && \
echo "%sudo ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/nopasswd
# Start the container as the user
WORKDIR /home/ubuntu
USER ubuntu
CMD bash
Hope this helps.