How to add users to Docker container?

后端 未结 8 1237
无人及你
无人及你 2020-11-28 17:07

I have a docker container with some processes (uwsgi and celery) running inside. I want to create a celery user and a uwsgi user for these processes as well as a worker grou

8条回答
  •  星月不相逢
    2020-11-28 18:05

    The trick is to use useradd instead of its interactive wrapper adduser. I usually create users with:

    RUN useradd -ms /bin/bash newuser
    

    which creates a home directory for the user and ensures that bash is the default shell.

    You can then add:

    USER newuser
    WORKDIR /home/newuser
    

    to your dockerfile. Every command afterwards as well as interactive sessions will be executed as user newuser:

    docker run -t -i image
    newuser@131b7ad86360:~$
    

    You might have to give newuser the permissions to execute the programs you intend to run before invoking the user command.

    Using non-privileged users inside containers is a good idea for security reasons. It also has a few drawbacks. Most importantly, people deriving images from your image will have to switch back to root before they can execute commands with superuser privileges.

提交回复
热议问题