How do I set the password of an account in a Docke

2019-07-25 09:35发布

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.

标签: docker
1条回答
Rolldiameter
2楼-- · 2019-07-25 10:17

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.

查看更多
登录 后发表回答