How to add users to Docker container?

后端 未结 8 1190
无人及你
无人及你 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:03

    You can imitate open source Dockerfile, for example:

    Node: node12-github

    RUN groupadd --gid 1000 node \
        && useradd --uid 1000 --gid node --shell /bin/bash --create-home node
    

    superset: superset-github

    RUN useradd --user-group --create-home --no-log-init --shell /bin/bash 
        superset
    

    I think it's a good way to follow open source.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题