How to use sudo inside a docker container?

前端 未结 11 769
臣服心动
臣服心动 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 16:48

    The other answers didn't work for me. I kept searching and found a blog post that covered how a team was running non-root inside of a docker container.

    Here's the TL;DR version:

    RUN apt-get update \
     && apt-get install -y sudo
    
    RUN adduser --disabled-password --gecos '' docker
    RUN adduser docker sudo
    RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
    
    USER docker
    
    # this is where I was running into problems with the other approaches
    RUN sudo apt-get update 
    

    I was using FROM node:9.3 for this, but I suspect that other similar container bases would work as well.

提交回复
热议问题