How to use sudo inside a docker container?

前端 未结 11 752
臣服心动
臣服心动 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 16:57

    If you have a container running as root that runs a script (which you can't change) that needs access to the sudo command, you can simply create a new sudo script in your $PATH that calls the passed command.

    e.g. In your Dockerfile:

    RUN if type sudo 2>/dev/null; then \ 
         echo "The sudo command already exists... Skipping."; \
        else \
         echo -e "#!/bin/sh\n\${@}" > /usr/sbin/sudo; \
         chmod +x /usr/sbin/sudo; \
        fi
    

提交回复
热议问题