How to use sudo inside a docker container?

前端 未结 11 757
臣服心动
臣服心动 2020-11-30 16:17

Normally, docker containers are run using the user root. I\'d like to use a different user, which is no problem using docker\'s USER directive. But this use

11条回答
  •  旧巷少年郎
    2020-11-30 17:02

    Here's how I setup a non-root user with the base image of ubuntu:18.04:

    RUN \
        groupadd -g 999 foo && useradd -u 999 -g foo -G sudo -m -s /bin/bash foo && \
        sed -i /etc/sudoers -re 's/^%sudo.*/%sudo ALL=(ALL:ALL) NOPASSWD: ALL/g' && \
        sed -i /etc/sudoers -re 's/^root.*/root ALL=(ALL:ALL) NOPASSWD: ALL/g' && \
        sed -i /etc/sudoers -re 's/^#includedir.*/## **Removed the include directive** ##"/g' && \
        echo "foo ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers && \
        echo "Customized the sudoers file for passwordless access to the foo user!" && \
        echo "foo user:";  su - foo -c id
    

    What happens with the above code:

    • The user and group foo is created.
    • The user foo is added to the both the foo and sudo group.
    • The uid and gid is set to the value of 999.
    • The home directory is set to /home/foo.
    • The shell is set to /bin/bash.
    • The sed command does inline updates to the /etc/sudoers file to allow foo and root users passwordless access to the sudo group.
    • The sed command disables the #includedir directive that would allow any files in subdirectories to override these inline updates.

提交回复
热议问题